import { Operation, FieldDescriptor, HLCTimestamp } from '@korajs/core'; import { O as OperationRow, R as RawCollectionRow, C as CollectionRecord, W as WriteOptions } from './types-BVVjCckP.js'; export { e as MetaRow, f as Subscription, g as VersionVectorRow } from './types-BVVjCckP.js'; import { L as LocalMutationContext } from './sql-builder-DWY0xBiA.js'; export { D as DatabaseQuery, S as SqlQuery, b as buildCountQuery, a as buildInsertQuery, c as buildLwwSoftDeleteQuery, d as buildLwwUpdateQuery, e as buildSelectQuery, f as buildSoftDeleteQuery, g as buildUpdateQuery, h as buildWhereClause, i as hasUnsyncedOwnOperations } from './sql-builder-DWY0xBiA.js'; import '@korajs/core/lite'; /** * Serialize a JS record to SQL-compatible values for INSERT/UPDATE. * Transforms: boolean → 0/1, array → JSON string, richtext → Yjs binary update. * * @param data - The record data with JS-native types * @param fields - The field descriptors from the schema * @returns An object with SQL-compatible values */ declare function serializeRecord(data: Record, fields: Record): Record; /** * Deserialize a SQL row to JS-native types for the application layer. * Transforms: 0/1 → boolean, JSON string → array, strips _deleted, maps _created_at/_updated_at. * * @param row - The raw SQL row * @param fields - The field descriptors from the schema * @returns A CollectionRecord with JS-native types */ declare function deserializeRecord(row: RawCollectionRow, fields: Record): CollectionRecord; /** * Serialize an Operation to a row for the operations log table. * * @param op - The operation to serialize * @returns An OperationRow suitable for SQL INSERT */ declare function serializeOperation(op: Operation): OperationRow; /** * Deserialize a row from the operations log table back to an Operation. * * @param row - The raw operation row from SQL * @returns The deserialized Operation object */ declare function deserializeOperation(row: OperationRow): Operation; /** * Deserialize an operation row with collection name already known. */ declare function deserializeOperationWithCollection(row: OperationRow, collection: string): Operation; /** * Serialize an HLC timestamp for storage in the `_version` column. * Lexicographic order matches {@link HybridLogicalClock.compare}. */ declare function serializeRowVersion(timestamp: HLCTimestamp): string; /** * Read the version stamp stored on a materialized row. * Legacy rows without `_version` fall back to wallTime with logical 0. */ declare function rowVersionFromRecord(row: Pick): HLCTimestamp; /** * Returns true when `incoming` is strictly newer than the version on `row`. */ declare function isIncomingNewerThanRow(incoming: HLCTimestamp, row: Pick): boolean; /** * Insert a record and persist its operation, atomically, through the single * local write path (sequence reserved and operation built inside the write * transaction). * * @param options - `durable: true` shows the record only once it has committed (no * instant-updates prediction for this write) */ declare function executeInsert(ctx: LocalMutationContext, data: Record, options?: WriteOptions): Promise; /** * Insert several records of one collection in ONE storage transaction: all of them * or none. * * Every row is validated (and its secret fields transformed) before the * transaction starts, so an invalid row refuses the whole call without writing * anything. Each row then becomes its own insert operation through the single * local write path, in input order: consecutive sequence numbers from the * transaction's block, increasing HLC timestamps, each operation causally after * the previous one, and one shared `transactionId`, exactly like the same inserts * made in `store.transaction`. Subscriptions and `operation:created` see the * operations only after the commit, in that order. * * With instant updates on, a batch within the per-write record cap is shown at once as * one entry per record (all together or none); a larger batch waits for the commit. * * @returns The inserted records, in input order */ declare function executeInsertMany(ctx: LocalMutationContext, rows: readonly Record[], options?: WriteOptions): Promise; /** * Update a record and persist its operation, atomically. The row read, the * state-machine check and atomic-op resolution all happen inside the write * transaction, so concurrent increments compose (STORE-9). * * @param options - `durable: true` shows the change only once it has committed (no * instant-updates prediction for this write) */ declare function executeUpdate(ctx: LocalMutationContext, id: string, data: Record, options?: WriteOptions): Promise; interface ExecuteDeleteOptions extends WriteOptions { /** When true, skip referential checks and cascades (caller already enforced them). */ skipReferentialEnforcement?: boolean; } /** * Soft-delete a record and persist the operation log entry atomically, together * with any referential side effects (cascades / set-null), in one transaction. * Returns the primary delete operation followed by the side-effect operations. */ declare function executeDelete(ctx: LocalMutationContext, id: string, options?: ExecuteDeleteOptions): Promise; /** * Internal write-to-UI phase timers (instant-UI stage S0). * * Measures where the latency between `await collection.insert(...)` and a reactive * subscriber callback goes. This is a measurement aid for benchmarks and the * design doc `docs/design/instant-ui-updates.md`; it is NOT public API and is only * reachable through `@korajs/store/internal`. * * Cost when nothing is installed: one module-level null check per hook. Timers only * observe: no hook changes ordering, awaits or results of the write or read path. */ /** The kind of local write a record belongs to. */ type PhaseWriteKind = 'insert' | 'update' | 'delete'; /** * Phases, in the order a write-to-callback travels through them. * * Write side (main thread view of `runLocalWrite`): * - `prepare`: validation, defaults, id generation before the transaction opens. * - `txOpen`: from calling `adapter.transaction` until the body starts (adapter mutex wait plus BEGIN round trip). * - `sequence`: sequence reservation inside the body (a subset of `body`). * - `body`: the transaction body (sequence reservation, operation build, buffered statements). * - `commit`: from the body returning until `adapter.transaction` resolves (flush of buffered statements plus COMMIT). * - `publish`: synchronous post-commit work (causal heads, observer, `subscriptionManager.notify`, `operation:created`). * * Read side (subscription manager, summed over every affected subscription): * - `flushDelay`: from `notify` to the start of the flush microtask. * - `findAffected`: choosing the affected subscriptions. * - `reread`: `executeFn` (adapter mutex wait plus the SELECT plus row decoding). * - `diffNotify`: result equality check plus the subscriber callback itself. */ type PhaseName = 'prepare' | 'txOpen' | 'sequence' | 'body' | 'commit' | 'publish' | 'flushDelay' | 'findAffected' | 'reread' | 'diffNotify'; /** All phase names, in travel order. */ declare const PHASE_NAMES: readonly PhaseName[]; /** Timings of one write and the subscription re-runs it caused. */ interface PhaseRecord { readonly kind: PhaseWriteKind; /** Milliseconds per phase (summed when a phase ran more than once). */ readonly phases: Record; /** Number of subscription re-runs. */ rereads: number; /** Number of subscriber callbacks actually invoked. */ callbacks: number; /** `performance.now()` when the write was started. */ readonly startedAt: number; /** `performance.now()` of the first subscriber callback after the commit, or null. */ firstCallbackAt: number | null; /** `performance.now()` when `publish` ended (the notify moment). */ publishedAt: number | null; } /** * Collects {@link PhaseRecord}s. One record is open at a time (writes in a * measurement are sequential); read-side phases are attributed to it. */ declare class PhaseTimers { private current; private readonly finished; /** Start a record for a write; closes the previous open record. */ begin(kind: PhaseWriteKind): void; /** Add elapsed milliseconds to a phase of the open record. */ add(phase: PhaseName, ms: number): void; /** Note that one subscription re-run finished. */ noteReread(): void; /** Note the moment the post-commit publish ended. */ notePublished(at: number): void; /** Note a subscriber callback invocation. */ noteCallback(at: number): void; /** The open record, if any. */ peek(): PhaseRecord | null; /** Close the open record and return every finished record, clearing the buffer. */ take(): PhaseRecord[]; private close; } /** Install (or with null, remove) the process-wide timers. Internal. */ declare function installPhaseTimers(timers: PhaseTimers | null): void; /** The installed timers, or null (the normal case). */ declare function activePhaseTimers(): PhaseTimers | null; /** Median and 95th percentile of a sample (nearest rank). */ declare function summarizeSamples(samples: readonly number[]): { p50: number; p95: number; }; /** Per-phase p50 and p95 over a list of records. */ declare function summarizePhases(records: readonly PhaseRecord[]): Record; export { type ExecuteDeleteOptions, LocalMutationContext, OperationRow, PHASE_NAMES, type PhaseName, type PhaseRecord, PhaseTimers, type PhaseWriteKind, RawCollectionRow, activePhaseTimers, deserializeOperation, deserializeOperationWithCollection, deserializeRecord, executeDelete, executeInsert, executeInsertMany, executeUpdate, installPhaseTimers, isIncomingNewerThanRow, rowVersionFromRecord, serializeOperation, serializeRecord, serializeRowVersion, summarizePhases, summarizeSamples };