@korajs/store 0.3.2 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/better-sqlite3.cjs +1 -1
- package/dist/adapters/better-sqlite3.cjs.map +1 -1
- package/dist/adapters/better-sqlite3.d.cts +1 -1
- package/dist/adapters/better-sqlite3.d.ts +1 -1
- package/dist/adapters/better-sqlite3.js +1 -1
- package/dist/adapters/better-sqlite3.js.map +1 -1
- package/dist/adapters/indexeddb.cjs +413 -18
- package/dist/adapters/indexeddb.cjs.map +1 -1
- package/dist/adapters/indexeddb.d.cts +23 -5
- package/dist/adapters/indexeddb.d.ts +23 -5
- package/dist/adapters/indexeddb.js +166 -19
- package/dist/adapters/indexeddb.js.map +1 -1
- package/dist/adapters/sqlite-wasm-shared-host.cjs +68 -0
- package/dist/adapters/sqlite-wasm-shared-host.cjs.map +1 -0
- package/dist/adapters/sqlite-wasm-shared-host.d.cts +2 -0
- package/dist/adapters/sqlite-wasm-shared-host.d.ts +2 -0
- package/dist/adapters/sqlite-wasm-shared-host.js +66 -0
- package/dist/adapters/sqlite-wasm-shared-host.js.map +1 -0
- package/dist/adapters/sqlite-wasm-worker.cjs +34 -5
- package/dist/adapters/sqlite-wasm-worker.cjs.map +1 -1
- package/dist/adapters/sqlite-wasm-worker.js +34 -5
- package/dist/adapters/sqlite-wasm-worker.js.map +1 -1
- package/dist/adapters/sqlite-wasm.cjs +249 -2
- package/dist/adapters/sqlite-wasm.cjs.map +1 -1
- package/dist/adapters/sqlite-wasm.d.cts +15 -3
- package/dist/adapters/sqlite-wasm.d.ts +15 -3
- package/dist/adapters/sqlite-wasm.js +2 -2
- package/dist/audit-trace-store-AM4EWQNZ.js +15 -0
- package/dist/backup-JN3LB4YT.js +14 -0
- package/dist/backup-JN3LB4YT.js.map +1 -0
- package/dist/chunk-56LL4XP6.js +205 -0
- package/dist/chunk-56LL4XP6.js.map +1 -0
- package/dist/chunk-6AHYGQQT.js +425 -0
- package/dist/chunk-6AHYGQQT.js.map +1 -0
- package/dist/chunk-7BPETY5Y.js +570 -0
- package/dist/chunk-7BPETY5Y.js.map +1 -0
- package/dist/chunk-AGN3CIPB.js +188 -0
- package/dist/chunk-AGN3CIPB.js.map +1 -0
- package/dist/chunk-EZSTD2XC.js +380 -0
- package/dist/chunk-EZSTD2XC.js.map +1 -0
- package/dist/chunk-MYSJN3DF.js +117 -0
- package/dist/chunk-MYSJN3DF.js.map +1 -0
- package/dist/{chunk-DXKLAQ6P.js → chunk-XO4LMPAH.js} +1 -1
- package/dist/chunk-XO4LMPAH.js.map +1 -0
- package/dist/export-audit-5GV6VRDZ.js +15 -0
- package/dist/export-audit-5GV6VRDZ.js.map +1 -0
- package/dist/index.cjs +3351 -510
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +822 -50
- package/dist/index.d.ts +822 -50
- package/dist/index.js +1976 -558
- package/dist/index.js.map +1 -1
- package/dist/internal.cjs +789 -0
- package/dist/internal.cjs.map +1 -0
- package/dist/internal.d.cts +154 -0
- package/dist/internal.d.ts +154 -0
- package/dist/internal.js +48 -0
- package/dist/internal.js.map +1 -0
- package/dist/{sqlite-wasm-channel-46AOWNPM.js → sqlite-wasm-channel-HDDW6UBH.js} +2 -2
- package/dist/sqlite-wasm-channel-HDDW6UBH.js.map +1 -0
- package/dist/{sqlite-wasm-channel-Lakjuk2E.d.cts → sqlite-wasm-channel-I9y_jObg.d.cts} +1 -0
- package/dist/{sqlite-wasm-channel-Lakjuk2E.d.ts → sqlite-wasm-channel-I9y_jObg.d.ts} +1 -0
- package/dist/types-BMyHmwvn.d.ts +163 -0
- package/dist/types-CTr00g_d.d.cts +163 -0
- package/dist/types-DepnRDk_.d.cts +212 -0
- package/dist/types-DepnRDk_.d.ts +212 -0
- package/package.json +23 -2
- package/dist/chunk-DXKLAQ6P.js.map +0 -1
- package/dist/chunk-ZP5AXQ3Z.js +0 -179
- package/dist/chunk-ZP5AXQ3Z.js.map +0 -1
- package/dist/types-DF-KDSK1.d.cts +0 -106
- package/dist/types-DF-KDSK1.d.ts +0 -106
- /package/dist/{sqlite-wasm-channel-46AOWNPM.js.map → audit-trace-store-AM4EWQNZ.js.map} +0 -0
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts","../src/errors.ts","../src/store/store.ts","../src/collection/collection.ts","../src/query/sql-builder.ts","../src/serialization/serializer.ts","../src/serialization/richtext-serializer.ts","../src/query/pluralize.ts","../src/query/query-builder.ts","../src/subscription/subscription-manager.ts"],"sourcesContent":["// @korajs/store — public API\n// Every export here is a public API commitment. Be explicit.\n\n// === Types ===\nexport type {\n\tApplyResult,\n\tCollectionRecord,\n\tMigrationPlan,\n\tOrderByClause,\n\tOrderByDirection,\n\tQueryDescriptor,\n\tStoreConfig,\n\tStorageAdapter,\n\tSubscriptionCallback,\n\tTransaction,\n\tWhereClause,\n\tWhereOperators,\n} from './types'\n\n// === Errors ===\nexport {\n\tAdapterError,\n\tPersistenceError,\n\tQueryError,\n\tRecordNotFoundError,\n\tStoreNotOpenError,\n\tWorkerInitError,\n\tWorkerTimeoutError,\n} from './errors'\n\n// === Store ===\nexport { Store } from './store/store'\nexport type { CollectionAccessor } from './store/store'\n\n// === Query ===\nexport { QueryBuilder } from './query/query-builder'\n\n// === Subscription ===\nexport { SubscriptionManager } from './subscription/subscription-manager'\n\n// === Collection ===\nexport { Collection } from './collection/collection'\n\n// === Richtext Serialization ===\nexport { decodeRichtext, encodeRichtext, richtextToPlainText } from './serialization/richtext-serializer'\n\n// === Query Utilities ===\nexport { pluralize, singularize } from './query/pluralize'\n","import { KoraError } from '@korajs/core'\n\n/**\n * Thrown when a query is invalid (bad field names, invalid operators, etc.).\n */\nexport class QueryError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'QUERY_ERROR', context)\n\t\tthis.name = 'QueryError'\n\t}\n}\n\n/**\n * Thrown when a record is not found by ID (findById, update, delete on missing record).\n */\nexport class RecordNotFoundError extends KoraError {\n\tconstructor(collection: string, recordId: string) {\n\t\tsuper(`Record \"${recordId}\" not found in collection \"${collection}\"`, 'RECORD_NOT_FOUND', {\n\t\t\tcollection,\n\t\t\trecordId,\n\t\t})\n\t\tthis.name = 'RecordNotFoundError'\n\t}\n}\n\n/**\n * Thrown when a storage adapter operation fails.\n */\nexport class AdapterError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'ADAPTER_ERROR', context)\n\t\tthis.name = 'AdapterError'\n\t}\n}\n\n/**\n * Thrown when an operation is attempted on a store that has not been opened.\n */\nexport class StoreNotOpenError extends KoraError {\n\tconstructor() {\n\t\tsuper('Store is not open. Call store.open() before performing operations.', 'STORE_NOT_OPEN')\n\t\tthis.name = 'StoreNotOpenError'\n\t}\n}\n\n/**\n * Thrown when the Web Worker fails to initialize (WASM load failure, OPFS unavailable, etc.).\n */\nexport class WorkerInitError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(`Worker initialization failed: ${message}`, 'WORKER_INIT_ERROR', context)\n\t\tthis.name = 'WorkerInitError'\n\t}\n}\n\n/**\n * Thrown when the Web Worker does not respond within the configured timeout.\n */\nexport class WorkerTimeoutError extends KoraError {\n\tconstructor(operation: string, timeoutMs: number) {\n\t\tsuper(\n\t\t\t`Worker did not respond within ${timeoutMs}ms for operation \"${operation}\"`,\n\t\t\t'WORKER_TIMEOUT',\n\t\t\t{ operation, timeoutMs },\n\t\t)\n\t\tthis.name = 'WorkerTimeoutError'\n\t}\n}\n\n/**\n * Thrown when IndexedDB persistence operations fail (serialize/deserialize).\n */\nexport class PersistenceError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(`Persistence error: ${message}`, 'PERSISTENCE_ERROR', context)\n\t\tthis.name = 'PersistenceError'\n\t}\n}\n","import { HybridLogicalClock, createVersionVector, generateUUIDv7 } from '@korajs/core'\nimport type {\n\tKoraEventEmitter,\n\tOperation,\n\tOperationLog,\n\tSchemaDefinition,\n\tVersionVector,\n} from '@korajs/core'\nimport { Collection } from '../collection/collection'\nimport { StoreNotOpenError } from '../errors'\nimport { QueryBuilder } from '../query/query-builder'\nimport { buildInsertQuery, buildSoftDeleteQuery, buildUpdateQuery } from '../query/sql-builder'\nimport {\n\tdeserializeOperationWithCollection,\n\tserializeOperation,\n\tserializeRecord,\n} from '../serialization/serializer'\nimport { SubscriptionManager } from '../subscription/subscription-manager'\nimport type {\n\tApplyResult,\n\tMetaRow,\n\tOperationRow,\n\tRawCollectionRow,\n\tStorageAdapter,\n\tStoreConfig,\n\tVersionVectorRow,\n} from '../types'\n\n/**\n * Store is the main orchestrator. It owns a schema, a storage adapter,\n * a clock, and a subscription manager. It creates Collection instances\n * for each schema collection, and provides the sync contract via\n * applyRemoteOperation and getOperationRange.\n *\n * @example\n * ```typescript\n * const store = new Store({ schema, adapter })\n * await store.open()\n * const todo = await store.collection('todos').insert({ title: 'Hello' })\n * await store.close()\n * ```\n */\nexport class Store implements OperationLog {\n\tprivate opened = false\n\tprivate nodeId = ''\n\tprivate sequenceNumber = 0\n\tprivate versionVector: VersionVector = createVersionVector()\n\tprivate clock: HybridLogicalClock | null = null\n\tprivate collections = new Map<string, Collection>()\n\tprivate subscriptionManager = new SubscriptionManager()\n\n\tprivate readonly schema: SchemaDefinition\n\tprivate readonly adapter: StorageAdapter\n\tprivate readonly configNodeId: string | undefined\n\tprivate readonly emitter: KoraEventEmitter | null\n\n\tconstructor(config: StoreConfig) {\n\t\tthis.schema = config.schema\n\t\tthis.adapter = config.adapter\n\t\tthis.configNodeId = config.nodeId\n\t\tthis.emitter = config.emitter ?? null\n\t}\n\n\t/**\n\t * Open the store: initialize the database, load or generate a node ID,\n\t * restore the sequence number and version vector, and create Collection instances.\n\t */\n\tasync open(): Promise<void> {\n\t\tawait this.adapter.open(this.schema)\n\n\t\t// Load or generate node ID\n\t\tthis.nodeId = await this.loadOrGenerateNodeId()\n\t\tthis.clock = new HybridLogicalClock(this.nodeId)\n\n\t\t// Load sequence number and version vector\n\t\tthis.sequenceNumber = await this.loadSequenceNumber()\n\t\tthis.versionVector = await this.loadVersionVector()\n\n\t\t// Create collection instances\n\t\tfor (const [name, definition] of Object.entries(this.schema.collections)) {\n\t\t\tconst col = new Collection(\n\t\t\t\tname,\n\t\t\t\tdefinition,\n\t\t\t\tthis.schema,\n\t\t\t\tthis.adapter,\n\t\t\t\tthis.clock,\n\t\t\t\tthis.nodeId,\n\t\t\t\t() => this.nextSequenceNumber(),\n\t\t\t\t(collectionName, operation) => {\n\t\t\t\t\tthis.subscriptionManager.notify(collectionName, operation)\n\t\t\t\t\tif (this.emitter) {\n\t\t\t\t\t\tthis.emitter.emit({ type: 'operation:created', operation })\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t)\n\t\t\tthis.collections.set(name, col)\n\t\t}\n\n\t\tthis.opened = true\n\t}\n\n\t/**\n\t * Close the store: clear subscriptions and close the adapter.\n\t */\n\tasync close(): Promise<void> {\n\t\tthis.subscriptionManager.clear()\n\t\tthis.collections.clear()\n\t\tthis.opened = false\n\t\tawait this.adapter.close()\n\t}\n\n\t/**\n\t * Get a Collection instance for CRUD operations.\n\t * @throws {StoreNotOpenError} If the store is not open\n\t * @throws {Error} If the collection name is not in the schema\n\t */\n\tcollection(name: string): CollectionAccessor {\n\t\tthis.ensureOpen()\n\t\tconst col = this.collections.get(name)\n\t\tif (!col) {\n\t\t\tthrow new Error(\n\t\t\t\t`Unknown collection \"${name}\". Available: ${[...this.collections.keys()].join(', ')}`,\n\t\t\t)\n\t\t}\n\n\t\tconst definition = this.schema.collections[name]\n\t\tif (!definition) {\n\t\t\tthrow new Error(`Collection definition not found for \"${name}\"`)\n\t\t}\n\n\t\treturn {\n\t\t\tinsert: (data: Record<string, unknown>) => col.insert(data),\n\t\t\tfindById: (id: string) => col.findById(id),\n\t\t\tupdate: (id: string, data: Record<string, unknown>) => col.update(id, data),\n\t\t\tdelete: (id: string) => col.delete(id),\n\t\t\twhere: (conditions) =>\n\t\t\t\tnew QueryBuilder(name, definition, this.adapter, this.subscriptionManager, conditions, this.schema),\n\t\t}\n\t}\n\n\t/**\n\t * Get the current version vector.\n\t */\n\tgetVersionVector(): VersionVector {\n\t\tthis.ensureOpen()\n\t\treturn new Map(this.versionVector)\n\t}\n\n\t/**\n\t * Get the node ID for this store instance.\n\t */\n\tgetNodeId(): string {\n\t\tthis.ensureOpen()\n\t\treturn this.nodeId\n\t}\n\n\t/**\n\t * Apply a remote operation received from sync.\n\t * Checks for duplicates, applies to the data table, persists the operation,\n\t * and updates the version vector.\n\t */\n\tasync applyRemoteOperation(op: Operation): Promise<ApplyResult> {\n\t\tthis.ensureOpen()\n\n\t\tconst collection = op.collection\n\t\tconst definition = this.schema.collections[collection]\n\t\tif (!definition) {\n\t\t\treturn 'skipped'\n\t\t}\n\n\t\t// Check for duplicate (content-addressed dedup)\n\t\tconst existing = await this.adapter.query<{ id: string }>(\n\t\t\t`SELECT id FROM _kora_ops_${collection} WHERE id = ?`,\n\t\t\t[op.id],\n\t\t)\n\t\tif (existing.length > 0) {\n\t\t\treturn 'duplicate'\n\t\t}\n\n\t\t// Update the clock with the remote timestamp\n\t\tif (this.clock) {\n\t\t\tthis.clock.receive(op.timestamp)\n\t\t}\n\n\t\t// Apply the operation to the data table\n\t\tawait this.adapter.transaction(async (tx) => {\n\t\t\tif (op.type === 'insert' && op.data) {\n\t\t\t\tconst serializedData = serializeRecord(op.data, definition.fields)\n\t\t\t\tconst now = op.timestamp.wallTime\n\t\t\t\tconst record: Record<string, unknown> = {\n\t\t\t\t\tid: op.recordId,\n\t\t\t\t\t...serializedData,\n\t\t\t\t\t_created_at: now,\n\t\t\t\t\t_updated_at: now,\n\t\t\t\t}\n\t\t\t\tconst insertQuery = buildInsertQuery(collection, record)\n\t\t\t\tawait tx.execute(insertQuery.sql, insertQuery.params)\n\t\t\t} else if (op.type === 'update' && op.data) {\n\t\t\t\tconst serializedChanges = serializeRecord(op.data, definition.fields)\n\t\t\t\tconst updateQuery = buildUpdateQuery(collection, op.recordId, {\n\t\t\t\t\t...serializedChanges,\n\t\t\t\t\t_updated_at: op.timestamp.wallTime,\n\t\t\t\t})\n\t\t\t\tawait tx.execute(updateQuery.sql, updateQuery.params)\n\t\t\t} else if (op.type === 'delete') {\n\t\t\t\tconst deleteQuery = buildSoftDeleteQuery(collection, op.recordId, op.timestamp.wallTime)\n\t\t\t\tawait tx.execute(deleteQuery.sql, deleteQuery.params)\n\t\t\t}\n\n\t\t\t// Persist the operation\n\t\t\tconst opRow = serializeOperation(op)\n\t\t\tconst opInsert = buildInsertQuery(\n\t\t\t\t`_kora_ops_${collection}`,\n\t\t\t\topRow as unknown as Record<string, unknown>,\n\t\t\t)\n\t\t\tawait tx.execute(opInsert.sql, opInsert.params)\n\n\t\t\t// Update version vector\n\t\t\tconst currentSeq = this.versionVector.get(op.nodeId) ?? 0\n\t\t\tif (op.sequenceNumber > currentSeq) {\n\t\t\t\tthis.versionVector.set(op.nodeId, op.sequenceNumber)\n\t\t\t\tawait tx.execute(\n\t\t\t\t\t'INSERT OR REPLACE INTO _kora_version_vector (node_id, sequence_number) VALUES (?, ?)',\n\t\t\t\t\t[op.nodeId, op.sequenceNumber],\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\n\t\t// Notify subscriptions\n\t\tthis.subscriptionManager.notify(collection, op)\n\n\t\treturn 'applied'\n\t}\n\n\t/**\n\t * Get operations from a node within a sequence number range.\n\t * Implements the OperationLog interface for computeDelta.\n\t */\n\tgetRange(nodeId: string, fromSeq: number, toSeq: number): Operation[] {\n\t\t// This is synchronous per the OperationLog interface.\n\t\t// We can't use async here, so this must be called with data already loaded.\n\t\t// For now, this is a placeholder that the sync layer will call after awaiting.\n\t\treturn []\n\t}\n\n\t/**\n\t * Async version of getRange for use by the sync layer.\n\t */\n\tasync getOperationRange(nodeId: string, fromSeq: number, toSeq: number): Promise<Operation[]> {\n\t\tthis.ensureOpen()\n\t\tconst allOps: Operation[] = []\n\n\t\tfor (const collectionName of Object.keys(this.schema.collections)) {\n\t\t\tconst rows = await this.adapter.query<OperationRow>(\n\t\t\t\t`SELECT * FROM _kora_ops_${collectionName} WHERE node_id = ? AND sequence_number >= ? AND sequence_number <= ? ORDER BY sequence_number ASC`,\n\t\t\t\t[nodeId, fromSeq, toSeq],\n\t\t\t)\n\t\t\tfor (const row of rows) {\n\t\t\t\tallOps.push(deserializeOperationWithCollection(row, collectionName))\n\t\t\t}\n\t\t}\n\n\t\t// Sort by sequence number across collections\n\t\tallOps.sort((a, b) => a.sequenceNumber - b.sequenceNumber)\n\t\treturn allOps\n\t}\n\n\t/**\n\t * Get the schema definition.\n\t */\n\tgetSchema(): SchemaDefinition {\n\t\treturn this.schema\n\t}\n\n\t/** Expose the subscription manager for direct access (e.g., by QueryBuilder) */\n\tgetSubscriptionManager(): SubscriptionManager {\n\t\treturn this.subscriptionManager\n\t}\n\n\tprivate nextSequenceNumber(): number {\n\t\tthis.sequenceNumber++\n\t\tthis.versionVector.set(this.nodeId, this.sequenceNumber)\n\t\treturn this.sequenceNumber\n\t}\n\n\tprivate async loadOrGenerateNodeId(): Promise<string> {\n\t\tif (this.configNodeId) {\n\t\t\t// Persist the configured node ID\n\t\t\tawait this.adapter.execute(\n\t\t\t\t\"INSERT OR REPLACE INTO _kora_meta (key, value) VALUES ('node_id', ?)\",\n\t\t\t\t[this.configNodeId],\n\t\t\t)\n\t\t\treturn this.configNodeId\n\t\t}\n\n\t\t// Try to load existing node ID\n\t\tconst rows = await this.adapter.query<MetaRow>(\n\t\t\t\"SELECT value FROM _kora_meta WHERE key = 'node_id'\",\n\t\t)\n\t\tif (rows[0]) {\n\t\t\treturn rows[0].value\n\t\t}\n\n\t\t// Generate new node ID\n\t\tconst newNodeId = generateUUIDv7()\n\t\tawait this.adapter.execute(\"INSERT INTO _kora_meta (key, value) VALUES ('node_id', ?)\", [\n\t\t\tnewNodeId,\n\t\t])\n\t\treturn newNodeId\n\t}\n\n\tprivate async loadSequenceNumber(): Promise<number> {\n\t\tconst rows = await this.adapter.query<VersionVectorRow>(\n\t\t\t'SELECT sequence_number FROM _kora_version_vector WHERE node_id = ?',\n\t\t\t[this.nodeId],\n\t\t)\n\t\treturn rows[0]?.sequence_number ?? 0\n\t}\n\n\tprivate async loadVersionVector(): Promise<VersionVector> {\n\t\tconst rows = await this.adapter.query<VersionVectorRow>(\n\t\t\t'SELECT node_id, sequence_number FROM _kora_version_vector',\n\t\t)\n\t\tconst vector = createVersionVector()\n\t\tfor (const row of rows) {\n\t\t\tvector.set(row.node_id, row.sequence_number)\n\t\t}\n\t\treturn vector\n\t}\n\n\tprivate ensureOpen(): void {\n\t\tif (!this.opened) {\n\t\t\tthrow new StoreNotOpenError()\n\t\t}\n\t}\n}\n\n/**\n * Public-facing collection accessor. Provides CRUD + where.\n */\nexport interface CollectionAccessor {\n\tinsert(data: Record<string, unknown>): Promise<import('../types').CollectionRecord>\n\tfindById(id: string): Promise<import('../types').CollectionRecord | null>\n\tupdate(id: string, data: Record<string, unknown>): Promise<import('../types').CollectionRecord>\n\tdelete(id: string): Promise<void>\n\twhere(conditions: Record<string, unknown>): QueryBuilder\n}\n","import type {\n\tCollectionDefinition,\n\tHLCTimestamp,\n\tHybridLogicalClock,\n\tOperation,\n\tSchemaDefinition,\n} from '@korajs/core'\nimport { createOperation, generateUUIDv7, validateRecord } from '@korajs/core'\nimport { RecordNotFoundError } from '../errors'\nimport { buildInsertQuery, buildSoftDeleteQuery, buildUpdateQuery } from '../query/sql-builder'\nimport { deserializeRecord, serializeOperation, serializeRecord } from '../serialization/serializer'\nimport type { CollectionRecord, RawCollectionRow, StorageAdapter } from '../types'\n\n/**\n * Callback invoked after a mutation so the Store can notify subscriptions.\n */\nexport type MutationCallback = (collection: string, operation: Operation) => void\n\n/**\n * Collection provides CRUD operations on a single schema collection.\n * Each mutation creates an Operation and persists both the data and the operation atomically.\n */\nexport class Collection {\n\tconstructor(\n\t\tprivate readonly name: string,\n\t\tprivate readonly definition: CollectionDefinition,\n\t\tprivate readonly schema: SchemaDefinition,\n\t\tprivate readonly adapter: StorageAdapter,\n\t\tprivate readonly clock: HybridLogicalClock,\n\t\tprivate readonly nodeId: string,\n\t\tprivate readonly getSequenceNumber: () => number,\n\t\tprivate readonly onMutation: MutationCallback,\n\t) {}\n\n\t/**\n\t * Insert a new record into the collection.\n\t * Generates a UUID v7 for the id, validates data, and persists atomically.\n\t *\n\t * @param data - The record data (auto fields and defaults are applied automatically)\n\t * @returns The inserted record with id, createdAt, updatedAt\n\t */\n\tasync insert(data: Record<string, unknown>): Promise<CollectionRecord> {\n\t\tconst validated = validateRecord(this.name, this.definition, data, 'insert')\n\t\tconst recordId = generateUUIDv7()\n\t\tconst now = Date.now()\n\n\t\t// Set auto timestamp fields\n\t\tfor (const [fieldName, descriptor] of Object.entries(this.definition.fields)) {\n\t\t\tif (descriptor.auto && descriptor.kind === 'timestamp') {\n\t\t\t\tvalidated[fieldName] = now\n\t\t\t}\n\t\t}\n\n\t\tconst sequenceNumber = this.getSequenceNumber()\n\t\tconst operation = await createOperation(\n\t\t\t{\n\t\t\t\tnodeId: this.nodeId,\n\t\t\t\ttype: 'insert',\n\t\t\t\tcollection: this.name,\n\t\t\t\trecordId,\n\t\t\t\tdata: { ...validated },\n\t\t\t\tpreviousData: null,\n\t\t\t\tsequenceNumber,\n\t\t\t\tcausalDeps: [],\n\t\t\t\tschemaVersion: this.schema.version,\n\t\t\t},\n\t\t\tthis.clock,\n\t\t)\n\n\t\tconst serializedData = serializeRecord(validated, this.definition.fields)\n\t\tconst record: Record<string, unknown> = {\n\t\t\tid: recordId,\n\t\t\t...serializedData,\n\t\t\t_created_at: now,\n\t\t\t_updated_at: now,\n\t\t}\n\n\t\tconst insertQuery = buildInsertQuery(this.name, record)\n\t\tconst opRow = serializeOperation(operation)\n\t\tconst opInsert = buildInsertQuery(\n\t\t\t`_kora_ops_${this.name}`,\n\t\t\topRow as unknown as Record<string, unknown>,\n\t\t)\n\n\t\tawait this.adapter.transaction(async (tx) => {\n\t\t\tawait tx.execute(insertQuery.sql, insertQuery.params)\n\t\t\tawait tx.execute(opInsert.sql, opInsert.params)\n\t\t\tawait tx.execute(\n\t\t\t\t'INSERT OR REPLACE INTO _kora_version_vector (node_id, sequence_number) VALUES (?, ?)',\n\t\t\t\t[this.nodeId, sequenceNumber],\n\t\t\t)\n\t\t})\n\n\t\tthis.onMutation(this.name, operation)\n\n\t\treturn {\n\t\t\tid: recordId,\n\t\t\t...validated,\n\t\t\tcreatedAt: now,\n\t\t\tupdatedAt: now,\n\t\t}\n\t}\n\n\t/**\n\t * Find a record by its ID. Returns null if not found or soft-deleted.\n\t */\n\tasync findById(id: string): Promise<CollectionRecord | null> {\n\t\tconst rows = await this.adapter.query<RawCollectionRow>(\n\t\t\t`SELECT * FROM ${this.name} WHERE id = ? AND _deleted = 0`,\n\t\t\t[id],\n\t\t)\n\n\t\tconst row = rows[0]\n\t\tif (!row) return null\n\t\treturn deserializeRecord(row, this.definition.fields)\n\t}\n\n\t/**\n\t * Update an existing record. Only the provided fields are changed.\n\t *\n\t * @param id - The record ID to update\n\t * @param data - Partial data with only the fields to change\n\t * @returns The updated record\n\t * @throws {RecordNotFoundError} If the record doesn't exist or is deleted\n\t */\n\tasync update(id: string, data: Record<string, unknown>): Promise<CollectionRecord> {\n\t\tconst currentRows = await this.adapter.query<RawCollectionRow>(\n\t\t\t`SELECT * FROM ${this.name} WHERE id = ? AND _deleted = 0`,\n\t\t\t[id],\n\t\t)\n\t\tconst currentRow = currentRows[0]\n\t\tif (!currentRow) {\n\t\t\tthrow new RecordNotFoundError(this.name, id)\n\t\t}\n\n\t\tconst validated = validateRecord(this.name, this.definition, data, 'update')\n\t\tconst now = Date.now()\n\n\t\t// Build previousData from current row for the changed fields\n\t\tconst previousData: Record<string, unknown> = {}\n\t\tconst currentRecord = deserializeRecord(currentRow, this.definition.fields)\n\t\tfor (const key of Object.keys(validated)) {\n\t\t\tpreviousData[key] = currentRecord[key]\n\t\t}\n\n\t\tconst sequenceNumber = this.getSequenceNumber()\n\t\tconst operation = await createOperation(\n\t\t\t{\n\t\t\t\tnodeId: this.nodeId,\n\t\t\t\ttype: 'update',\n\t\t\t\tcollection: this.name,\n\t\t\t\trecordId: id,\n\t\t\t\tdata: { ...validated },\n\t\t\t\tpreviousData,\n\t\t\t\tsequenceNumber,\n\t\t\t\tcausalDeps: [],\n\t\t\t\tschemaVersion: this.schema.version,\n\t\t\t},\n\t\t\tthis.clock,\n\t\t)\n\n\t\tconst serializedChanges = serializeRecord(validated, this.definition.fields)\n\t\tconst updateQuery = buildUpdateQuery(this.name, id, {\n\t\t\t...serializedChanges,\n\t\t\t_updated_at: now,\n\t\t})\n\t\tconst opRow = serializeOperation(operation)\n\t\tconst opInsert = buildInsertQuery(\n\t\t\t`_kora_ops_${this.name}`,\n\t\t\topRow as unknown as Record<string, unknown>,\n\t\t)\n\n\t\tawait this.adapter.transaction(async (tx) => {\n\t\t\tawait tx.execute(updateQuery.sql, updateQuery.params)\n\t\t\tawait tx.execute(opInsert.sql, opInsert.params)\n\t\t\tawait tx.execute(\n\t\t\t\t'INSERT OR REPLACE INTO _kora_version_vector (node_id, sequence_number) VALUES (?, ?)',\n\t\t\t\t[this.nodeId, sequenceNumber],\n\t\t\t)\n\t\t})\n\n\t\tthis.onMutation(this.name, operation)\n\n\t\t// Return the full updated record\n\t\tconst updatedRow = await this.findById(id)\n\t\tif (!updatedRow) {\n\t\t\tthrow new RecordNotFoundError(this.name, id)\n\t\t}\n\t\treturn updatedRow\n\t}\n\n\t/**\n\t * Soft-delete a record by its ID.\n\t *\n\t * @param id - The record ID to delete\n\t * @throws {RecordNotFoundError} If the record doesn't exist or is already deleted\n\t */\n\tasync delete(id: string): Promise<void> {\n\t\tconst currentRows = await this.adapter.query<RawCollectionRow>(\n\t\t\t`SELECT * FROM ${this.name} WHERE id = ? AND _deleted = 0`,\n\t\t\t[id],\n\t\t)\n\t\tif (!currentRows[0]) {\n\t\t\tthrow new RecordNotFoundError(this.name, id)\n\t\t}\n\n\t\tconst now = Date.now()\n\t\tconst sequenceNumber = this.getSequenceNumber()\n\t\tconst operation = await createOperation(\n\t\t\t{\n\t\t\t\tnodeId: this.nodeId,\n\t\t\t\ttype: 'delete',\n\t\t\t\tcollection: this.name,\n\t\t\t\trecordId: id,\n\t\t\t\tdata: null,\n\t\t\t\tpreviousData: null,\n\t\t\t\tsequenceNumber,\n\t\t\t\tcausalDeps: [],\n\t\t\t\tschemaVersion: this.schema.version,\n\t\t\t},\n\t\t\tthis.clock,\n\t\t)\n\n\t\tconst deleteQuery = buildSoftDeleteQuery(this.name, id, now)\n\t\tconst opRow = serializeOperation(operation)\n\t\tconst opInsert = buildInsertQuery(\n\t\t\t`_kora_ops_${this.name}`,\n\t\t\topRow as unknown as Record<string, unknown>,\n\t\t)\n\n\t\tawait this.adapter.transaction(async (tx) => {\n\t\t\tawait tx.execute(deleteQuery.sql, deleteQuery.params)\n\t\t\tawait tx.execute(opInsert.sql, opInsert.params)\n\t\t\tawait tx.execute(\n\t\t\t\t'INSERT OR REPLACE INTO _kora_version_vector (node_id, sequence_number) VALUES (?, ?)',\n\t\t\t\t[this.nodeId, sequenceNumber],\n\t\t\t)\n\t\t})\n\n\t\tthis.onMutation(this.name, operation)\n\t}\n\n\t/** Get the collection name */\n\tgetName(): string {\n\t\treturn this.name\n\t}\n\n\t/** Get the collection definition */\n\tgetDefinition(): CollectionDefinition {\n\t\treturn this.definition\n\t}\n}\n","import type { CollectionDefinition, FieldDescriptor } from '@korajs/core'\nimport { QueryError } from '../errors'\nimport type { QueryDescriptor, WhereOperators } from '../types'\n\n/**\n * Result of building a SQL query: the parameterized SQL string and its bound values.\n */\nexport interface SqlQuery {\n\tsql: string\n\tparams: unknown[]\n}\n\n/**\n * Build a SELECT query from a QueryDescriptor.\n * Automatically adds `WHERE _deleted = 0` to exclude soft-deleted records.\n *\n * @param descriptor - The query descriptor\n * @param fields - The field descriptors from the collection schema\n * @returns A parameterized SQL query\n */\nexport function buildSelectQuery(\n\tdescriptor: QueryDescriptor,\n\tfields: Record<string, FieldDescriptor>,\n): SqlQuery {\n\tconst params: unknown[] = []\n\tconst parts = [`SELECT * FROM ${descriptor.collection}`]\n\n\tconst whereClause = buildWhereClauseParts(descriptor.where, fields, params)\n\t// Always filter out soft-deleted records\n\tconst deletedFilter = '_deleted = 0'\n\tif (whereClause) {\n\t\tparts.push(`WHERE ${deletedFilter} AND ${whereClause}`)\n\t} else {\n\t\tparts.push(`WHERE ${deletedFilter}`)\n\t}\n\n\tif (descriptor.orderBy.length > 0) {\n\t\tconst orderParts = descriptor.orderBy.map((o) => {\n\t\t\tvalidateFieldName(o.field, fields)\n\t\t\treturn `${o.field} ${o.direction.toUpperCase()}`\n\t\t})\n\t\tparts.push(`ORDER BY ${orderParts.join(', ')}`)\n\t}\n\n\tif (descriptor.limit !== undefined) {\n\t\tparts.push(`LIMIT ${descriptor.limit}`)\n\t}\n\n\tif (descriptor.offset !== undefined) {\n\t\tparts.push(`OFFSET ${descriptor.offset}`)\n\t}\n\n\treturn { sql: parts.join(' '), params }\n}\n\n/**\n * Build a COUNT query from a QueryDescriptor.\n * Automatically adds `WHERE _deleted = 0`.\n *\n * @param descriptor - The query descriptor\n * @param fields - The field descriptors from the collection schema\n * @returns A parameterized SQL query that returns { count: number }\n */\nexport function buildCountQuery(\n\tdescriptor: QueryDescriptor,\n\tfields: Record<string, FieldDescriptor>,\n): SqlQuery {\n\tconst params: unknown[] = []\n\tconst parts = [`SELECT COUNT(*) as count FROM ${descriptor.collection}`]\n\n\tconst whereClause = buildWhereClauseParts(descriptor.where, fields, params)\n\tconst deletedFilter = '_deleted = 0'\n\tif (whereClause) {\n\t\tparts.push(`WHERE ${deletedFilter} AND ${whereClause}`)\n\t} else {\n\t\tparts.push(`WHERE ${deletedFilter}`)\n\t}\n\n\treturn { sql: parts.join(' '), params }\n}\n\n/**\n * Build an INSERT query for a collection record.\n *\n * @param collection - The collection name\n * @param record - The record data (already serialized with id, _created_at, _updated_at)\n * @returns A parameterized SQL query\n */\nexport function buildInsertQuery(collection: string, record: Record<string, unknown>): SqlQuery {\n\tconst columns = Object.keys(record)\n\tconst placeholders = columns.map(() => '?')\n\tconst params = Object.values(record)\n\n\tconst sql = `INSERT INTO ${collection} (${columns.join(', ')}) VALUES (${placeholders.join(', ')})`\n\treturn { sql, params }\n}\n\n/**\n * Build an UPDATE query for a collection record.\n *\n * @param collection - The collection name\n * @param id - The record ID\n * @param changes - The fields to update (already serialized)\n * @returns A parameterized SQL query\n */\nexport function buildUpdateQuery(\n\tcollection: string,\n\tid: string,\n\tchanges: Record<string, unknown>,\n): SqlQuery {\n\tconst setClauses = Object.keys(changes).map((col) => `${col} = ?`)\n\tconst params = [...Object.values(changes), id]\n\n\tconst sql = `UPDATE ${collection} SET ${setClauses.join(', ')} WHERE id = ?`\n\treturn { sql, params }\n}\n\n/**\n * Build a soft-delete query (SET _deleted = 1).\n *\n * @param collection - The collection name\n * @param id - The record ID\n * @param updatedAt - The timestamp to set on _updated_at\n * @returns A parameterized SQL query\n */\nexport function buildSoftDeleteQuery(collection: string, id: string, updatedAt: number): SqlQuery {\n\treturn {\n\t\tsql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ? WHERE id = ?`,\n\t\tparams: [updatedAt, id],\n\t}\n}\n\n/**\n * Build a WHERE clause from conditions, validating field names against the schema.\n *\n * @param where - The where conditions\n * @param fields - The field descriptors from the collection schema\n * @returns The SQL WHERE clause string and params, or null if no conditions\n */\nexport function buildWhereClause(\n\twhere: Record<string, unknown>,\n\tfields: Record<string, FieldDescriptor>,\n): SqlQuery | null {\n\tconst params: unknown[] = []\n\tconst result = buildWhereClauseParts(where, fields, params)\n\tif (!result) return null\n\treturn { sql: result, params }\n}\n\n// --- Internal helpers ---\n\nconst VALID_OPERATORS = new Set(['$eq', '$ne', '$gt', '$gte', '$lt', '$lte', '$in'])\n\nfunction buildWhereClauseParts(\n\twhere: Record<string, unknown>,\n\tfields: Record<string, FieldDescriptor>,\n\tparams: unknown[],\n): string | null {\n\tconst conditions: string[] = []\n\n\tfor (const [fieldName, value] of Object.entries(where)) {\n\t\tvalidateFieldName(fieldName, fields)\n\t\tconst descriptor = fields[fieldName]\n\n\t\tif (value !== null && typeof value === 'object' && !Array.isArray(value)) {\n\t\t\t// Operator object: { $gt: 5, $lt: 10 }\n\t\t\tconst ops = value as WhereOperators\n\t\t\tfor (const [op, opValue] of Object.entries(ops)) {\n\t\t\t\tif (!VALID_OPERATORS.has(op)) {\n\t\t\t\t\tthrow new QueryError(`Unknown operator \"${op}\" on field \"${fieldName}\"`, {\n\t\t\t\t\t\tfield: fieldName,\n\t\t\t\t\t\toperator: op,\n\t\t\t\t\t\tvalidOperators: [...VALID_OPERATORS],\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tconditions.push(buildOperatorCondition(fieldName, op, opValue, descriptor, params))\n\t\t\t}\n\t\t} else {\n\t\t\t// Shorthand: { completed: false } means { completed: { $eq: false } }\n\t\t\tconditions.push(buildOperatorCondition(fieldName, '$eq', value, descriptor, params))\n\t\t}\n\t}\n\n\tif (conditions.length === 0) return null\n\treturn conditions.join(' AND ')\n}\n\nfunction buildOperatorCondition(\n\tfieldName: string,\n\toperator: string,\n\tvalue: unknown,\n\tdescriptor: FieldDescriptor | undefined,\n\tparams: unknown[],\n): string {\n\t// Serialize boolean values to 0/1 for SQL comparison\n\tconst sqlValue =\n\t\tdescriptor?.kind === 'boolean' && typeof value === 'boolean' ? (value ? 1 : 0) : value\n\n\tswitch (operator) {\n\t\tcase '$eq':\n\t\t\tif (sqlValue === null) {\n\t\t\t\treturn `${fieldName} IS NULL`\n\t\t\t}\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} = ?`\n\t\tcase '$ne':\n\t\t\tif (sqlValue === null) {\n\t\t\t\treturn `${fieldName} IS NOT NULL`\n\t\t\t}\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} != ?`\n\t\tcase '$gt':\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} > ?`\n\t\tcase '$gte':\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} >= ?`\n\t\tcase '$lt':\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} < ?`\n\t\tcase '$lte':\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} <= ?`\n\t\tcase '$in': {\n\t\t\tif (!Array.isArray(sqlValue)) {\n\t\t\t\tthrow new QueryError(`$in operator requires an array value for field \"${fieldName}\"`, {\n\t\t\t\t\tfield: fieldName,\n\t\t\t\t\treceived: typeof sqlValue,\n\t\t\t\t})\n\t\t\t}\n\t\t\tconst placeholders = sqlValue.map(() => '?')\n\t\t\tfor (const item of sqlValue) {\n\t\t\t\tparams.push(\n\t\t\t\t\tdescriptor?.kind === 'boolean' && typeof item === 'boolean' ? (item ? 1 : 0) : item,\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn `${fieldName} IN (${placeholders.join(', ')})`\n\t\t}\n\t\tdefault:\n\t\t\tthrow new QueryError(`Unknown operator \"${operator}\"`, { operator })\n\t}\n}\n\nfunction validateFieldName(fieldName: string, fields: Record<string, FieldDescriptor>): void {\n\t// Allow schema fields plus metadata fields that map to query-able columns\n\tconst allowedFields = new Set([\n\t\t...Object.keys(fields),\n\t\t'id',\n\t\t'createdAt',\n\t\t'updatedAt',\n\t\t'_created_at',\n\t\t'_updated_at',\n\t])\n\tif (!allowedFields.has(fieldName)) {\n\t\tthrow new QueryError(\n\t\t\t`Unknown field \"${fieldName}\" in query. Available fields: ${[...allowedFields].join(', ')}`,\n\t\t\t{ field: fieldName },\n\t\t)\n\t}\n}\n","import { HybridLogicalClock } from '@korajs/core'\nimport type { CollectionDefinition, FieldDescriptor, Operation } from '@korajs/core'\nimport type { CollectionRecord, OperationRow, RawCollectionRow } from '../types'\nimport { decodeRichtext, encodeRichtext } from './richtext-serializer'\n\n/**\n * Serialize a JS record to SQL-compatible values for INSERT/UPDATE.\n * Transforms: boolean → 0/1, array → JSON string, richtext → Yjs binary update.\n *\n * @param data - The record data with JS-native types\n * @param fields - The field descriptors from the schema\n * @returns An object with SQL-compatible values\n */\nexport function serializeRecord(\n\tdata: Record<string, unknown>,\n\tfields: Record<string, FieldDescriptor>,\n): Record<string, unknown> {\n\tconst result: Record<string, unknown> = {}\n\tfor (const [key, value] of Object.entries(data)) {\n\t\tconst descriptor = fields[key]\n\t\tif (!descriptor) {\n\t\t\tresult[key] = value\n\t\t\tcontinue\n\t\t}\n\t\tresult[key] = serializeValue(value, descriptor)\n\t}\n\treturn result\n}\n\n/**\n * Deserialize a SQL row to JS-native types for the application layer.\n * Transforms: 0/1 → boolean, JSON string → array, strips _deleted, maps _created_at/_updated_at.\n *\n * @param row - The raw SQL row\n * @param fields - The field descriptors from the schema\n * @returns A CollectionRecord with JS-native types\n */\nexport function deserializeRecord(\n\trow: RawCollectionRow,\n\tfields: Record<string, FieldDescriptor>,\n): CollectionRecord {\n\tconst result: CollectionRecord = {\n\t\tid: row.id,\n\t\tcreatedAt: row._created_at,\n\t\tupdatedAt: row._updated_at,\n\t}\n\n\tfor (const [key, descriptor] of Object.entries(fields)) {\n\t\tconst rawValue = row[key]\n\t\tif (rawValue === undefined || rawValue === null) {\n\t\t\tresult[key] = rawValue ?? null\n\t\t\tcontinue\n\t\t}\n\t\tresult[key] = deserializeValue(rawValue, descriptor)\n\t}\n\n\treturn result\n}\n\n/**\n * Serialize an Operation to a row for the operations log table.\n *\n * @param op - The operation to serialize\n * @returns An OperationRow suitable for SQL INSERT\n */\nexport function serializeOperation(op: Operation): OperationRow {\n\treturn {\n\t\tid: op.id,\n\t\tnode_id: op.nodeId,\n\t\ttype: op.type,\n\t\trecord_id: op.recordId,\n\t\tdata: op.data ? JSON.stringify(op.data) : null,\n\t\tprevious_data: op.previousData ? JSON.stringify(op.previousData) : null,\n\t\ttimestamp: HybridLogicalClock.serialize(op.timestamp),\n\t\tsequence_number: op.sequenceNumber,\n\t\tcausal_deps: JSON.stringify(op.causalDeps),\n\t\tschema_version: op.schemaVersion,\n\t}\n}\n\n/**\n * Deserialize a row from the operations log table back to an Operation.\n *\n * @param row - The raw operation row from SQL\n * @returns The deserialized Operation object\n */\nexport function deserializeOperation(row: OperationRow): Operation {\n\treturn {\n\t\tid: row.id,\n\t\tnodeId: row.node_id,\n\t\ttype: row.type as Operation['type'],\n\t\tcollection: '', // Collection name is derived from the table name by the caller\n\t\trecordId: row.record_id,\n\t\tdata: row.data ? (JSON.parse(row.data) as Record<string, unknown>) : null,\n\t\tpreviousData: row.previous_data\n\t\t\t? (JSON.parse(row.previous_data) as Record<string, unknown>)\n\t\t\t: null,\n\t\ttimestamp: HybridLogicalClock.deserialize(row.timestamp),\n\t\tsequenceNumber: row.sequence_number,\n\t\tcausalDeps: JSON.parse(row.causal_deps) as string[],\n\t\tschemaVersion: row.schema_version,\n\t}\n}\n\n/**\n * Deserialize an operation row with collection name already known.\n */\nexport function deserializeOperationWithCollection(\n\trow: OperationRow,\n\tcollection: string,\n): Operation {\n\tconst op = deserializeOperation(row)\n\treturn { ...op, collection }\n}\n\nfunction serializeValue(value: unknown, descriptor: FieldDescriptor): unknown {\n\tif (value === null || value === undefined) {\n\t\treturn null\n\t}\n\n\tswitch (descriptor.kind) {\n\t\tcase 'boolean':\n\t\t\treturn value ? 1 : 0\n\t\tcase 'array':\n\t\t\treturn JSON.stringify(value)\n\t\tcase 'richtext':\n\t\t\treturn encodeRichtext(value as string | Uint8Array | ArrayBuffer)\n\t\tdefault:\n\t\t\treturn value\n\t}\n}\n\nfunction deserializeValue(value: unknown, descriptor: FieldDescriptor): unknown {\n\tswitch (descriptor.kind) {\n\t\tcase 'boolean':\n\t\t\treturn value === 1 || value === true\n\t\tcase 'array':\n\t\t\tif (typeof value === 'string') {\n\t\t\t\treturn JSON.parse(value) as unknown[]\n\t\t\t}\n\t\t\treturn value\n\t\tcase 'richtext':\n\t\t\treturn decodeRichtext(value)\n\t\tdefault:\n\t\t\treturn value\n\t}\n}\n","import * as Y from 'yjs'\n\nconst TEXT_KEY = 'content'\n\nexport type RichtextInput = string | Uint8Array | ArrayBuffer | null | undefined\n\n/**\n * Encodes richtext values into Yjs document updates.\n */\nexport function encodeRichtext(value: RichtextInput): Uint8Array | null {\n\tif (value === null || value === undefined) {\n\t\treturn null\n\t}\n\n\tif (typeof value === 'string') {\n\t\tconst doc = new Y.Doc()\n\t\tdoc.getText(TEXT_KEY).insert(0, value)\n\t\treturn Y.encodeStateAsUpdate(doc)\n\t}\n\n\tif (value instanceof Uint8Array) {\n\t\treturn value\n\t}\n\n\tif (value instanceof ArrayBuffer) {\n\t\treturn new Uint8Array(value)\n\t}\n\n\tthrow new Error('Richtext value must be a string, Uint8Array, ArrayBuffer, null, or undefined.')\n}\n\n/**\n * Decodes driver-provided richtext values into Uint8Array.\n */\nexport function decodeRichtext(value: unknown): Uint8Array | null {\n\tif (value === null || value === undefined) {\n\t\treturn null\n\t}\n\n\tif (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {\n\t\treturn new Uint8Array(value)\n\t}\n\n\tif (value instanceof Uint8Array) {\n\t\treturn value\n\t}\n\n\tif (value instanceof ArrayBuffer) {\n\t\treturn new Uint8Array(value)\n\t}\n\n\tthrow new Error('Richtext storage value must be Uint8Array, ArrayBuffer, Buffer, null, or undefined.')\n}\n\n/**\n * Reads plain text from a richtext Yjs state update.\n */\nexport function richtextToPlainText(value: RichtextInput): string {\n\tconst encoded = encodeRichtext(value)\n\tif (!encoded) return ''\n\n\tconst doc = new Y.Doc()\n\tY.applyUpdate(doc, encoded)\n\treturn doc.getText(TEXT_KEY).toString()\n}\n","/**\n * Minimal pluralization/singularization utilities for relation name resolution.\n * Handles common English patterns — not meant to be exhaustive.\n */\n\nfunction isVowel(char: string | undefined): boolean {\n\tif (!char) return false\n\treturn 'aeiouAEIOU'.includes(char)\n}\n\n/**\n * Pluralize a word using common English rules.\n *\n * @example\n * ```\n * pluralize('project') // 'projects'\n * pluralize('category') // 'categories'\n * pluralize('match') // 'matches'\n * ```\n */\nexport function pluralize(word: string): string {\n\tif (word.endsWith('s')) return word\n\tif (word.endsWith('y') && !isVowel(word[word.length - 2])) {\n\t\treturn `${word.slice(0, -1)}ies`\n\t}\n\tif (word.endsWith('sh') || word.endsWith('ch') || word.endsWith('x') || word.endsWith('z')) {\n\t\treturn `${word}es`\n\t}\n\treturn `${word}s`\n}\n\n/**\n * Singularize a word using common English rules.\n *\n * @example\n * ```\n * singularize('projects') // 'project'\n * singularize('categories') // 'category'\n * singularize('matches') // 'match'\n * ```\n */\nexport function singularize(word: string): string {\n\tif (word.endsWith('ies') && !isVowel(word[word.length - 4])) {\n\t\treturn `${word.slice(0, -3)}y`\n\t}\n\tif (\n\t\tword.endsWith('shes') ||\n\t\tword.endsWith('ches') ||\n\t\tword.endsWith('xes') ||\n\t\tword.endsWith('zes')\n\t) {\n\t\treturn word.slice(0, -2)\n\t}\n\tif (word.endsWith('ses')) {\n\t\treturn word.slice(0, -2)\n\t}\n\tif (word.endsWith('s') && !word.endsWith('ss')) {\n\t\treturn word.slice(0, -1)\n\t}\n\treturn word\n}\n","import type { CollectionDefinition, SchemaDefinition } from '@korajs/core'\nimport { deserializeRecord } from '../serialization/serializer'\nimport type { SubscriptionManager } from '../subscription/subscription-manager'\nimport type {\n\tCollectionRecord,\n\tOrderByDirection,\n\tQueryDescriptor,\n\tRawCollectionRow,\n\tStorageAdapter,\n\tSubscriptionCallback,\n\tWhereClause,\n} from '../types'\nimport { pluralize, singularize } from './pluralize'\nimport { buildCountQuery, buildSelectQuery } from './sql-builder'\n\n/**\n * Fluent query builder for constructing and executing collection queries.\n * Supports where, orderBy, limit, offset, include, exec, count, and subscribe.\n *\n * The generic parameter `T` defaults to `CollectionRecord` for backward compatibility\n * but can be narrowed to a specific record type for full type inference.\n *\n * @example\n * ```typescript\n * const todos = await app.todos\n * .where({ completed: false })\n * .orderBy('createdAt', 'desc')\n * .limit(10)\n * .exec()\n * ```\n */\nexport class QueryBuilder<T = CollectionRecord> {\n\tprivate descriptor: QueryDescriptor\n\n\tconstructor(\n\t\tprivate readonly collectionName: string,\n\t\tprivate readonly definition: CollectionDefinition,\n\t\tprivate readonly adapter: StorageAdapter,\n\t\tprivate readonly subscriptionManager: SubscriptionManager,\n\t\tinitialWhere: WhereClause = {},\n\t\tprivate readonly schema?: SchemaDefinition,\n\t) {\n\t\tthis.descriptor = {\n\t\t\tcollection: collectionName,\n\t\t\twhere: { ...initialWhere },\n\t\t\torderBy: [],\n\t\t}\n\t}\n\n\t/**\n\t * Add WHERE conditions (AND semantics, merged with existing conditions).\n\t */\n\twhere(conditions: WhereClause): QueryBuilder<T> {\n\t\tconst clone = this.clone()\n\t\tclone.descriptor = {\n\t\t\t...clone.descriptor,\n\t\t\twhere: { ...clone.descriptor.where, ...conditions },\n\t\t}\n\t\treturn clone\n\t}\n\n\t/**\n\t * Add ORDER BY clause.\n\t */\n\torderBy(field: string, direction: OrderByDirection = 'asc'): QueryBuilder<T> {\n\t\tconst clone = this.clone()\n\t\tclone.descriptor = {\n\t\t\t...clone.descriptor,\n\t\t\torderBy: [...clone.descriptor.orderBy, { field, direction }],\n\t\t}\n\t\treturn clone\n\t}\n\n\t/**\n\t * Set result limit.\n\t */\n\tlimit(n: number): QueryBuilder<T> {\n\t\tconst clone = this.clone()\n\t\tclone.descriptor = { ...clone.descriptor, limit: n }\n\t\treturn clone\n\t}\n\n\t/**\n\t * Set result offset.\n\t */\n\toffset(n: number): QueryBuilder<T> {\n\t\tconst clone = this.clone()\n\t\tclone.descriptor = { ...clone.descriptor, offset: n }\n\t\treturn clone\n\t}\n\n\t/**\n\t * Include related records in the query results.\n\t * Follows relations defined in the schema to batch-fetch related data.\n\t *\n\t * @param targets - Relation target names (collection names or relation names)\n\t * @returns A new QueryBuilder with include targets added\n\t *\n\t * @example\n\t * ```typescript\n\t * const todosWithProject = await app.todos\n\t * .where({ completed: false })\n\t * .include('project')\n\t * .exec()\n\t * ```\n\t */\n\tinclude(...targets: string[]): QueryBuilder<T> {\n\t\tconst clone = this.clone()\n\t\tconst existing = clone.descriptor.include ?? []\n\t\tclone.descriptor = {\n\t\t\t...clone.descriptor,\n\t\t\tinclude: [...existing, ...targets],\n\t\t}\n\t\treturn clone\n\t}\n\n\t/**\n\t * Execute the query and return results.\n\t */\n\tasync exec(): Promise<T[]> {\n\t\tconst { sql, params } = buildSelectQuery(this.descriptor, this.definition.fields)\n\t\tconst rows = await this.adapter.query<RawCollectionRow>(sql, params)\n\t\tconst records = rows.map((row) => deserializeRecord(row, this.definition.fields))\n\n\t\t// Resolve includes if any\n\t\tif (this.descriptor.include && this.descriptor.include.length > 0 && this.schema) {\n\t\t\tawait this.resolveIncludes(records)\n\t\t}\n\n\t\treturn records as T[]\n\t}\n\n\t/**\n\t * Execute a COUNT query and return the count.\n\t */\n\tasync count(): Promise<number> {\n\t\tconst { sql, params } = buildCountQuery(this.descriptor, this.definition.fields)\n\t\tconst rows = await this.adapter.query<{ count: number }>(sql, params)\n\t\treturn rows[0]?.count ?? 0\n\t}\n\n\t/**\n\t * Subscribe to query results. Callback is called immediately with current results,\n\t * then again whenever the results change due to mutations.\n\t *\n\t * @returns An unsubscribe function\n\t */\n\tsubscribe(callback: SubscriptionCallback<T>): () => void {\n\t\tconst executeFn = () => this.exec()\n\n\t\t// Resolve includeCollections for subscription tracking\n\t\tconst descriptorCopy = { ...this.descriptor }\n\t\tif (descriptorCopy.include && descriptorCopy.include.length > 0 && this.schema) {\n\t\t\tdescriptorCopy.includeCollections = this.resolveIncludeCollections(descriptorCopy.include)\n\t\t}\n\n\t\t// Use registerAndFetch to execute immediately, set lastResults,\n\t\t// and call callback — ensuring subsequent flushes diff correctly.\n\t\treturn this.subscriptionManager.registerAndFetch(\n\t\t\tdescriptorCopy,\n\t\t\tcallback as SubscriptionCallback<CollectionRecord>,\n\t\t\texecuteFn as () => Promise<CollectionRecord[]>,\n\t\t)\n\t}\n\n\t/** Get the internal descriptor (for testing/debugging) */\n\tgetDescriptor(): QueryDescriptor {\n\t\treturn { ...this.descriptor }\n\t}\n\n\tprivate clone(): QueryBuilder<T> {\n\t\tconst qb = new QueryBuilder<T>(\n\t\t\tthis.collectionName,\n\t\t\tthis.definition,\n\t\t\tthis.adapter,\n\t\t\tthis.subscriptionManager,\n\t\t\t{},\n\t\t\tthis.schema,\n\t\t)\n\t\tqb.descriptor = {\n\t\t\t...this.descriptor,\n\t\t\twhere: { ...this.descriptor.where },\n\t\t\torderBy: [...this.descriptor.orderBy],\n\t\t\tinclude: this.descriptor.include ? [...this.descriptor.include] : undefined,\n\t\t\tincludeCollections: this.descriptor.includeCollections\n\t\t\t\t? [...this.descriptor.includeCollections]\n\t\t\t\t: undefined,\n\t\t}\n\t\treturn qb\n\t}\n\n\t/**\n\t * Resolve include targets to their actual collection names for subscription tracking.\n\t */\n\tprivate resolveIncludeCollections(targets: string[]): string[] {\n\t\tif (!this.schema) return []\n\t\tconst collections: string[] = []\n\n\t\tfor (const target of targets) {\n\t\t\tconst relation = this.findRelation(target)\n\t\t\tif (relation) {\n\t\t\t\t// For many-to-one: the related collection is `relation.to`\n\t\t\t\t// For one-to-many: the related collection is `relation.from`\n\t\t\t\tif (relation.from === this.collectionName) {\n\t\t\t\t\tcollections.push(relation.to)\n\t\t\t\t} else {\n\t\t\t\t\tcollections.push(relation.from)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn collections\n\t}\n\n\t/**\n\t * Resolve includes after primary query, batch-fetching related records.\n\t */\n\tprivate async resolveIncludes(records: CollectionRecord[]): Promise<void> {\n\t\tif (!this.schema || !this.descriptor.include || records.length === 0) return\n\n\t\tfor (const target of this.descriptor.include) {\n\t\t\tconst relation = this.findRelation(target)\n\t\t\tif (!relation) {\n\t\t\t\tthrow new QueryError(\n\t\t\t\t\t`No relation found for include target \"${target}\" on collection \"${this.collectionName}\". ` +\n\t\t\t\t\t\t`Check that a relation is defined in your schema that connects \"${this.collectionName}\" to \"${target}\".`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tif (relation.from === this.collectionName) {\n\t\t\t\t// Many-to-one or one-to-one: primary has FK → fetch parent records\n\t\t\t\tawait this.resolveManyToOneInclude(records, relation, target)\n\t\t\t} else {\n\t\t\t\t// One-to-many: related collection has FK → fetch children\n\t\t\t\tawait this.resolveOneToManyInclude(records, relation, target)\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Many-to-one: collect FK values from primary results, batch-fetch related, attach as singular.\n\t */\n\tprivate async resolveManyToOneInclude(\n\t\trecords: CollectionRecord[],\n\t\trelation: { from: string; to: string; field: string },\n\t\ttarget: string,\n\t): Promise<void> {\n\t\tconst fkField = relation.field\n\t\tconst fkValues = records\n\t\t\t.map((r) => r[fkField])\n\t\t\t.filter((v): v is string => v !== null && v !== undefined && typeof v === 'string')\n\n\t\tif (fkValues.length === 0) {\n\t\t\t// All null FKs — set property to null on all records\n\t\t\tconst propName = singularize(target)\n\t\t\tfor (const record of records) {\n\t\t\t\t;(record as Record<string, unknown>)[propName] = null\n\t\t\t}\n\t\t\treturn\n\t\t}\n\n\t\tconst uniqueFks = [...new Set(fkValues)]\n\t\tconst relatedCollection = relation.to\n\t\tconst relatedDef = this.schema?.collections[relatedCollection]\n\t\tif (!relatedDef) return\n\n\t\t// Batch fetch: SELECT * FROM <to> WHERE id IN (...) AND _deleted = 0\n\t\tconst placeholders = uniqueFks.map(() => '?').join(', ')\n\t\tconst sql = `SELECT * FROM ${relatedCollection} WHERE id IN (${placeholders}) AND _deleted = 0`\n\t\tconst rows = await this.adapter.query<RawCollectionRow>(sql, uniqueFks)\n\t\tconst relatedRecords = rows.map((row) => deserializeRecord(row, relatedDef.fields))\n\n\t\t// Build lookup\n\t\tconst lookup = new Map<string, CollectionRecord>()\n\t\tfor (const r of relatedRecords) {\n\t\t\tlookup.set(r.id, r)\n\t\t}\n\n\t\t// Attach as singular property\n\t\tconst propName = singularize(target)\n\t\tfor (const record of records) {\n\t\t\tconst fk = record[fkField] as string | null\n\t\t\t;(record as Record<string, unknown>)[propName] = fk ? (lookup.get(fk) ?? null) : null\n\t\t}\n\t}\n\n\t/**\n\t * One-to-many: collect primary IDs, batch-fetch children, attach as array.\n\t */\n\tprivate async resolveOneToManyInclude(\n\t\trecords: CollectionRecord[],\n\t\trelation: { from: string; to: string; field: string },\n\t\ttarget: string,\n\t): Promise<void> {\n\t\tconst primaryIds = records.map((r) => r.id)\n\t\tconst relatedCollection = relation.from\n\t\tconst relatedDef = this.schema?.collections[relatedCollection]\n\t\tif (!relatedDef) return\n\n\t\tconst fkField = relation.field\n\t\tconst placeholders = primaryIds.map(() => '?').join(', ')\n\t\tconst sql = `SELECT * FROM ${relatedCollection} WHERE ${fkField} IN (${placeholders}) AND _deleted = 0`\n\t\tconst rows = await this.adapter.query<RawCollectionRow>(sql, primaryIds)\n\t\tconst relatedRecords = rows.map((row) => deserializeRecord(row, relatedDef.fields))\n\n\t\t// Group by FK\n\t\tconst grouped = new Map<string, CollectionRecord[]>()\n\t\tfor (const r of relatedRecords) {\n\t\t\tconst fk = r[fkField] as string\n\t\t\tif (!grouped.has(fk)) {\n\t\t\t\tgrouped.set(fk, [])\n\t\t\t}\n\t\t\tgrouped.get(fk)?.push(r)\n\t\t}\n\n\t\t// Attach as array property\n\t\tconst propName = pluralize(target)\n\t\tfor (const record of records) {\n\t\t\t;(record as Record<string, unknown>)[propName] = grouped.get(record.id) ?? []\n\t\t}\n\t}\n\n\t/**\n\t * Find a relation definition matching the include target.\n\t * Searches by relation name, target collection name, and singularized/pluralized variants.\n\t */\n\tprivate findRelation(\n\t\ttarget: string,\n\t): { from: string; to: string; field: string; type: string } | null {\n\t\tif (!this.schema) return null\n\n\t\tfor (const [_name, rel] of Object.entries(this.schema.relations)) {\n\t\t\t// Direct match: target is the related collection name\n\t\t\tif (rel.from === this.collectionName && rel.to === target) return rel\n\t\t\tif (rel.to === this.collectionName && rel.from === target) return rel\n\n\t\t\t// Singularized match: \"project\" matches relation to \"projects\"\n\t\t\tif (rel.from === this.collectionName && rel.to === pluralize(target)) return rel\n\t\t\tif (rel.to === this.collectionName && rel.from === pluralize(target)) return rel\n\n\t\t\t// Pluralized match: \"todos\" matches relation from \"todos\"\n\t\t\tif (rel.from === this.collectionName && rel.to === singularize(target)) return rel\n\t\t\tif (rel.to === this.collectionName && rel.from === singularize(target)) return rel\n\t\t}\n\n\t\treturn null\n\t}\n}\n\n/**\n * Error thrown when a query encounters an invalid state.\n */\nclass QueryError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message)\n\t\tthis.name = 'QueryError'\n\t}\n}\n","import type { Operation } from '@korajs/core'\nimport type {\n\tCollectionRecord,\n\tQueryDescriptor,\n\tSubscription,\n\tSubscriptionCallback,\n} from '../types'\n\nlet nextSubId = 0\n\n/**\n * Manages reactive subscriptions. When a mutation occurs on a collection,\n * affected subscriptions are re-evaluated in a microtask batch and callbacks\n * are invoked only if results actually changed.\n */\nexport class SubscriptionManager {\n\tprivate subscriptions = new Map<string, Subscription>()\n\tprivate pendingCollections = new Set<string>()\n\tprivate flushScheduled = false\n\n\t/**\n\t * Register a new subscription.\n\t *\n\t * @param descriptor - The query descriptor defining what this subscription watches\n\t * @param callback - Called with results whenever they change\n\t * @param executeFn - Function to re-execute the query and get current results\n\t * @returns An unsubscribe function\n\t */\n\tregister(\n\t\tdescriptor: QueryDescriptor,\n\t\tcallback: SubscriptionCallback<CollectionRecord>,\n\t\texecuteFn: () => Promise<CollectionRecord[]>,\n\t): () => void {\n\t\tconst id = `sub_${++nextSubId}`\n\t\tconst subscription: Subscription = {\n\t\t\tid,\n\t\t\tdescriptor,\n\t\t\tcallback,\n\t\t\texecuteFn,\n\t\t\tlastResults: [],\n\t\t}\n\t\tthis.subscriptions.set(id, subscription)\n\n\t\treturn () => {\n\t\t\tthis.subscriptions.delete(id)\n\t\t}\n\t}\n\n\t/**\n\t * Register a subscription and immediately execute the query.\n\t * The initial results are stored as lastResults so subsequent flushes\n\t * correctly diff against the initial state.\n\t *\n\t * @returns An unsubscribe function\n\t */\n\tregisterAndFetch(\n\t\tdescriptor: QueryDescriptor,\n\t\tcallback: SubscriptionCallback<CollectionRecord>,\n\t\texecuteFn: () => Promise<CollectionRecord[]>,\n\t): () => void {\n\t\tconst id = `sub_${++nextSubId}`\n\t\tconst subscription: Subscription = {\n\t\t\tid,\n\t\t\tdescriptor,\n\t\t\tcallback,\n\t\t\texecuteFn,\n\t\t\tlastResults: [],\n\t\t}\n\t\tthis.subscriptions.set(id, subscription)\n\n\t\t// Execute immediately, set lastResults, and call callback\n\t\texecuteFn().then((results) => {\n\t\t\t// Guard: subscription may have been removed before the async fetch completes\n\t\t\tif (this.subscriptions.has(id)) {\n\t\t\t\tsubscription.lastResults = results\n\t\t\t\tcallback(results)\n\t\t\t}\n\t\t})\n\n\t\treturn () => {\n\t\t\tthis.subscriptions.delete(id)\n\t\t}\n\t}\n\n\t/**\n\t * Notify the manager that a mutation occurred on a collection.\n\t * Schedules a microtask flush to batch multiple mutations in the same tick.\n\t */\n\tnotify(collection: string, _operation: Operation): void {\n\t\tthis.pendingCollections.add(collection)\n\t\tthis.scheduleFlush()\n\t}\n\n\t/**\n\t * Immediately flush all pending notifications.\n\t * Useful for testing. In production, flushing happens via microtask.\n\t */\n\tasync flush(): Promise<void> {\n\t\tif (this.pendingCollections.size === 0) return\n\n\t\tconst collections = new Set(this.pendingCollections)\n\t\tthis.pendingCollections.clear()\n\t\tthis.flushScheduled = false\n\n\t\t// Find affected subscriptions (including those with related collection includes)\n\t\tconst affected: Subscription[] = []\n\t\tfor (const sub of this.subscriptions.values()) {\n\t\t\tif (collections.has(sub.descriptor.collection)) {\n\t\t\t\taffected.push(sub)\n\t\t\t} else if (sub.descriptor.includeCollections) {\n\t\t\t\t// Re-evaluate if a mutation affects an included (related) collection\n\t\t\t\tfor (const incCol of sub.descriptor.includeCollections) {\n\t\t\t\t\tif (collections.has(incCol)) {\n\t\t\t\t\t\taffected.push(sub)\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Re-execute and diff\n\t\tfor (const sub of affected) {\n\t\t\ttry {\n\t\t\t\tconst newResults = await sub.executeFn()\n\t\t\t\tif (!this.resultsEqual(sub.lastResults, newResults)) {\n\t\t\t\t\tsub.lastResults = newResults\n\t\t\t\t\tsub.callback(newResults)\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// Subscription re-execution failed — skip silently for now.\n\t\t\t\t// In future, we could emit an error event for DevTools.\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Remove all subscriptions. Called on store close.\n\t */\n\tclear(): void {\n\t\tthis.subscriptions.clear()\n\t\tthis.pendingCollections.clear()\n\t\tthis.flushScheduled = false\n\t}\n\n\t/** Number of active subscriptions (for testing/debugging) */\n\tget size(): number {\n\t\treturn this.subscriptions.size\n\t}\n\n\tprivate scheduleFlush(): void {\n\t\tif (this.flushScheduled) return\n\t\tthis.flushScheduled = true\n\t\tqueueMicrotask(() => {\n\t\t\tthis.flush()\n\t\t})\n\t}\n\n\t/**\n\t * Compare two result sets. Uses length check + JSON comparison as pragmatic approach.\n\t * Sufficient for typical query results. Can be optimized to id-based diffing if profiling shows need.\n\t */\n\tprivate resultsEqual(prev: CollectionRecord[], next: CollectionRecord[]): boolean {\n\t\tif (prev.length !== next.length) return false\n\t\t// Fast path: both empty\n\t\tif (prev.length === 0) return true\n\t\treturn JSON.stringify(prev) === JSON.stringify(next)\n\t}\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAA0B;AAKnB,IAAM,aAAN,cAAyB,sBAAU;AAAA,EACzC,YAAY,SAAiB,SAAmC;AAC/D,UAAM,SAAS,eAAe,O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e;AAAA,IAC5C;AACA,WAAO;AAAA,EACR;AAAA,EAEQ,aAAmB;AAC1B,QAAI,CAAC,KAAK,QAAQ;AACjB,YAAM,IAAI,kBAAkB;AAAA,IAC7B;AAAA,EACD;AACD;","names":["import_core","import_core","import_core","QueryError","propName"]}
|
|
1
|
+
{"version":3,"sources":["../src/serialization/richtext-serializer.ts","../src/serialization/serializer.ts","../src/backup/backup.ts","../src/audit/audit-json.ts","../src/audit/audit-trace-store.ts","../src/audit/export-audit.ts","../src/index.ts","../src/errors.ts","../src/compaction/types.ts","../src/compaction/compact-operation-log.ts","../src/sync/sync-state.ts","../src/store/store.ts","../src/mutations/execute-delete.ts","../src/lww/row-version.ts","../src/query/sql-builder.ts","../src/store/sequence-allocator.ts","../src/mutations/resolve-causal-deps.ts","../src/mutations/execute-insert.ts","../src/mutations/execute-update.ts","../src/state-machine/state-validator.ts","../src/collection/collection.ts","../src/query/query-builder.ts","../src/query/pluralize.ts","../src/relations/relation-enforcer.ts","../src/relations/relation-lookup.ts","../src/replay/replay-to.ts","../src/sequences/sequence-manager.ts","../src/subscription/bloom-filter.ts","../src/subscription/subscription-manager.ts","../src/transaction/transaction-context.ts","../src/transaction/transaction-sequence.ts","../src/store/tab-node-id.ts","../src/backup/index.ts","../src/audit/index.ts"],"sourcesContent":["import * as Y from 'yjs'\n\nconst TEXT_KEY = 'content'\n\nexport type RichtextInput = string | Uint8Array | ArrayBuffer | null | undefined\n\n/**\n * Encodes richtext values into Yjs document updates.\n */\nexport function encodeRichtext(value: RichtextInput): Uint8Array | null {\n\tif (value === null || value === undefined) {\n\t\treturn null\n\t}\n\n\tif (typeof value === 'string') {\n\t\tconst doc = new Y.Doc()\n\t\tdoc.getText(TEXT_KEY).insert(0, value)\n\t\treturn Y.encodeStateAsUpdate(doc)\n\t}\n\n\tif (value instanceof Uint8Array) {\n\t\treturn value\n\t}\n\n\tif (value instanceof ArrayBuffer) {\n\t\treturn new Uint8Array(value)\n\t}\n\n\tthrow new Error('Richtext value must be a string, Uint8Array, ArrayBuffer, null, or undefined.')\n}\n\n/**\n * Decodes driver-provided richtext values into Uint8Array.\n */\nexport function decodeRichtext(value: unknown): Uint8Array | null {\n\tif (value === null || value === undefined) {\n\t\treturn null\n\t}\n\n\tif (typeof Buffer !== 'undefined' && Buffer.isBuffer(value)) {\n\t\treturn new Uint8Array(value)\n\t}\n\n\tif (value instanceof Uint8Array) {\n\t\treturn value\n\t}\n\n\tif (value instanceof ArrayBuffer) {\n\t\treturn new Uint8Array(value)\n\t}\n\n\tthrow new Error(\n\t\t'Richtext storage value must be Uint8Array, ArrayBuffer, Buffer, null, or undefined.',\n\t)\n}\n\n/**\n * Reads plain text from a richtext Yjs state update.\n */\nexport function richtextToPlainText(value: RichtextInput): string {\n\tconst encoded = encodeRichtext(value)\n\tif (!encoded) return ''\n\n\tconst doc = new Y.Doc()\n\tY.applyUpdate(doc, encoded)\n\treturn doc.getText(TEXT_KEY).toString()\n}\n\n/**\n * Compares two richtext values by decoded plain text.\n * Yjs update bytes are not stable across separate encodes of the same string.\n */\nexport function richtextStatesEqual(a: unknown, b: unknown): boolean {\n\ttry {\n\t\treturn richtextToPlainText(a as RichtextInput) === richtextToPlainText(b as RichtextInput)\n\t} catch {\n\t\treturn false\n\t}\n}\n","import { HybridLogicalClock } from '@korajs/core'\nimport type { CollectionDefinition, FieldDescriptor, Operation } from '@korajs/core'\nimport type { CollectionRecord, OperationRow, RawCollectionRow } from '../types'\nimport { decodeRichtext, encodeRichtext } from './richtext-serializer'\n\n/**\n * Serialize a JS record to SQL-compatible values for INSERT/UPDATE.\n * Transforms: boolean → 0/1, array → JSON string, richtext → Yjs binary update.\n *\n * @param data - The record data with JS-native types\n * @param fields - The field descriptors from the schema\n * @returns An object with SQL-compatible values\n */\nexport function serializeRecord(\n\tdata: Record<string, unknown>,\n\tfields: Record<string, FieldDescriptor>,\n): Record<string, unknown> {\n\tconst result: Record<string, unknown> = {}\n\tfor (const [key, value] of Object.entries(data)) {\n\t\tconst descriptor = fields[key]\n\t\tif (!descriptor) {\n\t\t\tresult[key] = value\n\t\t\tcontinue\n\t\t}\n\t\tresult[key] = serializeValue(value, descriptor)\n\t}\n\treturn result\n}\n\n/**\n * Deserialize a SQL row to JS-native types for the application layer.\n * Transforms: 0/1 → boolean, JSON string → array, strips _deleted, maps _created_at/_updated_at.\n *\n * @param row - The raw SQL row\n * @param fields - The field descriptors from the schema\n * @returns A CollectionRecord with JS-native types\n */\nexport function deserializeRecord(\n\trow: RawCollectionRow,\n\tfields: Record<string, FieldDescriptor>,\n): CollectionRecord {\n\tconst result: CollectionRecord = {\n\t\tid: row.id,\n\t\tcreatedAt: row._created_at,\n\t\tupdatedAt: row._updated_at,\n\t}\n\n\tfor (const [key, descriptor] of Object.entries(fields)) {\n\t\tconst rawValue = row[key]\n\t\tif (rawValue === undefined || rawValue === null) {\n\t\t\tresult[key] = rawValue ?? null\n\t\t\tcontinue\n\t\t}\n\t\tresult[key] = deserializeValue(rawValue, descriptor)\n\t}\n\n\treturn result\n}\n\n/**\n * Internal key used to embed atomicOps metadata in the data JSON column.\n * This avoids adding a new column to the ops table (no migration needed).\n */\nconst ATOMIC_OPS_KEY = '__kora_atomic_ops__'\n\n/**\n * Internal key used to embed transactionId in the data JSON column.\n */\nconst TX_ID_KEY = '__kora_tx_id__'\n\n/**\n * Internal key used to embed mutationName in the data JSON column.\n */\nconst MUTATION_NAME_KEY = '__kora_mutation__'\n\n/**\n * Serialize an Operation to a row for the operations log table.\n *\n * @param op - The operation to serialize\n * @returns An OperationRow suitable for SQL INSERT\n */\nexport function serializeOperation(op: Operation): OperationRow {\n\tconst hasMetadata = op.transactionId !== undefined || op.mutationName !== undefined\n\tlet dataPayload: Record<string, unknown> | null = null\n\tif (op.data) {\n\t\t// Embed metadata in the data JSON when present\n\t\tdataPayload = { ...op.data }\n\t\tif (op.atomicOps !== undefined && Object.keys(op.atomicOps).length > 0) {\n\t\t\tdataPayload[ATOMIC_OPS_KEY] = op.atomicOps\n\t\t}\n\t\tif (op.transactionId !== undefined) {\n\t\t\tdataPayload[TX_ID_KEY] = op.transactionId\n\t\t}\n\t\tif (op.mutationName !== undefined) {\n\t\t\tdataPayload[MUTATION_NAME_KEY] = op.mutationName\n\t\t}\n\t} else if (hasMetadata) {\n\t\t// For delete operations (data is null), we still need to store metadata\n\t\tdataPayload = {}\n\t\tif (op.transactionId !== undefined) {\n\t\t\tdataPayload[TX_ID_KEY] = op.transactionId\n\t\t}\n\t\tif (op.mutationName !== undefined) {\n\t\t\tdataPayload[MUTATION_NAME_KEY] = op.mutationName\n\t\t}\n\t}\n\n\treturn {\n\t\tid: op.id,\n\t\tnode_id: op.nodeId,\n\t\ttype: op.type,\n\t\trecord_id: op.recordId,\n\t\tdata: dataPayload ? JSON.stringify(dataPayload) : null,\n\t\tprevious_data: op.previousData ? JSON.stringify(op.previousData) : null,\n\t\ttimestamp: HybridLogicalClock.serialize(op.timestamp),\n\t\tsequence_number: op.sequenceNumber,\n\t\tcausal_deps: JSON.stringify(op.causalDeps),\n\t\tschema_version: op.schemaVersion,\n\t}\n}\n\n/**\n * Deserialize a row from the operations log table back to an Operation.\n *\n * @param row - The raw operation row from SQL\n * @returns The deserialized Operation object\n */\nexport function deserializeOperation(row: OperationRow): Operation {\n\tlet data: Record<string, unknown> | null = null\n\tlet atomicOps: Record<string, unknown> | undefined\n\tlet transactionId: string | undefined\n\tlet mutationName: string | undefined\n\n\tif (row.data) {\n\t\tconst parsed = JSON.parse(row.data) as Record<string, unknown>\n\t\t// Extract embedded metadata keys\n\t\tif (ATOMIC_OPS_KEY in parsed) {\n\t\t\tatomicOps = parsed[ATOMIC_OPS_KEY] as Record<string, unknown>\n\t\t}\n\t\tif (TX_ID_KEY in parsed) {\n\t\t\ttransactionId = parsed[TX_ID_KEY] as string\n\t\t}\n\t\tif (MUTATION_NAME_KEY in parsed) {\n\t\t\tmutationName = parsed[MUTATION_NAME_KEY] as string\n\t\t}\n\t\t// Remove metadata keys from data\n\t\tconst { [ATOMIC_OPS_KEY]: _a, [TX_ID_KEY]: _t, [MUTATION_NAME_KEY]: _m, ...rest } = parsed\n\t\tdata = Object.keys(rest).length > 0 ? rest : null\n\t}\n\n\treturn {\n\t\tid: row.id,\n\t\tnodeId: row.node_id,\n\t\ttype: row.type as Operation['type'],\n\t\tcollection: '', // Collection name is derived from the table name by the caller\n\t\trecordId: row.record_id,\n\t\tdata,\n\t\tpreviousData: row.previous_data\n\t\t\t? (JSON.parse(row.previous_data) as Record<string, unknown>)\n\t\t\t: null,\n\t\ttimestamp: HybridLogicalClock.deserialize(row.timestamp),\n\t\tsequenceNumber: row.sequence_number,\n\t\tcausalDeps: JSON.parse(row.causal_deps) as string[],\n\t\tschemaVersion: row.schema_version,\n\t\t...(atomicOps !== undefined ? { atomicOps: atomicOps as Operation['atomicOps'] } : {}),\n\t\t...(transactionId !== undefined ? { transactionId } : {}),\n\t\t...(mutationName !== undefined ? { mutationName } : {}),\n\t}\n}\n\n/**\n * Deserialize an operation row with collection name already known.\n */\nexport function deserializeOperationWithCollection(\n\trow: OperationRow,\n\tcollection: string,\n): Operation {\n\tconst op = deserializeOperation(row)\n\treturn { ...op, collection }\n}\n\nfunction serializeValue(value: unknown, descriptor: FieldDescriptor): unknown {\n\tif (value === null || value === undefined) {\n\t\treturn null\n\t}\n\n\tswitch (descriptor.kind) {\n\t\tcase 'boolean':\n\t\t\treturn value ? 1 : 0\n\t\tcase 'array':\n\t\t\treturn JSON.stringify(value)\n\t\tcase 'richtext':\n\t\t\treturn encodeRichtext(value as string | Uint8Array | ArrayBuffer)\n\t\tdefault:\n\t\t\treturn value\n\t}\n}\n\nfunction deserializeValue(value: unknown, descriptor: FieldDescriptor): unknown {\n\tswitch (descriptor.kind) {\n\t\tcase 'boolean':\n\t\t\treturn value === 1 || value === true\n\t\tcase 'array':\n\t\t\tif (typeof value === 'string') {\n\t\t\t\treturn JSON.parse(value) as unknown[]\n\t\t\t}\n\t\t\treturn value\n\t\tcase 'richtext':\n\t\t\treturn decodeRichtext(value)\n\t\tdefault:\n\t\t\treturn value\n\t}\n}\n","import { generateUUIDv7 } from '@korajs/core'\nimport type { Operation, SchemaDefinition, VersionVector } from '@korajs/core'\nimport { deserializeOperationWithCollection } from '../serialization/serializer'\nimport type { OperationRow, StorageAdapter } from '../types'\nimport type {\n\tBackupManifest,\n\tBackupOptions,\n\tBackupProgress,\n\tRestoreOptions,\n\tRestoreResult,\n} from './types'\n\n// ── Section I/O helpers ──────────────────────────────────────────────────────\n\nfunction encodeSection(name: string, content: Uint8Array): Uint8Array {\n\tconst nameBytes = new TextEncoder().encode(name)\n\tconst header = new Uint8Array(8)\n\tconst dv = new DataView(header.buffer)\n\tdv.setUint32(0, nameBytes.length, true)\n\tdv.setUint32(4, content.length, true)\n\n\tconst result = new Uint8Array(header.length + nameBytes.length + content.length)\n\tresult.set(header, 0)\n\tresult.set(nameBytes, 8)\n\tresult.set(content, 8 + nameBytes.length)\n\treturn result\n}\n\nfunction encodeJsonSection(name: string, data: unknown): Uint8Array {\n\tconst json = JSON.stringify(data)\n\treturn encodeSection(name, new TextEncoder().encode(json))\n}\n\nfunction encodeNdjsonSection(name: string, records: unknown[]): Uint8Array {\n\tconst lines = `${records.map((r) => JSON.stringify(r)).join('\\n')}\\n`\n\treturn encodeSection(name, new TextEncoder().encode(lines))\n}\n\nfunction readUint32(view: DataView, offset: number): number {\n\treturn view.getUint32(offset, true)\n}\n\ninterface Section {\n\tname: string\n\tcontent: Uint8Array\n}\n\nfunction parseSections(data: Uint8Array): Section[] {\n\tconst sections: Section[] = []\n\tconst dv = new DataView(data.buffer, data.byteOffset, data.byteLength)\n\tlet offset = 0\n\n\twhile (offset + 8 <= data.byteLength) {\n\t\tconst nameLen = readUint32(dv, offset)\n\t\tconst contentLen = readUint32(dv, offset + 4)\n\t\toffset += 8\n\n\t\tif (offset + nameLen + contentLen > data.byteLength) break\n\n\t\tconst name = new TextDecoder().decode(data.slice(offset, offset + nameLen))\n\t\toffset += nameLen\n\n\t\tconst content = data.slice(offset, offset + contentLen)\n\t\toffset += contentLen\n\n\t\tsections.push({ name, content })\n\t}\n\n\treturn sections\n}\n\nfunction findSection(sections: Section[], name: string): Uint8Array | null {\n\tfor (const s of sections) {\n\t\tif (s.name === name) return s.content\n\t}\n\treturn null\n}\n\nfunction parseJsonSection<T>(sections: Section[], name: string): T | null {\n\tconst content = findSection(sections, name)\n\tif (!content) return null\n\treturn JSON.parse(new TextDecoder().decode(content)) as T\n}\n\n/**\n * Export format version. Bump on breaking changes.\n */\nconst BACKUP_VERSION = 1\n\n/**\n * Export a backup from a store.\n *\n * Reads all data via the adapter and produces a portable binary backup.\n *\n * @param adapter - The storage adapter\n * @param schema - The schema definition\n * @param nodeId - Current node ID\n * @param schemaVersion - Schema version\n * @param options - Backup options\n * @returns Backup as a Uint8Array\n */\nexport async function exportBackup(\n\tadapter: StorageAdapter,\n\tschema: SchemaDefinition,\n\tnodeId: string,\n\tschemaVersion: number,\n\toptions?: BackupOptions,\n): Promise<Uint8Array> {\n\tconst onProgress = options?.onProgress ?? (() => {})\n\tconst includeRecords = options?.includeRecords ?? true\n\tconst collectionFilter = options?.collections\n\tconst collections = collectionFilter\n\t\t? Object.keys(schema.collections).filter((c) => collectionFilter.includes(c))\n\t\t: Object.keys(schema.collections)\n\n\tonProgress({ phase: 'reading', progress: 0, message: 'Reading version vector' })\n\tconst versionVector = await readVersionVector(adapter)\n\tonProgress({ phase: 'reading', progress: 0.1, message: 'Reading metadata' })\n\tconst meta = await readMeta(adapter)\n\tonProgress({ phase: 'reading', progress: 0.2, message: 'Reading operations' })\n\tconst operations = await readAllOperations(adapter, schema)\n\tonProgress({ phase: 'reading', progress: 0.5, message: 'Reading records' })\n\n\tconst sections: Uint8Array[] = []\n\tlet allContentForChecksum = new Uint8Array(0)\n\n\t// Helper: add a section and update running checksum content\n\tconst addSection = (name: string, data: Uint8Array) => {\n\t\tsections.push(data)\n\t\tconst newLen = allContentForChecksum.length + data.length\n\t\tconst combined = new Uint8Array(newLen)\n\t\tcombined.set(allContentForChecksum, 0)\n\t\tcombined.set(data, allContentForChecksum.length)\n\t\tallContentForChecksum = combined\n\t}\n\n\t// Version vector section\n\tconst vvObj: Record<string, number> = {}\n\tfor (const [nid, seq] of versionVector) {\n\t\tvvObj[nid] = seq\n\t}\n\taddSection('version_vector', encodeJsonSection('version_vector', vvObj))\n\n\t// Meta section\n\tconst metaObj: Record<string, string> = {}\n\tfor (const row of meta) {\n\t\tmetaObj[row.key] = row.value\n\t}\n\taddSection('meta', encodeJsonSection('meta', metaObj))\n\n\t// Operations section\n\tconst opLines = `${operations.map((op) => JSON.stringify(op)).join('\\n')}\\n`\n\taddSection('operations', encodeSection('operations', new TextEncoder().encode(opLines)))\n\n\t// Records sections (optional)\n\tlet totalRecords = 0\n\tif (includeRecords) {\n\t\tfor (const col of collections) {\n\t\t\tconst records = await readCollectionRecords(adapter, col)\n\t\t\tif (records.length > 0) {\n\t\t\t\tconst lines = `${records.map((r) => JSON.stringify(r)).join('\\n')}\\n`\n\t\t\t\tconst sectionName = `records:${col}`\n\t\t\t\taddSection(sectionName, encodeSection(sectionName, new TextEncoder().encode(lines)))\n\t\t\t\ttotalRecords += records.length\n\t\t\t}\n\t\t}\n\t}\n\tonProgress({ phase: 'reading', progress: 0.9, message: 'Computing checksum' })\n\n\t// Compute SHA-256 checksum over all content sections\n\tconst checksumHex = await computeSha256(allContentForChecksum)\n\tonProgress({ phase: 'writing', progress: 1, message: 'Finalizing' })\n\n\t// Build manifest\n\tconst manifest: BackupManifest = {\n\t\tversion: BACKUP_VERSION,\n\t\tcreatedAt: Date.now(),\n\t\tnodeId,\n\t\tschemaVersion,\n\t\toperationCount: operations.length,\n\t\tcollections,\n\t\tincludesRecords: includeRecords,\n\t\tchecksum: checksumHex,\n\t}\n\n\t// Final output: header + manifest + all content sections + checksum section\n\tconst header = new Uint8Array(0)\n\n\t// Merge manifest + all sections + checksum section\n\tconst manifestSection = encodeJsonSection('manifest', manifest)\n\tconst checksumSection = encodeSection('checksum', new TextEncoder().encode(checksumHex))\n\n\tconst totalLen = manifestSection.length + allContentForChecksum.length + checksumSection.length\n\tconst result = new Uint8Array(totalLen)\n\tlet pos = 0\n\tresult.set(manifestSection, pos)\n\tpos += manifestSection.length\n\tresult.set(allContentForChecksum, pos)\n\tpos += allContentForChecksum.length\n\tresult.set(checksumSection, pos)\n\n\treturn result\n}\n\n/**\n * Read backup manifest without loading the entire backup.\n * Parses only the first section (which is always the manifest).\n *\n * @param data - The raw backup data\n * @returns The backup manifest\n */\nexport function readBackupManifest(data: Uint8Array): BackupManifest {\n\tconst sections = parseSections(data)\n\tconst manifest = parseJsonSection<BackupManifest>(sections, 'manifest')\n\tif (!manifest) {\n\t\tthrow new Error('Invalid backup: manifest section not found')\n\t}\n\treturn manifest\n}\n\n/**\n * Validate the backup checksum.\n *\n * @param data - The raw backup data\n * @returns True if the checksum is valid\n */\nexport async function verifyBackupChecksum(data: Uint8Array): Promise<boolean> {\n\tconst sections = parseSections(data)\n\tconst manifest = parseJsonSection<BackupManifest>(sections, 'manifest')\n\tif (!manifest) return false\n\n\tconst storedChecksum = manifest.checksum\n\tif (!storedChecksum) return false\n\n\t// Recompute checksum over content sections (everything after manifest, before checksum)\n\tconst manifestSection = findSection(sections, 'manifest')\n\tif (!manifestSection) return false\n\n\t// Find the checksum section\n\tconst checksumSection = findSection(sections, 'checksum')\n\n\t// The content spans from end of manifest section to start of checksum section\n\tconst checksumBytes = checksumSection\n\t\t? new TextEncoder().encode(manifest.checksum)\n\t\t: new Uint8Array(0)\n\n\t// Rebuild all content sections and hash them\n\tconst contentSections = sections.filter((s) => s.name !== 'manifest' && s.name !== 'checksum')\n\n\tlet contentForHash = new Uint8Array(0)\n\tfor (const section of contentSections) {\n\t\tconst sectionBytes = encodeSection(section.name, section.content)\n\t\tconst newLen = contentForHash.length + sectionBytes.length\n\t\tconst combined = new Uint8Array(newLen)\n\t\tcombined.set(contentForHash, 0)\n\t\tcombined.set(sectionBytes, contentForHash.length)\n\t\tcontentForHash = combined\n\t}\n\n\tconst computed = await computeSha256(contentForHash)\n\treturn computed === storedChecksum\n}\n\n/**\n * Restore a backup into a store.\n *\n * @param adapter - The storage adapter\n * @param schema - The schema definition\n * @param data - The backup data\n * @param options - Restore options\n * @returns Result of the restore operation\n */\nexport async function restoreBackup(\n\tadapter: StorageAdapter,\n\tschema: SchemaDefinition,\n\tdata: Uint8Array,\n\toptions?: RestoreOptions,\n): Promise<RestoreResult> {\n\tconst startTime = Date.now()\n\tconst onProgress = options?.onProgress ?? (() => {})\n\tconst collectionFilter = options?.collections\n\n\tonProgress({ phase: 'verifying', progress: 0, message: 'Parsing backup' })\n\n\tconst sections = parseSections(data)\n\tconst manifest = parseJsonSection<BackupManifest>(sections, 'manifest')\n\tif (!manifest) {\n\t\treturn {\n\t\t\toperationsRestored: 0,\n\t\t\trecordsRestored: 0,\n\t\t\tsuccess: false,\n\t\t\terror: 'Invalid backup: manifest section not found',\n\t\t\tduration: Date.now() - startTime,\n\t\t}\n\t}\n\n\t// Validate format version\n\tif (manifest.version !== BACKUP_VERSION) {\n\t\treturn {\n\t\t\toperationsRestored: 0,\n\t\t\trecordsRestored: 0,\n\t\t\tsuccess: false,\n\t\t\terror: `Unsupported backup version: ${manifest.version}`,\n\t\t\tduration: Date.now() - startTime,\n\t\t}\n\t}\n\n\tonProgress({ phase: 'verifying', progress: 0.2, message: 'Verifying checksum' })\n\n\t// Verify checksum\n\tconst valid = await verifyBackupChecksum(data)\n\tif (!valid) {\n\t\treturn {\n\t\t\toperationsRestored: 0,\n\t\t\trecordsRestored: 0,\n\t\t\tsuccess: false,\n\t\t\terror: 'Backup checksum mismatch: data may be corrupted',\n\t\t\tduration: Date.now() - startTime,\n\t\t}\n\t}\n\n\tonProgress({ phase: 'restoring', progress: 0.3, message: 'Loading operations' })\n\n\t// Parse operations\n\tconst opsContent = findSection(sections, 'operations')\n\tlet operations: Operation[] = []\n\tif (opsContent) {\n\t\tconst text = new TextDecoder().decode(opsContent)\n\t\tconst lines = text\n\t\t\t.trim()\n\t\t\t.split('\\n')\n\t\t\t.filter((l) => l.length > 0)\n\t\toperations = lines.map((line) => JSON.parse(line) as Operation)\n\t}\n\n\t// Filter collections if requested\n\tif (collectionFilter) {\n\t\toperations = operations.filter((op) => collectionFilter.includes(op.collection))\n\t}\n\n\t// Load version vector\n\tconst vvData = parseJsonSection<Record<string, number>>(sections, 'version_vector')\n\n\t// Load meta\n\tconst metaData = parseJsonSection<Record<string, string>>(sections, 'meta')\n\n\tonProgress({ phase: 'restoring', progress: 0.4, message: 'Applying operations' })\n\n\tif (options?.merge) {\n\t\t// Merge mode: replay operations through applyRemoteOperation.\n\t\t// This won't work directly since adapter doesn't have applyRemoteOperation.\n\t\t// Instead, we import the raw data into the adapter.\n\t\tawait adapter.transaction(async (tx) => {\n\t\t\t// Import version vector\n\t\t\tif (vvData) {\n\t\t\t\tfor (const [nid, seq] of Object.entries(vvData)) {\n\t\t\t\t\tawait tx.execute(\n\t\t\t\t\t\t'INSERT OR REPLACE INTO _kora_version_vector (node_id, sequence_number) VALUES (?, ?)',\n\t\t\t\t\t\t[nid, seq],\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Import meta\n\t\t\tif (metaData) {\n\t\t\t\tfor (const [key, value] of Object.entries(metaData)) {\n\t\t\t\t\tawait tx.execute('INSERT OR REPLACE INTO _kora_meta (key, value) VALUES (?, ?)', [\n\t\t\t\t\t\tkey,\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t])\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Import operations into _kora_ops_ tables\n\t\t\tfor (const op of operations) {\n\t\t\t\tconst collection = op.collection\n\t\t\t\tconst opRow = {\n\t\t\t\t\tid: op.id,\n\t\t\t\t\tnode_id: op.nodeId,\n\t\t\t\t\ttype: op.type,\n\t\t\t\t\trecord_id: op.recordId,\n\t\t\t\t\tdata: op.data !== null ? JSON.stringify(op.data) : null,\n\t\t\t\t\tprevious_data: op.previousData !== null ? JSON.stringify(op.previousData) : null,\n\t\t\t\t\ttimestamp: JSON.stringify(op.timestamp),\n\t\t\t\t\tsequence_number: op.sequenceNumber,\n\t\t\t\t\tcausal_deps: JSON.stringify(op.causalDeps),\n\t\t\t\t\tschema_version: op.schemaVersion,\n\t\t\t\t}\n\n\t\t\t\tawait tx.execute(\n\t\t\t\t\t`INSERT OR IGNORE INTO _kora_ops_${collection} (id, node_id, type, record_id, data, previous_data, timestamp, sequence_number, causal_deps, schema_version) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n\t\t\t\t\t[\n\t\t\t\t\t\topRow.id,\n\t\t\t\t\t\topRow.node_id,\n\t\t\t\t\t\topRow.type,\n\t\t\t\t\t\topRow.record_id,\n\t\t\t\t\t\topRow.data,\n\t\t\t\t\t\topRow.previous_data,\n\t\t\t\t\t\topRow.timestamp,\n\t\t\t\t\t\topRow.sequence_number,\n\t\t\t\t\t\topRow.causal_deps,\n\t\t\t\t\t\topRow.schema_version,\n\t\t\t\t\t],\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\t} else {\n\t\t// Replace mode: clear existing data and bulk import\n\t\tawait adapter.transaction(async (tx) => {\n\t\t\t// Clear existing data\n\t\t\tfor (const collection of Object.keys(schema.collections)) {\n\t\t\t\tawait tx.execute(`DELETE FROM _kora_ops_${collection}`)\n\t\t\t\tawait tx.execute(`DELETE FROM ${collection}`)\n\t\t\t}\n\t\t\tawait tx.execute('DELETE FROM _kora_version_vector')\n\t\t\tawait tx.execute('DELETE FROM _kora_meta')\n\n\t\t\t// Import version vector\n\t\t\tif (vvData) {\n\t\t\t\tfor (const [nid, seq] of Object.entries(vvData)) {\n\t\t\t\t\tawait tx.execute(\n\t\t\t\t\t\t'INSERT INTO _kora_version_vector (node_id, sequence_number) VALUES (?, ?)',\n\t\t\t\t\t\t[nid, seq],\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Import meta\n\t\t\tif (metaData) {\n\t\t\t\tfor (const [key, value] of Object.entries(metaData)) {\n\t\t\t\t\tawait tx.execute('INSERT INTO _kora_meta (key, value) VALUES (?, ?)', [key, value])\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Import operations\n\t\t\tfor (const op of operations) {\n\t\t\t\tconst collection = op.collection\n\t\t\t\tawait tx.execute(\n\t\t\t\t\t`INSERT INTO _kora_ops_${collection} (id, node_id, type, record_id, data, previous_data, timestamp, sequence_number, causal_deps, schema_version) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n\t\t\t\t\t[\n\t\t\t\t\t\top.id,\n\t\t\t\t\t\top.nodeId,\n\t\t\t\t\t\top.type,\n\t\t\t\t\t\top.recordId,\n\t\t\t\t\t\top.data !== null ? JSON.stringify(op.data) : null,\n\t\t\t\t\t\top.previousData !== null ? JSON.stringify(op.previousData) : null,\n\t\t\t\t\t\tJSON.stringify(op.timestamp),\n\t\t\t\t\t\top.sequenceNumber,\n\t\t\t\t\t\tJSON.stringify(op.causalDeps),\n\t\t\t\t\t\top.schemaVersion,\n\t\t\t\t\t],\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\t}\n\n\tonProgress({ phase: 'restoring', progress: 0.8, message: 'Restoring records' })\n\n\t// Restore records sections (optional)\n\tlet recordsRestored = 0\n\tif (manifest.includesRecords) {\n\t\tfor (const col of manifest.collections) {\n\t\t\tif (collectionFilter && !collectionFilter.includes(col)) continue\n\n\t\t\tconst sectionContent = findSection(sections, `records:${col}`)\n\t\t\tif (!sectionContent) continue\n\n\t\t\tconst text = new TextDecoder().decode(sectionContent)\n\t\t\tconst lines = text\n\t\t\t\t.trim()\n\t\t\t\t.split('\\n')\n\t\t\t\t.filter((l) => l.length > 0)\n\t\t\tconst records = lines.map((line) => JSON.parse(line) as Record<string, unknown>)\n\n\t\t\tif (records.length === 0) continue\n\n\t\t\tawait adapter.transaction(async (tx) => {\n\t\t\t\tfor (const record of records) {\n\t\t\t\t\tconst keys = Object.keys(record)\n\t\t\t\t\tconst placeholders = keys.map(() => '?').join(', ')\n\t\t\t\t\tconst values = keys.map((k) => {\n\t\t\t\t\t\tconst v = record[k]\n\t\t\t\t\t\tif (typeof v === 'boolean') return v ? 1 : 0\n\t\t\t\t\t\tif (Array.isArray(v) || (typeof v === 'object' && v !== null)) return JSON.stringify(v)\n\t\t\t\t\t\treturn v\n\t\t\t\t\t})\n\n\t\t\t\t\tawait tx.execute(\n\t\t\t\t\t\t`INSERT OR REPLACE INTO ${col} (${keys.join(', ')}) VALUES (${placeholders})`,\n\t\t\t\t\t\tvalues,\n\t\t\t\t\t)\n\t\t\t\t}\n\t\t\t})\n\n\t\t\trecordsRestored += records.length\n\t\t}\n\t}\n\n\tonProgress({ phase: 'restoring', progress: 1, message: 'Done' })\n\n\treturn {\n\t\toperationsRestored: operations.length,\n\t\trecordsRestored,\n\t\tsuccess: true,\n\t\tduration: Date.now() - startTime,\n\t}\n}\n\n// ── Reading helpers ──────────────────────────────────────────────────────────\n\nasync function readVersionVector(adapter: StorageAdapter): Promise<Map<string, number>> {\n\tconst rows = await adapter.query<{ node_id: string; sequence_number: number }>(\n\t\t'SELECT node_id, sequence_number FROM _kora_version_vector',\n\t)\n\tconst vv = new Map<string, number>()\n\tfor (const row of rows) {\n\t\tvv.set(row.node_id, row.sequence_number)\n\t}\n\treturn vv\n}\n\ninterface MetaRow {\n\tkey: string\n\tvalue: string\n}\n\nasync function readMeta(adapter: StorageAdapter): Promise<MetaRow[]> {\n\treturn adapter.query<MetaRow>('SELECT key, value FROM _kora_meta')\n}\n\nasync function readAllOperations(\n\tadapter: StorageAdapter,\n\tschema: SchemaDefinition,\n): Promise<Operation[]> {\n\tconst allOps: Operation[] = []\n\n\tfor (const collectionName of Object.keys(schema.collections)) {\n\t\tconst rows = await adapter.query<OperationRow>(\n\t\t\t`SELECT * FROM _kora_ops_${collectionName} ORDER BY sequence_number ASC`,\n\t\t)\n\n\t\tfor (const row of rows) {\n\t\t\tallOps.push(deserializeOperationWithCollection(row, collectionName))\n\t\t}\n\t}\n\n\treturn allOps\n}\n\nasync function readCollectionRecords(\n\tadapter: StorageAdapter,\n\tcollection: string,\n): Promise<Record<string, unknown>[]> {\n\treturn adapter.query<Record<string, unknown>>(`SELECT * FROM ${collection} WHERE _deleted = 0`)\n}\n\n// ── Crypto helpers ───────────────────────────────────────────────────────────\n\nasync function computeSha256(data: Uint8Array<ArrayBuffer>): Promise<string> {\n\tconst hashBuffer = await crypto.subtle.digest('SHA-256', data)\n\tconst hashArray = Array.from(new Uint8Array(hashBuffer))\n\treturn hashArray.map((b) => b.toString(16).padStart(2, '0')).join('')\n}\n","const WIRE_BYTES_KEY = '__kora_bytes__'\n\n/**\n * JSON.stringify replacer that preserves binary field values (e.g. richtext).\n */\nexport function auditJsonReplacer(_key: string, value: unknown): unknown {\n\tif (value instanceof Uint8Array) {\n\t\tlet binary = ''\n\t\tfor (let i = 0; i < value.length; i++) {\n\t\t\tbinary += String.fromCharCode(value[i] as number)\n\t\t}\n\t\treturn { [WIRE_BYTES_KEY]: btoa(binary) }\n\t}\n\treturn value\n}\n\n/**\n * JSON.parse reviver that restores binary field values.\n */\nexport function auditJsonReviver(_key: string, value: unknown): unknown {\n\tif (\n\t\tvalue !== null &&\n\t\ttypeof value === 'object' &&\n\t\tWIRE_BYTES_KEY in value &&\n\t\ttypeof (value as Record<string, unknown>)[WIRE_BYTES_KEY] === 'string'\n\t) {\n\t\tconst encoded = (value as Record<string, unknown>)[WIRE_BYTES_KEY]\n\t\tif (typeof encoded !== 'string') {\n\t\t\treturn value\n\t\t}\n\t\tconst binary = atob(encoded)\n\t\tconst bytes = new Uint8Array(binary.length)\n\t\tfor (let i = 0; i < binary.length; i++) {\n\t\t\tbytes[i] = binary.charCodeAt(i)\n\t\t}\n\t\treturn bytes\n\t}\n\treturn value\n}\n\n/**\n * Serialize a value for durable audit storage or export.\n */\nexport function serializeAuditJson(value: unknown): string {\n\treturn JSON.stringify(value, auditJsonReplacer)\n}\n\n/**\n * Deserialize audit JSON back to typed values.\n */\nexport function deserializeAuditJson<T>(json: string): T {\n\treturn JSON.parse(json, auditJsonReviver) as T\n}\n","import { generateUUIDv7 } from '@korajs/core'\nimport type { KoraEvent, MergeTrace } from '@korajs/core'\nimport type { StorageAdapter } from '../types'\nimport { deserializeAuditJson, serializeAuditJson } from './audit-json'\nimport type { AuditTraceQuery, PersistedAuditTrace } from './types'\n\ninterface AuditTraceRow {\n\tid: string\n\trecorded_at: number\n\tevent_type: string\n\tcollection: string\n\trecord_id: string\n\tfield: string\n\tstrategy: string\n\ttier: number\n\tconstraint_name: string | null\n\ttrace_json: string\n}\n\n/**\n * Build a {@link PersistedAuditTrace} from a merge-related Kora event.\n */\nexport function persistedAuditTraceFromEvent(\n\tevent: Extract<KoraEvent, { type: 'merge:completed' | 'merge:conflict' | 'constraint:violated' }>,\n): PersistedAuditTrace {\n\treturn {\n\t\tid: generateUUIDv7(),\n\t\trecordedAt: Date.now(),\n\t\teventType: event.type,\n\t\tconstraint: event.type === 'constraint:violated' ? event.constraint : undefined,\n\t\ttrace: event.trace,\n\t}\n}\n\n/**\n * Append a merge trace to the durable audit log.\n */\nexport async function appendAuditTrace(\n\tadapter: StorageAdapter,\n\ttrace: PersistedAuditTrace,\n): Promise<void> {\n\tconst collection = trace.trace.operationA.collection\n\tconst recordId = trace.trace.operationA.recordId\n\n\tawait adapter.execute(\n\t\t`INSERT INTO _kora_audit_traces (\n\t\t\tid, recorded_at, event_type, collection, record_id, field, strategy, tier, constraint_name, trace_json\n\t\t) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,\n\t\t[\n\t\t\ttrace.id,\n\t\t\ttrace.recordedAt,\n\t\t\ttrace.eventType,\n\t\t\tcollection,\n\t\t\trecordId,\n\t\t\ttrace.trace.field,\n\t\t\ttrace.trace.strategy,\n\t\t\ttrace.trace.tier,\n\t\t\ttrace.constraint ?? null,\n\t\t\tserializeAuditJson(trace),\n\t\t],\n\t)\n}\n\n/**\n * Load persisted audit traces with optional filters.\n */\nexport async function readAuditTraces(\n\tadapter: StorageAdapter,\n\tquery?: AuditTraceQuery,\n): Promise<PersistedAuditTrace[]> {\n\tconst conditions: string[] = []\n\tconst params: unknown[] = []\n\n\tif (query?.collections && query.collections.length > 0) {\n\t\tconst placeholders = query.collections.map(() => '?').join(', ')\n\t\tconditions.push(`collection IN (${placeholders})`)\n\t\tparams.push(...query.collections)\n\t}\n\tif (query?.since !== undefined) {\n\t\tconditions.push('recorded_at >= ?')\n\t\tparams.push(query.since)\n\t}\n\tif (query?.until !== undefined) {\n\t\tconditions.push('recorded_at <= ?')\n\t\tparams.push(query.until)\n\t}\n\n\tconst whereClause = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''\n\tconst rows = await adapter.query<AuditTraceRow>(\n\t\t`SELECT * FROM _kora_audit_traces ${whereClause} ORDER BY recorded_at ASC, id ASC`,\n\t\tparams,\n\t)\n\n\treturn rows.map(rowToPersistedTrace)\n}\n\nfunction rowToPersistedTrace(row: AuditTraceRow): PersistedAuditTrace {\n\treturn deserializeAuditJson<PersistedAuditTrace>(row.trace_json)\n}\n\n/**\n * Count persisted audit traces (for diagnostics).\n */\nexport async function countAuditTraces(adapter: StorageAdapter): Promise<number> {\n\tconst rows = await adapter.query<{ count: number }>(\n\t\t'SELECT COUNT(*) as count FROM _kora_audit_traces',\n\t)\n\treturn rows[0]?.count ?? 0\n}\n\n/**\n * Extract collection and record id from a merge trace for indexing.\n */\nexport function auditTraceIndexFields(trace: MergeTrace): {\n\tcollection: string\n\trecordId: string\n} {\n\treturn {\n\t\tcollection: trace.operationA.collection,\n\t\trecordId: trace.operationA.recordId,\n\t}\n}\n","import type { Operation, SchemaDefinition } from '@korajs/core'\nimport { deserializeOperationWithCollection } from '../serialization/serializer'\nimport type { StorageAdapter } from '../types'\nimport type { OperationRow } from '../types'\nimport { deserializeAuditJson, serializeAuditJson } from './audit-json'\nimport { readAuditTraces } from './audit-trace-store'\nimport type {\n\tAuditExportManifest,\n\tAuditExportOptions,\n\tAuditExportPayload,\n\tAuditTraceQuery,\n\tPersistedAuditTrace,\n} from './types'\n\nfunction encodeSection(name: string, content: Uint8Array): Uint8Array {\n\tconst nameBytes = new TextEncoder().encode(name)\n\tconst header = new Uint8Array(8)\n\tconst view = new DataView(header.buffer)\n\tview.setUint32(0, nameBytes.length, true)\n\tview.setUint32(4, content.length, true)\n\n\tconst result = new Uint8Array(header.length + nameBytes.length + content.length)\n\tresult.set(header, 0)\n\tresult.set(nameBytes, 8)\n\tresult.set(content, 8 + nameBytes.length)\n\treturn result\n}\n\nfunction encodeJsonSection(name: string, data: unknown): Uint8Array {\n\treturn encodeSection(name, new TextEncoder().encode(serializeAuditJson(data)))\n}\n\nfunction encodeNdjsonSection(name: string, records: unknown[]): Uint8Array {\n\tconst lines = `${records.map((record) => serializeAuditJson(record)).join('\\n')}\\n`\n\treturn encodeSection(name, new TextEncoder().encode(lines))\n}\n\nfunction readUint32(view: DataView, offset: number): number {\n\treturn view.getUint32(offset, true)\n}\n\ninterface Section {\n\tname: string\n\tcontent: Uint8Array\n}\n\nfunction parseSections(data: Uint8Array): Section[] {\n\tconst sections: Section[] = []\n\tconst view = new DataView(data.buffer, data.byteOffset, data.byteLength)\n\tlet offset = 0\n\n\twhile (offset + 8 <= data.byteLength) {\n\t\tconst nameLen = readUint32(view, offset)\n\t\tconst contentLen = readUint32(view, offset + 4)\n\t\toffset += 8\n\n\t\tif (offset + nameLen + contentLen > data.byteLength) {\n\t\t\tbreak\n\t\t}\n\n\t\tconst name = new TextDecoder().decode(data.slice(offset, offset + nameLen))\n\t\toffset += nameLen\n\n\t\tconst content = data.slice(offset, offset + contentLen)\n\t\toffset += contentLen\n\n\t\tsections.push({ name, content })\n\t}\n\n\treturn sections\n}\n\nfunction findSection(sections: Section[], name: string): Uint8Array | null {\n\tfor (const section of sections) {\n\t\tif (section.name === name) {\n\t\t\treturn section.content\n\t\t}\n\t}\n\treturn null\n}\n\nasync function computeSha256(data: Uint8Array<ArrayBuffer>): Promise<string> {\n\tconst hashBuffer = await crypto.subtle.digest('SHA-256', data)\n\treturn Array.from(new Uint8Array(hashBuffer))\n\t\t.map((byte) => byte.toString(16).padStart(2, '0'))\n\t\t.join('')\n}\n\nasync function readAllOperations(\n\tadapter: StorageAdapter,\n\tschema: SchemaDefinition,\n\tcollectionFilter?: string[],\n): Promise<Operation[]> {\n\tconst collections = collectionFilter\n\t\t? Object.keys(schema.collections).filter((name) => collectionFilter.includes(name))\n\t\t: Object.keys(schema.collections)\n\n\tconst allOps: Operation[] = []\n\tfor (const collectionName of collections) {\n\t\tconst rows = await adapter.query<OperationRow>(\n\t\t\t`SELECT * FROM _kora_ops_${collectionName} ORDER BY sequence_number ASC`,\n\t\t)\n\n\t\tfor (const row of rows) {\n\t\t\tallOps.push(deserializeOperationWithCollection(row, collectionName))\n\t\t}\n\t}\n\n\treturn allOps\n}\n\nfunction filterOperationsByCollections(\n\toperations: Operation[],\n\tcollectionFilter?: string[],\n): Operation[] {\n\tif (!collectionFilter || collectionFilter.length === 0) {\n\t\treturn operations\n\t}\n\tconst allowed = new Set(collectionFilter)\n\treturn operations.filter((op) => allowed.has(op.collection))\n}\n\n/**\n * Export operations and persisted merge traces as a portable binary audit bundle.\n */\nexport async function exportAudit(\n\tadapter: StorageAdapter,\n\tschema: SchemaDefinition,\n\tnodeId: string,\n\tschemaVersion: number,\n\toptions?: AuditExportOptions,\n): Promise<Uint8Array> {\n\tconst onProgress = options?.onProgress ?? (() => {})\n\tconst traceQuery: AuditTraceQuery = {\n\t\tcollections: options?.collections,\n\t\tsince: options?.since,\n\t\tuntil: options?.until,\n\t}\n\n\tonProgress({ phase: 'reading', progress: 0, message: 'Reading operations' })\n\tconst operations = filterOperationsByCollections(\n\t\tawait readAllOperations(adapter, schema, options?.collections),\n\t\toptions?.collections,\n\t)\n\n\tonProgress({ phase: 'reading', progress: 0.4, message: 'Reading merge traces' })\n\tconst mergeTraces = await readAuditTraces(adapter, traceQuery)\n\n\tonProgress({ phase: 'writing', progress: 0.7, message: 'Encoding audit export' })\n\tconst sections: Uint8Array[] = []\n\tlet contentForChecksum = new Uint8Array(0)\n\n\tconst addSection = (section: Uint8Array): void => {\n\t\tsections.push(section)\n\t\tconst combined = new Uint8Array(contentForChecksum.length + section.length)\n\t\tcombined.set(contentForChecksum, 0)\n\t\tcombined.set(section, contentForChecksum.length)\n\t\tcontentForChecksum = combined\n\t}\n\n\taddSection(encodeNdjsonSection('operations', operations))\n\taddSection(encodeNdjsonSection('merge_traces', mergeTraces))\n\n\tonProgress({ phase: 'verifying', progress: 0.9, message: 'Computing checksum' })\n\tconst checksum = await computeSha256(contentForChecksum as Uint8Array<ArrayBuffer>)\n\n\tconst manifest: AuditExportManifest = {\n\t\tversion: 1,\n\t\texportedAt: Date.now(),\n\t\tnodeId,\n\t\tschemaVersion,\n\t\toperationCount: operations.length,\n\t\tmergeTraceCount: mergeTraces.length,\n\t\tchecksum,\n\t}\n\n\tconst manifestSection = encodeJsonSection('manifest', manifest)\n\tconst checksumSection = encodeSection('checksum', new TextEncoder().encode(checksum))\n\n\tonProgress({ phase: 'writing', progress: 1, message: 'Finalizing' })\n\n\tconst parts = [manifestSection, ...sections, checksumSection]\n\tconst finalOutput = new Uint8Array(parts.reduce((sum, part) => sum + part.length, 0))\n\tlet offset = 0\n\tfor (const part of parts) {\n\t\tfinalOutput.set(part, offset)\n\t\toffset += part.length\n\t}\n\n\treturn finalOutput\n}\n\n/**\n * Decode an audit export binary into structured data.\n */\nexport function decodeAuditExport(data: Uint8Array): AuditExportPayload {\n\tconst sections = parseSections(data)\n\tconst manifest = JSON.parse(\n\t\tnew TextDecoder().decode(findSection(sections, 'manifest') ?? new Uint8Array()),\n\t) as AuditExportManifest\n\n\tconst operationsContent = findSection(sections, 'operations')\n\tconst mergeTracesContent = findSection(sections, 'merge_traces')\n\n\tconst operations = parseNdjsonSection<Operation>(operationsContent)\n\tconst mergeTraces = parseNdjsonSection<PersistedAuditTrace>(mergeTracesContent)\n\n\treturn { manifest, operations, mergeTraces }\n}\n\n/**\n * Read only the manifest from an audit export without decoding all sections.\n */\nexport function readAuditExportManifest(data: Uint8Array): AuditExportManifest {\n\tconst sections = parseSections(data)\n\tconst manifestContent = findSection(sections, 'manifest')\n\tif (!manifestContent) {\n\t\tthrow new Error('Invalid audit export: missing manifest section')\n\t}\n\treturn JSON.parse(new TextDecoder().decode(manifestContent)) as AuditExportManifest\n}\n\n/**\n * Verify the checksum of an audit export file.\n */\nexport async function verifyAuditExportChecksum(data: Uint8Array): Promise<boolean> {\n\tconst sections = parseSections(data)\n\tconst manifest = readAuditExportManifest(data)\n\tconst contentSections = sections.filter(\n\t\t(section) => section.name !== 'manifest' && section.name !== 'checksum',\n\t)\n\n\tlet content = new Uint8Array(0)\n\tfor (const section of contentSections) {\n\t\tconst encoded = encodeSection(section.name, section.content)\n\t\tconst combined = new Uint8Array(content.length + encoded.length)\n\t\tcombined.set(content, 0)\n\t\tcombined.set(encoded, content.length)\n\t\tcontent = combined\n\t}\n\n\tconst checksum = await computeSha256(content as Uint8Array<ArrayBuffer>)\n\treturn checksum === manifest.checksum\n}\n\nfunction parseNdjsonSection<T>(content: Uint8Array | null): T[] {\n\tif (!content || content.length === 0) {\n\t\treturn []\n\t}\n\tconst text = new TextDecoder().decode(content).trim()\n\tif (text.length === 0) {\n\t\treturn []\n\t}\n\treturn text.split('\\n').map((line) => deserializeAuditJson<T>(line))\n}\n","// @korajs/store — public API\n// Every export here is a public API commitment. Be explicit.\n\n// === Types ===\nexport type {\n\tApplyRemoteOptions,\n\tApplyResult,\n\tLocalMutationHandler,\n\tTransactionBufferedEntry,\n\tTransactionCommitBatch,\n\tTransactionCommitResult,\n\tMaterializedRowSnapshot,\n\tCollectionRecord,\n\tReplaySnapshot,\n\tMigrationPlan,\n\tOrderByClause,\n\tOrderByDirection,\n\tQueryDescriptor,\n\tStoreConfig,\n\tStoreIsolation,\n\tStorageAdapter,\n\tSubscriptionCallback,\n\tTransaction,\n\tWhereClause,\n\tWhereOperators,\n} from './types'\n\n// === Errors ===\nexport {\n\tAdapterError,\n\tPersistenceError,\n\tQueryError,\n\tRecordNotFoundError,\n\tStoreNotOpenError,\n\tWorkerInitError,\n\tWorkerTimeoutError,\n} from './errors'\n\n// === Operation log compaction ===\nexport type { CompactionResult, CompactionStrategy } from './compaction/types'\nexport { COMPACTION_BASELINE_META_KEY } from './compaction/types'\nexport {\n\tcompactOperationLog,\n\tcomputeAckCompactionWatermark,\n} from './compaction/compact-operation-log'\n\n// === Sync state helpers ===\nexport {\n\tcollectOperationsAheadOfServer,\n\tdeserializeVersionVectorFromMeta,\n\tLAST_ACKED_SERVER_VECTOR_META_KEY,\n\tmergeVersionVectors,\n\tserializeVersionVectorToMeta,\n} from './sync/sync-state'\n\n// === Store ===\nexport { Store } from './store/store'\nexport type { CollectionAccessor } from './store/store'\n\n// === Query ===\nexport { QueryBuilder } from './query/query-builder'\n\n// === Subscription ===\nexport { SubscriptionManager } from './subscription/subscription-manager'\nexport type {\n\tSubscriptionManagerOptions,\n\tSubscriptionStats,\n} from './subscription/subscription-manager'\nexport { SubscriptionBloomFilter } from './subscription/bloom-filter'\n\n// === Collection ===\nexport { Collection } from './collection/collection'\n\n// === State Machine Validation ===\nexport {\n\tInvalidStateTransitionError,\n\tvalidateStateTransition,\n\tvalidateUpdateStateMachine,\n} from './state-machine/state-validator'\n\n// === Transaction ===\nexport { TransactionContext } from './transaction/transaction-context'\nexport type {\n\tTransactionCollectionAccessor,\n\tTransactionContextConfig,\n} from './transaction/transaction-context'\n\n// === Sequences ===\nexport { SequenceManager } from './sequences/sequence-manager'\n\n// === Richtext Serialization ===\nexport {\n\tdecodeRichtext,\n\tencodeRichtext,\n\trichtextToPlainText,\n\trichtextStatesEqual,\n} from './serialization/richtext-serializer'\n\n// === Query Utilities ===\nexport { pluralize, singularize } from './query/pluralize'\n\n// === Backup/Restore ===\nexport {\n\texportBackup,\n\treadBackupManifest,\n\trestoreBackup,\n\tverifyBackupChecksum,\n} from './backup'\nexport type {\n\tBackupManifest,\n\tBackupOptions,\n\tBackupProgress,\n\tRestoreOptions,\n\tRestoreResult,\n} from './backup'\n\n// === Audit export ===\nexport {\n\tdecodeAuditExport,\n\tpersistedAuditTraceFromEvent,\n\treadAuditExportManifest,\n\tverifyAuditExportChecksum,\n} from './audit'\nexport type {\n\tAuditExportManifest,\n\tAuditExportOptions,\n\tAuditExportPayload,\n\tAuditExportProgress,\n\tAuditTraceQuery,\n\tPersistedAuditTrace,\n} from './audit'\n","import { KoraError } from '@korajs/core'\n\n/**\n * Thrown when a query is invalid (bad field names, invalid operators, etc.).\n */\nexport class QueryError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'QUERY_ERROR', context)\n\t\tthis.name = 'QueryError'\n\t}\n}\n\n/**\n * Thrown when a record is not found by ID (findById, update, delete on missing record).\n */\nexport class RecordNotFoundError extends KoraError {\n\tconstructor(collection: string, recordId: string) {\n\t\tsuper(`Record \"${recordId}\" not found in collection \"${collection}\"`, 'RECORD_NOT_FOUND', {\n\t\t\tcollection,\n\t\t\trecordId,\n\t\t})\n\t\tthis.name = 'RecordNotFoundError'\n\t}\n}\n\n/**\n * Thrown when a storage adapter operation fails.\n */\nexport class AdapterError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(message, 'ADAPTER_ERROR', context)\n\t\tthis.name = 'AdapterError'\n\t}\n}\n\n/**\n * Thrown when an operation is attempted on a store that has not been opened.\n */\nexport class StoreNotOpenError extends KoraError {\n\tconstructor() {\n\t\tsuper('Store is not open. Call store.open() before performing operations.', 'STORE_NOT_OPEN')\n\t\tthis.name = 'StoreNotOpenError'\n\t}\n}\n\n/**\n * Thrown when the Web Worker fails to initialize (WASM load failure, OPFS unavailable, etc.).\n */\nexport class WorkerInitError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(`Worker initialization failed: ${message}`, 'WORKER_INIT_ERROR', context)\n\t\tthis.name = 'WorkerInitError'\n\t}\n}\n\n/**\n * Thrown when the Web Worker does not respond within the configured timeout.\n */\nexport class WorkerTimeoutError extends KoraError {\n\tconstructor(operation: string, timeoutMs: number) {\n\t\tsuper(\n\t\t\t`Worker did not respond within ${timeoutMs}ms for operation \"${operation}\"`,\n\t\t\t'WORKER_TIMEOUT',\n\t\t\t{ operation, timeoutMs },\n\t\t)\n\t\tthis.name = 'WorkerTimeoutError'\n\t}\n}\n\n/**\n * Thrown when IndexedDB persistence operations fail (serialize/deserialize).\n */\nexport class PersistenceError extends KoraError {\n\tconstructor(message: string, context?: Record<string, unknown>) {\n\t\tsuper(`Persistence error: ${message}`, 'PERSISTENCE_ERROR', context)\n\t\tthis.name = 'PersistenceError'\n\t}\n}\n","import type { VersionVector } from '@korajs/core'\n\n/**\n * How the local operation log may be compacted.\n * Materialized collection rows are always kept as the convergence baseline.\n */\nexport type CompactionStrategy =\n\t| { mode: 'never' }\n\t| {\n\t\t\tmode: 'after-ack'\n\t\t\t/** Server version vector; defaults to persisted last-acked vector on the store. */\n\t\t\tserverVector?: VersionVector\n\t }\n\t| {\n\t\t\tmode: 'after-days'\n\t\t\t/** Delete only ops older than this many days (still requires ack watermark). */\n\t\t\tdays: number\n\t\t\tserverVector?: VersionVector\n\t }\n\n/**\n * Result of a compaction run.\n */\nexport interface CompactionResult {\n\t/** Number of operation log rows removed. */\n\tdeletedCount: number\n\t/** Per-node sequence ceiling used for deletion (from server ack vector). */\n\twatermark: VersionVector\n}\n\nexport const COMPACTION_BASELINE_META_KEY = 'compaction_baseline_at'\n","import type { SchemaDefinition, VersionVector } from '@korajs/core'\nimport { HybridLogicalClock, createVersionVector } from '@korajs/core'\nimport type { StorageAdapter, Transaction } from '../types'\nimport type { CompactionResult, CompactionStrategy } from './types'\nimport { COMPACTION_BASELINE_META_KEY } from './types'\n\n/**\n * Compute per-node sequence watermarks safe to compact (server has acknowledged these ops).\n */\nexport function computeAckCompactionWatermark(serverVector: VersionVector): VersionVector {\n\tconst watermark = createVersionVector()\n\tfor (const [nodeId, seq] of serverVector) {\n\t\tif (seq > 0) {\n\t\t\twatermark.set(nodeId, seq)\n\t\t}\n\t}\n\treturn watermark\n}\n\n/**\n * Remove acknowledged, materialized operation log entries from `_kora_ops_*` tables.\n * Does not modify collection rows — they are the compaction baseline snapshot.\n */\nexport async function compactOperationLog(\n\tadapter: StorageAdapter,\n\tschema: SchemaDefinition,\n\tstrategy: CompactionStrategy,\n\tserverVector: VersionVector,\n): Promise<CompactionResult> {\n\tif (strategy.mode === 'never') {\n\t\treturn { deletedCount: 0, watermark: createVersionVector() }\n\t}\n\n\tconst watermark = computeAckCompactionWatermark(serverVector)\n\tif (watermark.size === 0) {\n\t\treturn { deletedCount: 0, watermark }\n\t}\n\n\tconst ageCutoffWall =\n\t\tstrategy.mode === 'after-days' ? Date.now() - strategy.days * 24 * 60 * 60 * 1000 : null\n\tconst ageCutoffSerialized =\n\t\tageCutoffWall !== null\n\t\t\t? HybridLogicalClock.serialize({\n\t\t\t\t\twallTime: ageCutoffWall,\n\t\t\t\t\tlogical: 0,\n\t\t\t\t\tnodeId: '',\n\t\t\t\t}).slice(0, 15)\n\t\t\t: null\n\n\tlet deletedCount = 0\n\tconst collectionNames = Object.keys(schema.collections)\n\n\tawait adapter.transaction(async (tx: Transaction) => {\n\t\tfor (const collectionName of collectionNames) {\n\t\t\tconst table = `_kora_ops_${collectionName}`\n\t\t\tfor (const [nodeId, maxSeq] of watermark) {\n\t\t\t\tif (maxSeq <= 0) continue\n\n\t\t\t\tconst countSql = buildCountSql(table, ageCutoffSerialized !== null)\n\t\t\t\tconst countParams =\n\t\t\t\t\tageCutoffSerialized !== null ? [nodeId, maxSeq, ageCutoffSerialized] : [nodeId, maxSeq]\n\t\t\t\tconst countRows = await tx.query<{ count: number }>(countSql, countParams)\n\t\t\t\tdeletedCount += countRows[0]?.count ?? 0\n\n\t\t\t\tconst deleteSql = buildDeleteSql(table, ageCutoffSerialized !== null)\n\t\t\t\tawait tx.execute(deleteSql, countParams)\n\t\t\t}\n\t\t}\n\n\t\tawait tx.execute('INSERT OR REPLACE INTO _kora_meta (key, value) VALUES (?, ?)', [\n\t\t\tCOMPACTION_BASELINE_META_KEY,\n\t\t\tString(Date.now()),\n\t\t])\n\t})\n\n\treturn { deletedCount, watermark }\n}\n\nfunction buildCountSql(table: string, withAge: boolean): string {\n\tif (withAge) {\n\t\treturn `SELECT COUNT(*) as count FROM ${table} WHERE node_id = ? AND sequence_number <= ? AND SUBSTR(timestamp, 1, 15) < ?`\n\t}\n\treturn `SELECT COUNT(*) as count FROM ${table} WHERE node_id = ? AND sequence_number <= ?`\n}\n\nfunction buildDeleteSql(table: string, withAge: boolean): string {\n\tif (withAge) {\n\t\treturn `DELETE FROM ${table} WHERE node_id = ? AND sequence_number <= ? AND SUBSTR(timestamp, 1, 15) < ?`\n\t}\n\treturn `DELETE FROM ${table} WHERE node_id = ? AND sequence_number <= ?`\n}\n","import type { Operation, VersionVector } from '@korajs/core'\nimport { createVersionVector } from '@korajs/core'\nimport type { MetaRow, StorageAdapter } from '../types'\n\nexport const LAST_ACKED_SERVER_VECTOR_META_KEY = 'last_acked_server_vector'\nexport const DELTA_CURSOR_META_KEY = 'delta_cursor'\n\n/**\n * Serialize a version vector for `_kora_meta` storage.\n */\nexport function serializeVersionVectorToMeta(vector: VersionVector): string {\n\tconst record: Record<string, number> = {}\n\tfor (const [nodeId, seq] of vector) {\n\t\trecord[nodeId] = seq\n\t}\n\treturn JSON.stringify(record)\n}\n\n/**\n * Deserialize a version vector from `_kora_meta`.\n */\nexport function deserializeVersionVectorFromMeta(value: string): VersionVector {\n\tconst parsed = JSON.parse(value) as Record<string, number>\n\tconst vector = createVersionVector()\n\tfor (const [nodeId, seq] of Object.entries(parsed)) {\n\t\tif (typeof seq === 'number' && seq >= 0) {\n\t\t\tvector.set(nodeId, seq)\n\t\t}\n\t}\n\treturn vector\n}\n\n/**\n * Merge two version vectors, keeping the maximum sequence per node.\n */\nexport function mergeVersionVectors(a: VersionVector, b: VersionVector): VersionVector {\n\tconst merged = new Map(a)\n\tfor (const [nodeId, seq] of b) {\n\t\tmerged.set(nodeId, Math.max(merged.get(nodeId) ?? 0, seq))\n\t}\n\treturn merged\n}\n\n/**\n * Operations present locally but not yet on the server (per server version vector).\n */\nexport async function collectOperationsAheadOfServer(\n\tlocalVector: VersionVector,\n\tserverVector: VersionVector,\n\tfetchRange: (nodeId: string, fromSeq: number, toSeq: number) => Promise<Operation[]>,\n): Promise<Operation[]> {\n\tconst missing: Operation[] = []\n\tfor (const [nodeId, localSeq] of localVector) {\n\t\tconst serverSeq = serverVector.get(nodeId) ?? 0\n\t\tif (localSeq > serverSeq) {\n\t\t\tconst ops = await fetchRange(nodeId, serverSeq + 1, localSeq)\n\t\t\tmissing.push(...ops)\n\t\t}\n\t}\n\tmissing.sort((a, b) => a.sequenceNumber - b.sequenceNumber)\n\treturn missing\n}\n\n/**\n * Load persisted last-acked server vector from `_kora_meta`.\n */\nexport async function loadLastAckedServerVector(adapter: StorageAdapter): Promise<VersionVector> {\n\tconst rows = await adapter.query<MetaRow>('SELECT value FROM _kora_meta WHERE key = ?', [\n\t\tLAST_ACKED_SERVER_VECTOR_META_KEY,\n\t])\n\tconst row = rows[0]\n\tif (!row?.value) {\n\t\treturn createVersionVector()\n\t}\n\ttry {\n\t\treturn deserializeVersionVectorFromMeta(row.value)\n\t} catch {\n\t\treturn createVersionVector()\n\t}\n}\n\n/**\n * Persist the last-acked server version vector to `_kora_meta`.\n */\nexport async function saveLastAckedServerVector(\n\tadapter: StorageAdapter,\n\tvector: VersionVector,\n): Promise<void> {\n\tawait adapter.execute('INSERT OR REPLACE INTO _kora_meta (key, value) VALUES (?, ?)', [\n\t\tLAST_ACKED_SERVER_VECTOR_META_KEY,\n\t\tserializeVersionVectorToMeta(vector),\n\t])\n}\n\n/**\n * Load a persisted delta cursor for resuming paginated initial sync.\n */\nexport async function loadDeltaCursor(adapter: StorageAdapter): Promise<string | null> {\n\tconst rows = await adapter.query<MetaRow>('SELECT value FROM _kora_meta WHERE key = ?', [\n\t\tDELTA_CURSOR_META_KEY,\n\t])\n\treturn rows[0]?.value ?? null\n}\n\n/**\n * Persist or clear the delta cursor in `_kora_meta`.\n */\nexport async function saveDeltaCursor(\n\tadapter: StorageAdapter,\n\tcursor: string | null,\n): Promise<void> {\n\tif (cursor === null) {\n\t\tawait adapter.execute('DELETE FROM _kora_meta WHERE key = ?', [DELTA_CURSOR_META_KEY])\n\t\treturn\n\t}\n\n\tawait adapter.execute('INSERT OR REPLACE INTO _kora_meta (key, value) VALUES (?, ?)', [\n\t\tDELTA_CURSOR_META_KEY,\n\t\tcursor,\n\t])\n}\n","import {\n\tCausalTracker,\n\tHybridLogicalClock,\n\tcreateVersionVector,\n\tgenerateUUIDv7,\n\tmigrationStepsToSQL,\n} from '@korajs/core'\nimport type {\n\tKoraEventEmitter,\n\tMigrationStep,\n\tOperation,\n\tOperationLog,\n\tSchemaDefinition,\n\tVersionVector,\n} from '@korajs/core'\nimport { readBackupManifest as readManifest } from '../backup/backup'\nimport type { BackupManifest, BackupOptions, RestoreOptions, RestoreResult } from '../backup/types'\nimport { Collection } from '../collection/collection'\nimport { compactOperationLog } from '../compaction/compact-operation-log'\nimport type { CompactionResult, CompactionStrategy } from '../compaction/types'\nimport { StoreNotOpenError } from '../errors'\nimport { isIncomingNewerThanRow, serializeRowVersion } from '../lww/row-version'\nimport type { LocalMutationContext } from '../mutations/types'\nimport { QueryBuilder } from '../query/query-builder'\nimport {\n\tbuildInsertQuery,\n\tbuildLwwSoftDeleteQuery,\n\tbuildLwwUpdateQuery,\n\tbuildSoftDeleteQuery,\n\tbuildUpdateQuery,\n} from '../query/sql-builder'\nimport { RelationEnforcer } from '../relations/relation-enforcer'\nimport { buildReplaySnapshot } from '../replay/replay-to'\nimport type { ReplaySnapshot } from '../replay/replay-to'\nimport { SequenceManager } from '../sequences/sequence-manager'\nimport {\n\tdeserializeOperationWithCollection,\n\tdeserializeRecord,\n\tserializeOperation,\n\tserializeRecord,\n} from '../serialization/serializer'\nimport { SubscriptionManager } from '../subscription/subscription-manager'\nimport {\n\tcollectOperationsAheadOfServer,\n\tloadDeltaCursor,\n\tloadLastAckedServerVector,\n\tmergeVersionVectors,\n\tsaveDeltaCursor,\n\tsaveLastAckedServerVector,\n} from '../sync/sync-state'\nimport { TransactionContext } from '../transaction/transaction-context'\nimport { TransactionSequenceAllocator } from '../transaction/transaction-sequence'\nimport type {\n\tApplyRemoteOptions,\n\tApplyResult,\n\tLocalMutationHandler,\n\tMaterializedRowSnapshot,\n\tMetaRow,\n\tOperationRow,\n\tRawCollectionRow,\n\tStorageAdapter,\n\tStoreConfig,\n\tStoreIsolation,\n\tVersionVectorRow,\n} from '../types'\nimport { allocateNextSequenceNumber } from './sequence-allocator'\nimport { resolvePerTabNodeId } from './tab-node-id'\n\n/**\n * Store is the main orchestrator. It owns a schema, a storage adapter,\n * a clock, and a subscription manager. It creates Collection instances\n * for each schema collection, and provides the sync contract via\n * applyRemoteOperation and getOperationRange.\n *\n * @example\n * ```typescript\n * const store = new Store({ schema, adapter })\n * await store.open()\n * const todo = await store.collection('todos').insert({ title: 'Hello' })\n * await store.close()\n * ```\n */\nexport class Store implements OperationLog {\n\tprivate opened = false\n\tprivate nodeId = ''\n\tprivate sequenceNumber = 0\n\tprivate versionVector: VersionVector = createVersionVector()\n\tprivate clock: HybridLogicalClock | null = null\n\tprivate collections = new Map<string, Collection>()\n\tprivate subscriptionManager: SubscriptionManager\n\tprivate sequenceManager: SequenceManager | null = null\n\n\tprivate readonly schema: SchemaDefinition\n\tprivate readonly adapter: StorageAdapter\n\tprivate readonly configNodeId: string | undefined\n\tprivate readonly dbName: string\n\tprivate readonly isolation: StoreIsolation\n\tprivate readonly emitter: KoraEventEmitter | null\n\tprivate localMutationHandler: LocalMutationHandler | null\n\tprivate relationEnforcer: RelationEnforcer | null = null\n\tprivate causalTracker: CausalTracker | null = null\n\n\tconstructor(config: StoreConfig) {\n\t\tthis.schema = config.schema\n\t\tthis.adapter = config.adapter\n\t\tthis.configNodeId = config.nodeId\n\t\tthis.dbName = config.dbName ?? 'kora-db'\n\t\tthis.isolation = config.isolation ?? 'shared'\n\t\tthis.emitter = config.emitter ?? null\n\t\tthis.localMutationHandler = config.localMutationHandler ?? null\n\t\tthis.subscriptionManager = new SubscriptionManager({\n\t\t\tonQuerySubscribed: config.onQuerySubscribed,\n\t\t})\n\t}\n\n\t/**\n\t * Open the store: initialize the database, load or generate a node ID,\n\t * restore the sequence number and version vector, and create Collection instances.\n\t */\n\tasync open(): Promise<void> {\n\t\tawait this.adapter.open(this.schema)\n\n\t\t// Run schema migrations if needed\n\t\tawait this.runMigrationsIfNeeded()\n\n\t\t// Load or generate node ID\n\t\tthis.nodeId = await this.loadOrGenerateNodeId()\n\t\tthis.clock = new HybridLogicalClock(this.nodeId)\n\t\tthis.causalTracker = new CausalTracker()\n\n\t\t// Initialize sequence manager\n\t\tthis.sequenceManager = new SequenceManager(this.adapter, this.nodeId)\n\n\t\t// Load sequence number and version vector\n\t\tthis.sequenceNumber = await this.loadSequenceNumber()\n\t\tthis.versionVector = await this.loadVersionVector()\n\n\t\t// Create RelationEnforcer if the schema has relations.\n\t\t// The enforcer is shared across all Collection instances so that\n\t\t// cascading deletes can cross collection boundaries.\n\t\tconst hasRelations = Object.keys(this.schema.relations).length > 0\n\t\tthis.relationEnforcer = hasRelations\n\t\t\t? new RelationEnforcer({\n\t\t\t\t\tschema: this.schema,\n\t\t\t\t\tadapter: this.adapter,\n\t\t\t\t\tclock: this.clock,\n\t\t\t\t\tnodeId: this.nodeId,\n\t\t\t\t})\n\t\t\t: null\n\n\t\t// Create collection instances\n\t\tfor (const [name, definition] of Object.entries(this.schema.collections)) {\n\t\t\tconst col = new Collection(\n\t\t\t\tname,\n\t\t\t\tdefinition,\n\t\t\t\tthis.schema,\n\t\t\t\tthis.adapter,\n\t\t\t\tthis.clock,\n\t\t\t\tthis.nodeId,\n\t\t\t\t() => this.allocateSequenceNumber(),\n\t\t\t\t(collectionName, operation) => {\n\t\t\t\t\tthis.recordOperationSequence(operation)\n\t\t\t\t\tthis.subscriptionManager.notify(collectionName, operation)\n\t\t\t\t\tif (this.emitter) {\n\t\t\t\t\t\tthis.emitter.emit({ type: 'operation:created', operation })\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t\tthis.relationEnforcer,\n\t\t\t\tthis.localMutationHandler,\n\t\t\t\tthis.causalTracker,\n\t\t\t)\n\t\t\tthis.collections.set(name, col)\n\t\t}\n\n\t\tthis.opened = true\n\t}\n\n\t/**\n\t * Close the store: clear subscriptions and close the adapter.\n\t */\n\tasync close(): Promise<void> {\n\t\tthis.subscriptionManager.clear()\n\t\tthis.collections.clear()\n\t\tthis.opened = false\n\t\tawait this.adapter.close()\n\t}\n\n\t/**\n\t * Get a Collection instance for CRUD operations.\n\t * @throws {StoreNotOpenError} If the store is not open\n\t * @throws {Error} If the collection name is not in the schema\n\t */\n\tcollection(name: string): CollectionAccessor {\n\t\tthis.ensureOpen()\n\t\tconst col = this.collections.get(name)\n\t\tif (!col) {\n\t\t\tthrow new Error(\n\t\t\t\t`Unknown collection \"${name}\". Available: ${[...this.collections.keys()].join(', ')}`,\n\t\t\t)\n\t\t}\n\n\t\tconst definition = this.schema.collections[name]\n\t\tif (!definition) {\n\t\t\tthrow new Error(`Collection definition not found for \"${name}\"`)\n\t\t}\n\n\t\treturn {\n\t\t\tinsert: (data: Record<string, unknown>) => col.insert(data),\n\t\t\tfindById: (id: string) => col.findById(id),\n\t\t\tupdate: (id: string, data: Record<string, unknown>) => col.update(id, data),\n\t\t\tdelete: (id: string) => col.delete(id),\n\t\t\twhere: (conditions) =>\n\t\t\t\tnew QueryBuilder(\n\t\t\t\t\tname,\n\t\t\t\t\tdefinition,\n\t\t\t\t\tthis.adapter,\n\t\t\t\t\tthis.subscriptionManager,\n\t\t\t\t\tconditions,\n\t\t\t\t\tthis.schema,\n\t\t\t\t),\n\t\t}\n\t}\n\n\t/**\n\t * Get the current version vector.\n\t */\n\tgetVersionVector(): VersionVector {\n\t\tthis.ensureOpen()\n\t\treturn new Map(this.versionVector)\n\t}\n\n\t/**\n\t * Get the node ID for this store instance.\n\t */\n\tgetNodeId(): string {\n\t\tthis.ensureOpen()\n\t\treturn this.nodeId\n\t}\n\n\t/**\n\t * Apply a remote operation received from sync.\n\t * Checks for duplicates, applies to the data table, persists the operation,\n\t * and updates the version vector.\n\t */\n\tasync applyRemoteOperation(op: Operation, options?: ApplyRemoteOptions): Promise<ApplyResult> {\n\t\tthis.ensureOpen()\n\n\t\tconst collection = op.collection\n\t\tconst definition = this.schema.collections[collection]\n\t\tif (!definition) {\n\t\t\treturn 'skipped'\n\t\t}\n\n\t\t// Check for duplicate (content-addressed dedup)\n\t\tconst existing = await this.adapter.query<{ id: string }>(\n\t\t\t`SELECT id FROM _kora_ops_${collection} WHERE id = ?`,\n\t\t\t[op.id],\n\t\t)\n\t\tif (existing.length > 0) {\n\t\t\treturn 'duplicate'\n\t\t}\n\n\t\t// Advance the local HLC; severe clock drift throws ClockDriftError (surfaced as sync:apply-failed).\n\t\tif (this.clock) {\n\t\t\tthis.clock.receive(op.timestamp)\n\t\t}\n\n\t\tconst remoteVersion = serializeRowVersion(op.timestamp)\n\t\tconst wallTime = op.timestamp.wallTime\n\n\t\t// Apply the operation to the data table (LWW-guarded; op log always appended below)\n\t\tawait this.adapter.transaction(async (tx) => {\n\t\t\tif (op.type === 'insert' && op.data) {\n\t\t\t\tconst existing = await tx.query<RawCollectionRow>(\n\t\t\t\t\t`SELECT _updated_at, _version FROM ${collection} WHERE id = ?`,\n\t\t\t\t\t[op.recordId],\n\t\t\t\t)\n\t\t\t\tconst row = existing[0]\n\t\t\t\tif (row && !isIncomingNewerThanRow(op.timestamp, row)) {\n\t\t\t\t\t// Stale remote insert — skip materialization, still record op below\n\t\t\t\t} else {\n\t\t\t\t\tconst serializedData = serializeRecord(op.data, definition.fields)\n\t\t\t\t\tconst record: Record<string, unknown> = {\n\t\t\t\t\t\tid: op.recordId,\n\t\t\t\t\t\t...serializedData,\n\t\t\t\t\t\t_created_at: wallTime,\n\t\t\t\t\t\t_updated_at: wallTime,\n\t\t\t\t\t\t_version: remoteVersion,\n\t\t\t\t\t}\n\t\t\t\t\tconst insertQuery = buildInsertQuery(collection, record)\n\t\t\t\t\tawait tx.execute(insertQuery.sql, insertQuery.params)\n\t\t\t\t}\n\t\t\t} else if (op.type === 'update' && op.data) {\n\t\t\t\tconst serializedChanges = serializeRecord(op.data, definition.fields)\n\t\t\t\tconst updatePayload: Record<string, unknown> = {\n\t\t\t\t\t...serializedChanges,\n\t\t\t\t\t_updated_at: wallTime,\n\t\t\t\t\t_version: remoteVersion,\n\t\t\t\t}\n\t\t\t\tif (options?.reactivateIfDeleted) {\n\t\t\t\t\tupdatePayload._deleted = 0\n\t\t\t\t}\n\t\t\t\tconst updateQuery = buildLwwUpdateQuery(\n\t\t\t\t\tcollection,\n\t\t\t\t\top.recordId,\n\t\t\t\t\tupdatePayload,\n\t\t\t\t\tremoteVersion,\n\t\t\t\t)\n\t\t\t\tawait tx.execute(updateQuery.sql, updateQuery.params)\n\t\t\t} else if (op.type === 'delete') {\n\t\t\t\tconst deleteQuery = buildLwwSoftDeleteQuery(\n\t\t\t\t\tcollection,\n\t\t\t\t\top.recordId,\n\t\t\t\t\twallTime,\n\t\t\t\t\tremoteVersion,\n\t\t\t\t)\n\t\t\t\tawait tx.execute(deleteQuery.sql, deleteQuery.params)\n\t\t\t}\n\n\t\t\t// Persist the operation\n\t\t\tconst opRow = serializeOperation(op)\n\t\t\tconst opInsert = buildInsertQuery(\n\t\t\t\t`_kora_ops_${collection}`,\n\t\t\t\topRow as unknown as Record<string, unknown>,\n\t\t\t)\n\t\t\tawait tx.execute(opInsert.sql, opInsert.params)\n\n\t\t\t// Update version vector\n\t\t\tconst currentSeq = this.versionVector.get(op.nodeId) ?? 0\n\t\t\tif (op.sequenceNumber > currentSeq) {\n\t\t\t\tthis.versionVector.set(op.nodeId, op.sequenceNumber)\n\t\t\t\tawait tx.execute(\n\t\t\t\t\t'INSERT OR REPLACE INTO _kora_version_vector (node_id, sequence_number) VALUES (?, ?)',\n\t\t\t\t\t[op.nodeId, op.sequenceNumber],\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\n\t\t// Notify subscriptions\n\t\tthis.subscriptionManager.notify(collection, op)\n\n\t\treturn 'applied'\n\t}\n\n\t/**\n\t * Get operations from a node within a sequence number range.\n\t * Implements the OperationLog interface for computeDelta.\n\t */\n\tasync getRange(nodeId: string, fromSeq: number, toSeq: number): Promise<Operation[]> {\n\t\treturn this.getOperationRange(nodeId, fromSeq, toSeq)\n\t}\n\n\t/**\n\t * Get operations from a node within a sequence number range.\n\t */\n\tasync getOperationRange(nodeId: string, fromSeq: number, toSeq: number): Promise<Operation[]> {\n\t\tthis.ensureOpen()\n\t\tconst allOps: Operation[] = []\n\n\t\tfor (const collectionName of Object.keys(this.schema.collections)) {\n\t\t\tconst rows = await this.adapter.query<OperationRow>(\n\t\t\t\t`SELECT * FROM _kora_ops_${collectionName} WHERE node_id = ? AND sequence_number >= ? AND sequence_number <= ? ORDER BY sequence_number ASC`,\n\t\t\t\t[nodeId, fromSeq, toSeq],\n\t\t\t)\n\t\t\tfor (const row of rows) {\n\t\t\t\tallOps.push(deserializeOperationWithCollection(row, collectionName))\n\t\t\t}\n\t\t}\n\n\t\t// Sort by sequence number across collections\n\t\tallOps.sort((a, b) => a.sequenceNumber - b.sequenceNumber)\n\t\treturn allOps\n\t}\n\n\t/**\n\t * Load every operation from the local append-only log across all collections.\n\t * Used by sync delta computation, backup export, and time-travel replay.\n\t */\n\tasync getAllOperations(): Promise<Operation[]> {\n\t\tthis.ensureOpen()\n\t\tconst allOps: Operation[] = []\n\n\t\tfor (const collectionName of Object.keys(this.schema.collections)) {\n\t\t\tconst rows = await this.adapter.query<OperationRow>(\n\t\t\t\t`SELECT * FROM _kora_ops_${collectionName} ORDER BY sequence_number ASC`,\n\t\t\t)\n\t\t\tfor (const row of rows) {\n\t\t\t\tallOps.push(deserializeOperationWithCollection(row, collectionName))\n\t\t\t}\n\t\t}\n\n\t\treturn allOps\n\t}\n\n\t/**\n\t * Rebuild an in-memory snapshot of materialized state at a causal cut in the op log.\n\t * Does not mutate the live store — intended for DevTools time-travel inspection.\n\t *\n\t * @param operationId - Content-addressed id of the operation to replay through (inclusive)\n\t * @throws {OperationError} When the operation id is not present in the local log\n\t */\n\tasync replayTo(operationId: string): Promise<ReplaySnapshot> {\n\t\tthis.ensureOpen()\n\t\tconst start = Date.now()\n\t\tconst allOps = await this.getAllOperations()\n\t\tconst snapshot = buildReplaySnapshot(this.schema, allOps, operationId)\n\n\t\tif (this.emitter) {\n\t\t\tthis.emitter.emit({\n\t\t\t\ttype: 'replay:completed',\n\t\t\t\ttargetOperationId: operationId,\n\t\t\t\toperationsApplied: snapshot.operationsApplied.length,\n\t\t\t\tduration: Date.now() - start,\n\t\t\t})\n\t\t}\n\n\t\treturn snapshot\n\t}\n\n\t/**\n\t * Persist a merge trace to the durable audit log.\n\t */\n\tasync appendAuditTrace(trace: import('../audit/types').PersistedAuditTrace): Promise<void> {\n\t\tthis.ensureOpen()\n\t\tconst { appendAuditTrace: append } = await import('../audit/audit-trace-store')\n\t\tawait append(this.adapter, trace)\n\t}\n\n\t/**\n\t * Read persisted audit traces with optional filters.\n\t */\n\tasync getAuditTraces(\n\t\tquery?: import('../audit/types').AuditTraceQuery,\n\t): Promise<import('../audit/types').PersistedAuditTrace[]> {\n\t\tthis.ensureOpen()\n\t\tconst { readAuditTraces } = await import('../audit/audit-trace-store')\n\t\treturn readAuditTraces(this.adapter, query)\n\t}\n\n\t/**\n\t * Export operations and merge traces as a portable audit bundle.\n\t */\n\tasync exportAudit(options?: import('../audit/types').AuditExportOptions): Promise<Uint8Array> {\n\t\tthis.ensureOpen()\n\t\tconst { exportAudit: doExport } = await import('../audit/export-audit')\n\t\treturn doExport(this.adapter, this.schema, this.nodeId, this.schema.version, options)\n\t}\n\n\t/**\n\t * Get the schema definition.\n\t */\n\tgetSchema(): SchemaDefinition {\n\t\treturn this.schema\n\t}\n\n\t/**\n\t * Route local CRUD through the unified apply pipeline (korajs ApplyPipeline).\n\t */\n\tsetLocalMutationHandler(handler: LocalMutationHandler | null): void {\n\t\tthis.localMutationHandler = handler\n\t\tfor (const col of this.collections.values()) {\n\t\t\tcol.setMutationHandler(handler)\n\t\t}\n\t}\n\n\t/**\n\t * Build mutation context for a collection (used by ApplyPipeline side effects).\n\t */\n\tcreateMutationContext(\n\t\tcollection: string,\n\t\toptions?: { inTransaction?: boolean; extraCausalDeps?: string[] },\n\t): LocalMutationContext {\n\t\tthis.ensureOpen()\n\t\tconst definition = this.schema.collections[collection]\n\t\tif (!definition || !this.clock) {\n\t\t\tthrow new StoreNotOpenError()\n\t\t}\n\t\treturn {\n\t\t\tcollection,\n\t\t\tdefinition,\n\t\t\tschema: this.schema,\n\t\t\tadapter: this.adapter,\n\t\t\tclock: this.clock,\n\t\t\tnodeId: this.nodeId,\n\t\t\tallocateSequenceNumber: () => this.allocateSequenceNumber(),\n\t\t\tonMutation: (collectionName, operation) => {\n\t\t\t\tthis.recordOperationSequence(operation)\n\t\t\t\tthis.subscriptionManager.notify(collectionName, operation)\n\t\t\t\tif (this.emitter) {\n\t\t\t\t\tthis.emitter.emit({ type: 'operation:created', operation })\n\t\t\t\t}\n\t\t\t},\n\t\t\trelationEnforcer: this.relationEnforcer,\n\t\t\tcausalTracker: this.causalTracker,\n\t\t\tinTransaction: options?.inTransaction ?? false,\n\t\t\textraCausalDeps: options?.extraCausalDeps,\n\t\t}\n\t}\n\n\t/**\n\t * Load a materialized row by ID, including soft-deleted tombstones.\n\t */\n\tasync findMaterializedRow(\n\t\tcollection: string,\n\t\trecordId: string,\n\t): Promise<MaterializedRowSnapshot | null> {\n\t\tthis.ensureOpen()\n\t\tconst definition = this.schema.collections[collection]\n\t\tif (!definition) {\n\t\t\treturn null\n\t\t}\n\n\t\tconst rows = await this.adapter.query<RawCollectionRow>(\n\t\t\t`SELECT * FROM ${collection} WHERE id = ?`,\n\t\t\t[recordId],\n\t\t)\n\t\tconst row = rows[0]\n\t\tif (!row) {\n\t\t\treturn null\n\t\t}\n\n\t\treturn {\n\t\t\trecord: deserializeRecord(row, definition.fields),\n\t\t\tdeleted: row._deleted === 1,\n\t\t}\n\t}\n\n\t/**\n\t * Latest operation from this device for a record (used for delete-vs-update merge on sync).\n\t */\n\t/**\n\t * Load the last server version vector acknowledged by this client (persisted in `_kora_meta`).\n\t */\n\tasync loadLastAckedServerVector(): Promise<VersionVector> {\n\t\tthis.ensureOpen()\n\t\treturn loadLastAckedServerVector(this.adapter)\n\t}\n\n\t/**\n\t * Persist the last server version vector this client believes the server has applied.\n\t */\n\tasync saveLastAckedServerVector(vector: VersionVector): Promise<void> {\n\t\tthis.ensureOpen()\n\t\tawait saveLastAckedServerVector(this.adapter, vector)\n\t}\n\n\t/**\n\t * Load persisted delta cursor for resuming paginated initial sync.\n\t */\n\tasync loadDeltaCursor(): Promise<string | null> {\n\t\tthis.ensureOpen()\n\t\treturn loadDeltaCursor(this.adapter)\n\t}\n\n\t/**\n\t * Persist or clear the delta cursor for paginated initial sync resume.\n\t */\n\tasync saveDeltaCursor(cursor: string | null): Promise<void> {\n\t\tthis.ensureOpen()\n\t\tawait saveDeltaCursor(this.adapter, cursor)\n\t}\n\n\t/**\n\t * Local operations not yet reflected on the server version vector.\n\t */\n\tasync getUnsyncedOperations(serverVector: VersionVector): Promise<Operation[]> {\n\t\tthis.ensureOpen()\n\t\treturn collectOperationsAheadOfServer(\n\t\t\tthis.getVersionVector(),\n\t\t\tserverVector,\n\t\t\t(nodeId, fromSeq, toSeq) => this.getOperationRange(nodeId, fromSeq, toSeq),\n\t\t)\n\t}\n\n\t/**\n\t * Count of local operations ahead of the server version vector.\n\t */\n\tasync countUnsyncedOperations(serverVector: VersionVector): Promise<number> {\n\t\tconst ops = await this.getUnsyncedOperations(serverVector)\n\t\treturn ops.length\n\t}\n\n\t/**\n\t * Compact the local operation log using materialized rows as the baseline.\n\t * Only removes ops the server has acknowledged (per {@link CompactionStrategy}).\n\t */\n\tasync compact(strategy: CompactionStrategy): Promise<CompactionResult> {\n\t\tthis.ensureOpen()\n\t\tif (strategy.mode === 'never') {\n\t\t\treturn compactOperationLog(this.adapter, this.schema, strategy, createVersionVector())\n\t\t}\n\n\t\tconst serverVector = strategy.serverVector ?? (await loadLastAckedServerVector(this.adapter))\n\t\treturn compactOperationLog(this.adapter, this.schema, strategy, serverVector)\n\t}\n\n\t/**\n\t * Merge session remote vector with persisted last-acked vector (max per node).\n\t */\n\tmergeServerVectors(sessionVector: VersionVector, persistedVector: VersionVector): VersionVector {\n\t\treturn mergeVersionVectors(persistedVector, sessionVector)\n\t}\n\n\tasync getLatestLocalOperationForRecord(\n\t\tcollection: string,\n\t\trecordId: string,\n\t): Promise<Operation | null> {\n\t\tthis.ensureOpen()\n\t\tconst rows = await this.adapter.query<OperationRow>(\n\t\t\t`SELECT * FROM _kora_ops_${collection} WHERE node_id = ? AND record_id = ? ORDER BY sequence_number DESC LIMIT 1`,\n\t\t\t[this.nodeId, recordId],\n\t\t)\n\t\tconst row = rows[0]\n\t\tif (!row) {\n\t\t\treturn null\n\t\t}\n\t\treturn deserializeOperationWithCollection(row, collection)\n\t}\n\n\t/**\n\t * Latest operation for a record from any node (for 3-way merge when local op log is empty).\n\t */\n\tasync getLatestOperationForRecord(\n\t\tcollection: string,\n\t\trecordId: string,\n\t): Promise<Operation | null> {\n\t\tthis.ensureOpen()\n\t\tconst rows = await this.adapter.query<OperationRow>(\n\t\t\t`SELECT * FROM _kora_ops_${collection} WHERE record_id = ?`,\n\t\t\t[recordId],\n\t\t)\n\n\t\tlet latest: Operation | null = null\n\t\tfor (const row of rows) {\n\t\t\tconst op = deserializeOperationWithCollection(row, collection)\n\t\t\tif (!latest || HybridLogicalClock.compare(op.timestamp, latest.timestamp) > 0) {\n\t\t\t\tlatest = op\n\t\t\t}\n\t\t}\n\t\treturn latest\n\t}\n\n\t/** Expose the subscription manager for direct access (e.g., by QueryBuilder) */\n\tgetSubscriptionManager(): SubscriptionManager {\n\t\treturn this.subscriptionManager\n\t}\n\n\t/**\n\t * Get the sequence manager for offline-safe sequence generation.\n\t * @throws {StoreNotOpenError} If the store is not open\n\t */\n\tgetSequenceManager(): SequenceManager {\n\t\tthis.ensureOpen()\n\t\tif (!this.sequenceManager) {\n\t\t\tthrow new StoreNotOpenError()\n\t\t}\n\t\treturn this.sequenceManager\n\t}\n\n\t/**\n\t * Create a TransactionContext for atomic multi-collection operations.\n\t * The returned context buffers all mutations and commits them atomically.\n\t *\n\t * After commit, the caller is responsible for notifying subscriptions\n\t * and emitting events for each operation.\n\t */\n\tcreateTransaction(): TransactionContext {\n\t\tthis.ensureOpen()\n\t\tif (!this.clock) {\n\t\t\tthrow new StoreNotOpenError()\n\t\t}\n\t\treturn new TransactionContext({\n\t\t\tschema: this.schema,\n\t\t\tadapter: this.adapter,\n\t\t\tclock: this.clock,\n\t\t\tnodeId: this.nodeId,\n\t\t\tsequenceAllocator: new TransactionSequenceAllocator(this.adapter, this.nodeId),\n\t\t\trelationEnforcer: this.relationEnforcer,\n\t\t\tcausalTracker: this.causalTracker,\n\t\t\tlocalMutationHandler: this.localMutationHandler,\n\t\t})\n\t}\n\n\t/**\n\t * Execute a function within a transaction. All mutations performed on the\n\t * TransactionContext are committed atomically. Subscription notifications\n\t * are batched and fired after the commit.\n\t *\n\t * If the function throws, the transaction is rolled back and the error is re-thrown.\n\t *\n\t * @param fn - Function receiving a TransactionContext for buffered operations\n\t * @returns The operations that were committed\n\t */\n\tasync transaction(fn: (tx: TransactionContext) => Promise<void>): Promise<Operation[]> {\n\t\tconst tx = this.createTransaction()\n\t\tthis.causalTracker?.beginTransaction()\n\t\ttry {\n\t\t\tawait fn(tx)\n\t\t\tconst { operations } = await tx.commit()\n\n\t\t\tthis.causalTracker?.clearTransaction()\n\n\t\t\t// Notify subscriptions and emit events after commit\n\t\t\tfor (const op of operations) {\n\t\t\t\tthis.recordOperationSequence(op)\n\t\t\t\tthis.subscriptionManager.notify(op.collection, op)\n\t\t\t\tif (this.emitter) {\n\t\t\t\t\tthis.emitter.emit({ type: 'operation:created', operation: op })\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn operations\n\t\t} catch (error) {\n\t\t\ttx.rollback()\n\t\t\tthis.causalTracker?.clearTransaction()\n\t\t\tthrow error\n\t\t}\n\t}\n\n\t/**\n\t * Export all data as a portable backup binary.\n\t * Includes operations, version vector, metadata, and optionally materialized records.\n\t *\n\t * @param options - Backup options (includeRecords, collections, onProgress)\n\t * @returns Backup as a Uint8Array\n\t */\n\tasync exportBackup(options?: BackupOptions): Promise<Uint8Array> {\n\t\tthis.ensureOpen()\n\t\tconst { exportBackup: doExport } = await import('../backup/backup')\n\t\treturn doExport(this.adapter, this.schema, this.nodeId, this.schema.version, options)\n\t}\n\n\t/**\n\t * Restore data from a backup binary.\n\t *\n\t * @param data - The backup data\n\t * @param options - Restore options (merge, collections, onProgress)\n\t * @returns Result of the restore operation\n\t */\n\tasync importBackup(data: Uint8Array, options?: RestoreOptions): Promise<RestoreResult> {\n\t\tthis.ensureOpen()\n\t\tconst { restoreBackup: doRestore } = await import('../backup/backup')\n\t\treturn doRestore(this.adapter, this.schema, data, options)\n\t}\n\n\t/**\n\t * Read backup manifest without loading the entire backup.\n\t *\n\t * @param data - The raw backup data\n\t * @returns The backup manifest\n\t */\n\tstatic readBackupManifest(data: Uint8Array): BackupManifest {\n\t\treturn readManifest(data)\n\t}\n\n\tprivate recordOperationSequence(operation: Operation): void {\n\t\tconst prev = this.versionVector.get(operation.nodeId) ?? 0\n\t\tif (operation.sequenceNumber > prev) {\n\t\t\tthis.versionVector.set(operation.nodeId, operation.sequenceNumber)\n\t\t}\n\t\tif (operation.nodeId === this.nodeId) {\n\t\t\tthis.sequenceNumber = Math.max(this.sequenceNumber, operation.sequenceNumber)\n\t\t}\n\t}\n\n\tprivate async allocateSequenceNumber(): Promise<number> {\n\t\tconst seq = await allocateNextSequenceNumber(this.adapter, this.nodeId)\n\t\tthis.sequenceNumber = seq\n\t\tthis.versionVector.set(this.nodeId, seq)\n\t\treturn seq\n\t}\n\n\t/**\n\t * Check the stored schema version and run any pending migrations.\n\t * Migrations are applied in version order within a transaction.\n\t */\n\tprivate async runMigrationsIfNeeded(): Promise<void> {\n\t\tconst storedVersion = await this.getStoredSchemaVersion()\n\t\tconst targetVersion = this.schema.version\n\n\t\tif (storedVersion >= targetVersion) {\n\t\t\t// Already up to date (or first run with version 1)\n\t\t\tif (storedVersion === 0) {\n\t\t\t\t// First open — store the initial version\n\t\t\t\tawait this.adapter.execute(\n\t\t\t\t\t\"INSERT OR REPLACE INTO _kora_meta (key, value) VALUES ('schema_version', ?)\",\n\t\t\t\t\t[String(targetVersion)],\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn\n\t\t}\n\n\t\t// Run each migration in order from storedVersion+1 to targetVersion\n\t\tconst migrations = this.schema.migrations ?? {}\n\t\tfor (let v = storedVersion + 1; v <= targetVersion; v++) {\n\t\t\tconst migration = migrations[v]\n\t\t\tif (!migration) continue\n\n\t\t\t// Generate SQL from structural steps\n\t\t\tconst sqlStatements = migrationStepsToSQL(migration.steps)\n\n\t\t\t// Execute structural changes individually, tolerating \"duplicate column\" errors\n\t\t\t// because generateSQL already emits --kora:safe-alter ALTER TABLE statements\n\t\t\t// for the current schema's columns (run via generateFullDDL in adapter.open()).\n\t\t\tfor (const sql of sqlStatements) {\n\t\t\t\ttry {\n\t\t\t\t\tawait this.adapter.execute(sql)\n\t\t\t\t} catch (e) {\n\t\t\t\t\tconst msg = (e as Error).message || ''\n\t\t\t\t\tif (!msg.includes('duplicate column name')) {\n\t\t\t\t\t\tthrow e\n\t\t\t\t\t}\n\t\t\t\t\t// Column already exists (added by safe-alter in generateSQL) — safe to skip\n\t\t\t\t}\n\t\t\t}\n\n\t\t\t// Run backfills in a transaction\n\t\t\tconst backfillSteps = migration.steps.filter(\n\t\t\t\t(s): s is Extract<MigrationStep, { type: 'backfill' }> => s.type === 'backfill',\n\t\t\t)\n\t\t\tfor (const step of backfillSteps) {\n\t\t\t\tawait this.runBackfill(step.collection, step.transform)\n\t\t\t}\n\t\t}\n\n\t\t// Update stored schema version\n\t\tawait this.adapter.execute(\n\t\t\t\"INSERT OR REPLACE INTO _kora_meta (key, value) VALUES ('schema_version', ?)\",\n\t\t\t[String(targetVersion)],\n\t\t)\n\t}\n\n\t/**\n\t * Get the stored schema version from _kora_meta. Returns 0 if not set.\n\t */\n\tprivate async getStoredSchemaVersion(): Promise<number> {\n\t\tconst rows = await this.adapter.query<MetaRow>(\n\t\t\t\"SELECT value FROM _kora_meta WHERE key = 'schema_version'\",\n\t\t)\n\t\treturn rows[0] ? Number(rows[0].value) : 0\n\t}\n\n\t/**\n\t * Run a backfill transform on all records in a collection.\n\t * Reads all rows, applies the transform, and updates changed fields.\n\t */\n\tprivate async runBackfill(\n\t\tcollection: string,\n\t\ttransform: (record: Record<string, unknown>) => Record<string, unknown>,\n\t): Promise<void> {\n\t\tconst rows = await this.adapter.query<RawCollectionRow>(\n\t\t\t`SELECT * FROM ${collection} WHERE _deleted = 0`,\n\t\t)\n\n\t\tawait this.adapter.transaction(async (tx) => {\n\t\t\tfor (const row of rows) {\n\t\t\t\tconst updates = transform(row as Record<string, unknown>)\n\t\t\t\tconst fields = Object.keys(updates)\n\t\t\t\tif (fields.length === 0) continue\n\n\t\t\t\tconst setClauses = fields.map((f) => `${f} = ?`).join(', ')\n\t\t\t\tconst values = fields.map((f) => {\n\t\t\t\t\tconst val = updates[f]\n\t\t\t\t\t// Serialize booleans to 0/1 for SQLite\n\t\t\t\t\tif (typeof val === 'boolean') return val ? 1 : 0\n\t\t\t\t\t// Serialize arrays/objects to JSON\n\t\t\t\t\tif (Array.isArray(val) || (typeof val === 'object' && val !== null)) {\n\t\t\t\t\t\treturn JSON.stringify(val)\n\t\t\t\t\t}\n\t\t\t\t\treturn val\n\t\t\t\t})\n\t\t\t\tvalues.push(row.id)\n\n\t\t\t\tawait tx.execute(`UPDATE ${collection} SET ${setClauses} WHERE id = ?`, values)\n\t\t\t}\n\t\t})\n\t}\n\n\tprivate async loadOrGenerateNodeId(): Promise<string> {\n\t\tif (this.configNodeId) {\n\t\t\tif (this.isolation !== 'per-tab') {\n\t\t\t\tawait this.adapter.execute(\n\t\t\t\t\t\"INSERT OR REPLACE INTO _kora_meta (key, value) VALUES ('node_id', ?)\",\n\t\t\t\t\t[this.configNodeId],\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn this.configNodeId\n\t\t}\n\n\t\tif (this.isolation === 'per-tab') {\n\t\t\treturn resolvePerTabNodeId(this.dbName)\n\t\t}\n\n\t\t// Try to load existing node ID\n\t\tconst rows = await this.adapter.query<MetaRow>(\n\t\t\t\"SELECT value FROM _kora_meta WHERE key = 'node_id'\",\n\t\t)\n\t\tif (rows[0]) {\n\t\t\treturn rows[0].value\n\t\t}\n\n\t\t// Generate new node ID\n\t\tconst newNodeId = generateUUIDv7()\n\t\tawait this.adapter.execute(\"INSERT INTO _kora_meta (key, value) VALUES ('node_id', ?)\", [\n\t\t\tnewNodeId,\n\t\t])\n\t\treturn newNodeId\n\t}\n\n\tprivate async loadSequenceNumber(): Promise<number> {\n\t\tconst rows = await this.adapter.query<VersionVectorRow>(\n\t\t\t'SELECT sequence_number FROM _kora_version_vector WHERE node_id = ?',\n\t\t\t[this.nodeId],\n\t\t)\n\t\treturn rows[0]?.sequence_number ?? 0\n\t}\n\n\tprivate async loadVersionVector(): Promise<VersionVector> {\n\t\tconst rows = await this.adapter.query<VersionVectorRow>(\n\t\t\t'SELECT node_id, sequence_number FROM _kora_version_vector',\n\t\t)\n\t\tconst vector = createVersionVector()\n\t\tfor (const row of rows) {\n\t\t\tvector.set(row.node_id, row.sequence_number)\n\t\t}\n\t\treturn vector\n\t}\n\n\tprivate ensureOpen(): void {\n\t\tif (!this.opened) {\n\t\t\tthrow new StoreNotOpenError()\n\t\t}\n\t}\n}\n\n/**\n * Public-facing collection accessor. Provides CRUD + where.\n */\nexport interface CollectionAccessor {\n\tinsert(data: Record<string, unknown>): Promise<import('../types').CollectionRecord>\n\tfindById(id: string): Promise<import('../types').CollectionRecord | null>\n\tupdate(id: string, data: Record<string, unknown>): Promise<import('../types').CollectionRecord>\n\tdelete(id: string): Promise<void>\n\twhere(conditions: Record<string, unknown>): QueryBuilder\n}\n","import { createOperation } from '@korajs/core'\nimport type { Operation } from '@korajs/core'\nimport { RecordNotFoundError } from '../errors'\nimport { serializeRowVersion } from '../lww/row-version'\nimport { buildInsertQuery, buildSoftDeleteQuery } from '../query/sql-builder'\nimport { serializeOperation } from '../serialization/serializer'\nimport { allocateNextSequenceInTransaction } from '../store/sequence-allocator'\nimport type { RawCollectionRow } from '../types'\nimport { resolveCausalDeps } from './resolve-causal-deps'\nimport type { LocalMutationContext } from './types'\n\nexport interface ExecuteDeleteOptions {\n\t/** When true, skip RelationEnforcer (caller already enforced referential integrity). */\n\tskipReferentialEnforcement?: boolean\n\t/** Pre-built delete operation (avoids duplicate sequence allocation). */\n\toperation?: Operation\n}\n\n/**\n * Soft-delete a record and persist the operation log entry atomically.\n * Returns the primary delete operation and any cascaded side-effect operations.\n */\nexport async function executeDelete(\n\tctx: LocalMutationContext,\n\tid: string,\n\toptions?: ExecuteDeleteOptions,\n): Promise<Operation[]> {\n\tconst currentRows = await ctx.adapter.query<RawCollectionRow>(\n\t\t`SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,\n\t\t[id],\n\t)\n\tif (!currentRows[0]) {\n\t\tthrow new RecordNotFoundError(ctx.collection, id)\n\t}\n\n\tconst causalDeps = resolveCausalDeps(ctx)\n\tlet operation = options?.operation\n\tconst cascadedOps: Operation[] = []\n\n\tawait ctx.adapter.transaction(async (tx) => {\n\t\tif (!operation) {\n\t\t\tconst sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId)\n\t\t\toperation = await createOperation(\n\t\t\t\t{\n\t\t\t\t\tnodeId: ctx.nodeId,\n\t\t\t\t\ttype: 'delete',\n\t\t\t\t\tcollection: ctx.collection,\n\t\t\t\t\trecordId: id,\n\t\t\t\t\tdata: null,\n\t\t\t\t\tpreviousData: null,\n\t\t\t\t\tsequenceNumber,\n\t\t\t\t\tcausalDeps,\n\t\t\t\t\tschemaVersion: ctx.schema.version,\n\t\t\t\t},\n\t\t\t\tctx.clock,\n\t\t\t)\n\t\t\tctx.causalTracker?.afterOperation(ctx.collection, operation.id, ctx.inTransaction)\n\t\t}\n\n\t\tif (ctx.relationEnforcer && !options?.skipReferentialEnforcement) {\n\t\t\tconst enforcementResult = await ctx.relationEnforcer.enforceDelete(ctx.collection, id, tx, [\n\t\t\t\toperation.id,\n\t\t\t])\n\t\t\tcascadedOps.push(...enforcementResult.operations)\n\t\t}\n\n\t\tconst version = serializeRowVersion(operation.timestamp)\n\t\tconst deleteQuery = buildSoftDeleteQuery(\n\t\t\tctx.collection,\n\t\t\tid,\n\t\t\toperation.timestamp.wallTime,\n\t\t\tversion,\n\t\t)\n\t\tconst opInsert = buildInsertQuery(\n\t\t\t`_kora_ops_${ctx.collection}`,\n\t\t\tserializeOperation(operation) as unknown as Record<string, unknown>,\n\t\t)\n\n\t\tawait tx.execute(deleteQuery.sql, deleteQuery.params)\n\t\tawait tx.execute(opInsert.sql, opInsert.params)\n\t})\n\n\tif (!operation) {\n\t\tthrow new Error('Delete operation was not created')\n\t}\n\n\tctx.onMutation(ctx.collection, operation)\n\tfor (const cascadedOp of cascadedOps) {\n\t\tctx.onMutation(cascadedOp.collection, cascadedOp)\n\t}\n\n\treturn [operation, ...cascadedOps]\n}\n","import { type HLCTimestamp, HybridLogicalClock } from '@korajs/core'\nimport type { RawCollectionRow } from '../types'\n\n/** Empty _version means legacy rows (compare by wallTime only via fallback). */\nexport const EMPTY_ROW_VERSION = ''\n\n/**\n * Serialize an HLC timestamp for storage in the `_version` column.\n * Lexicographic order matches {@link HybridLogicalClock.compare}.\n */\nexport function serializeRowVersion(timestamp: HLCTimestamp): string {\n\treturn HybridLogicalClock.serialize(timestamp)\n}\n\n/**\n * Read the version stamp stored on a materialized row.\n * Legacy rows without `_version` fall back to wallTime with logical 0.\n */\nexport function rowVersionFromRecord(\n\trow: Pick<RawCollectionRow, '_updated_at' | '_version'>,\n): HLCTimestamp {\n\tconst version = row._version\n\tif (typeof version === 'string' && version.length > 0) {\n\t\treturn HybridLogicalClock.deserialize(version)\n\t}\n\treturn { wallTime: row._updated_at, logical: 0, nodeId: '' }\n}\n\n/**\n * Returns true when `incoming` is strictly newer than the version on `row`.\n */\nexport function isIncomingNewerThanRow(\n\tincoming: HLCTimestamp,\n\trow: Pick<RawCollectionRow, '_updated_at' | '_version'>,\n): boolean {\n\treturn HybridLogicalClock.compare(incoming, rowVersionFromRecord(row)) > 0\n}\n\n/**\n * SQL fragment and params for LWW guards: apply only when row is missing or older.\n * Uses serialized `_version` for total order (matches HLC compare).\n */\nexport function lwwVersionWhereClause(remoteVersion: string): { sql: string; params: string[] } {\n\treturn {\n\t\tsql: '(_version = ? OR _version < ?)',\n\t\tparams: [EMPTY_ROW_VERSION, remoteVersion],\n\t}\n}\n","import type { CollectionDefinition, FieldDescriptor } from '@korajs/core'\nimport { QueryError } from '../errors'\nimport { lwwVersionWhereClause } from '../lww/row-version'\nimport type { QueryDescriptor, WhereOperators } from '../types'\n\n/**\n * Result of building a SQL query: the parameterized SQL string and its bound values.\n */\nexport interface SqlQuery {\n\tsql: string\n\tparams: unknown[]\n}\n\n/**\n * Build a SELECT query from a QueryDescriptor.\n * Automatically adds `WHERE _deleted = 0` to exclude soft-deleted records.\n *\n * @param descriptor - The query descriptor\n * @param fields - The field descriptors from the collection schema\n * @returns A parameterized SQL query\n */\nexport function buildSelectQuery(\n\tdescriptor: QueryDescriptor,\n\tfields: Record<string, FieldDescriptor>,\n): SqlQuery {\n\tconst params: unknown[] = []\n\tconst parts = [`SELECT * FROM ${descriptor.collection}`]\n\n\tconst whereClause = buildWhereClauseParts(descriptor.where, fields, params)\n\t// Always filter out soft-deleted records\n\tconst deletedFilter = '_deleted = 0'\n\tif (whereClause) {\n\t\tparts.push(`WHERE ${deletedFilter} AND ${whereClause}`)\n\t} else {\n\t\tparts.push(`WHERE ${deletedFilter}`)\n\t}\n\n\tif (descriptor.orderBy.length > 0) {\n\t\tconst orderParts = descriptor.orderBy.map((o) => {\n\t\t\tvalidateFieldName(o.field, fields)\n\t\t\treturn `${o.field} ${o.direction.toUpperCase()}`\n\t\t})\n\t\tparts.push(`ORDER BY ${orderParts.join(', ')}`)\n\t}\n\n\tif (descriptor.limit !== undefined) {\n\t\tparts.push(`LIMIT ${descriptor.limit}`)\n\t}\n\n\tif (descriptor.offset !== undefined) {\n\t\tparts.push(`OFFSET ${descriptor.offset}`)\n\t}\n\n\treturn { sql: parts.join(' '), params }\n}\n\n/**\n * Build a COUNT query from a QueryDescriptor.\n * Automatically adds `WHERE _deleted = 0`.\n *\n * @param descriptor - The query descriptor\n * @param fields - The field descriptors from the collection schema\n * @returns A parameterized SQL query that returns { count: number }\n */\nexport function buildCountQuery(\n\tdescriptor: QueryDescriptor,\n\tfields: Record<string, FieldDescriptor>,\n): SqlQuery {\n\tconst params: unknown[] = []\n\tconst parts = [`SELECT COUNT(*) as count FROM ${descriptor.collection}`]\n\n\tconst whereClause = buildWhereClauseParts(descriptor.where, fields, params)\n\tconst deletedFilter = '_deleted = 0'\n\tif (whereClause) {\n\t\tparts.push(`WHERE ${deletedFilter} AND ${whereClause}`)\n\t} else {\n\t\tparts.push(`WHERE ${deletedFilter}`)\n\t}\n\n\treturn { sql: parts.join(' '), params }\n}\n\n/**\n * Build an INSERT query for a collection record.\n *\n * @param collection - The collection name\n * @param record - The record data (already serialized with id, _created_at, _updated_at)\n * @returns A parameterized SQL query\n */\nexport function buildInsertQuery(collection: string, record: Record<string, unknown>): SqlQuery {\n\tconst columns = Object.keys(record)\n\tconst placeholders = columns.map(() => '?')\n\tconst params = Object.values(record)\n\n\tconst sql = `INSERT INTO ${collection} (${columns.join(', ')}) VALUES (${placeholders.join(', ')})`\n\treturn { sql, params }\n}\n\n/**\n * Build an UPDATE query for a collection record.\n *\n * @param collection - The collection name\n * @param id - The record ID\n * @param changes - The fields to update (already serialized)\n * @returns A parameterized SQL query\n */\nexport function buildUpdateQuery(\n\tcollection: string,\n\tid: string,\n\tchanges: Record<string, unknown>,\n): SqlQuery {\n\tconst setClauses = Object.keys(changes).map((col) => `${col} = ?`)\n\tconst params = [...Object.values(changes), id]\n\n\tconst sql = `UPDATE ${collection} SET ${setClauses.join(', ')} WHERE id = ?`\n\treturn { sql, params }\n}\n\n/**\n * Build a soft-delete query (SET _deleted = 1).\n *\n * @param collection - The collection name\n * @param id - The record ID\n * @param updatedAt - The timestamp to set on _updated_at\n * @returns A parameterized SQL query\n */\nexport function buildSoftDeleteQuery(\n\tcollection: string,\n\tid: string,\n\tupdatedAt: number,\n\tversion?: string,\n): SqlQuery {\n\tif (version !== undefined) {\n\t\treturn {\n\t\t\tsql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ?`,\n\t\t\tparams: [updatedAt, version, id],\n\t\t}\n\t}\n\treturn {\n\t\tsql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ? WHERE id = ?`,\n\t\tparams: [updatedAt, id],\n\t}\n}\n\n/**\n * Build an UPDATE that applies only when the row is missing or older than `remoteVersion`\n * (serialized HLC). Prevents stale remote sync from overwriting newer local materialized state.\n */\nexport function buildLwwUpdateQuery(\n\tcollection: string,\n\tid: string,\n\tchanges: Record<string, unknown>,\n\tremoteVersion: string,\n): SqlQuery {\n\tconst setClauses = Object.keys(changes).map((col) => `${col} = ?`)\n\tconst lww = lwwVersionWhereClause(remoteVersion)\n\tconst sql = `UPDATE ${collection} SET ${setClauses.join(', ')} WHERE id = ? AND ${lww.sql}`\n\tconst params = [...Object.values(changes), id, ...lww.params]\n\treturn { sql, params }\n}\n\n/**\n * Build a soft-delete that applies only when the row is older than `remoteVersion`.\n */\nexport function buildLwwSoftDeleteQuery(\n\tcollection: string,\n\tid: string,\n\tupdatedAt: number,\n\tversion: string,\n): SqlQuery {\n\tconst lww = lwwVersionWhereClause(version)\n\treturn {\n\t\tsql: `UPDATE ${collection} SET _deleted = 1, _updated_at = ?, _version = ? WHERE id = ? AND ${lww.sql}`,\n\t\tparams: [updatedAt, version, id, ...lww.params],\n\t}\n}\n\n/**\n * Build a WHERE clause from conditions, validating field names against the schema.\n *\n * @param where - The where conditions\n * @param fields - The field descriptors from the collection schema\n * @returns The SQL WHERE clause string and params, or null if no conditions\n */\nexport function buildWhereClause(\n\twhere: Record<string, unknown>,\n\tfields: Record<string, FieldDescriptor>,\n): SqlQuery | null {\n\tconst params: unknown[] = []\n\tconst result = buildWhereClauseParts(where, fields, params)\n\tif (!result) return null\n\treturn { sql: result, params }\n}\n\n// --- Internal helpers ---\n\nconst VALID_OPERATORS = new Set(['$eq', '$ne', '$gt', '$gte', '$lt', '$lte', '$in'])\n\nfunction buildWhereClauseParts(\n\twhere: Record<string, unknown>,\n\tfields: Record<string, FieldDescriptor>,\n\tparams: unknown[],\n): string | null {\n\tconst conditions: string[] = []\n\n\tfor (const [fieldName, value] of Object.entries(where)) {\n\t\tvalidateFieldName(fieldName, fields)\n\t\tconst descriptor = fields[fieldName]\n\n\t\tif (value !== null && typeof value === 'object' && !Array.isArray(value)) {\n\t\t\t// Operator object: { $gt: 5, $lt: 10 }\n\t\t\tconst ops = value as WhereOperators\n\t\t\tfor (const [op, opValue] of Object.entries(ops)) {\n\t\t\t\tif (!VALID_OPERATORS.has(op)) {\n\t\t\t\t\tthrow new QueryError(`Unknown operator \"${op}\" on field \"${fieldName}\"`, {\n\t\t\t\t\t\tfield: fieldName,\n\t\t\t\t\t\toperator: op,\n\t\t\t\t\t\tvalidOperators: [...VALID_OPERATORS],\n\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t\tconditions.push(buildOperatorCondition(fieldName, op, opValue, descriptor, params))\n\t\t\t}\n\t\t} else {\n\t\t\t// Shorthand: { completed: false } means { completed: { $eq: false } }\n\t\t\tconditions.push(buildOperatorCondition(fieldName, '$eq', value, descriptor, params))\n\t\t}\n\t}\n\n\tif (conditions.length === 0) return null\n\treturn conditions.join(' AND ')\n}\n\nfunction buildOperatorCondition(\n\tfieldName: string,\n\toperator: string,\n\tvalue: unknown,\n\tdescriptor: FieldDescriptor | undefined,\n\tparams: unknown[],\n): string {\n\t// Serialize boolean values to 0/1 for SQL comparison\n\tconst sqlValue =\n\t\tdescriptor?.kind === 'boolean' && typeof value === 'boolean' ? (value ? 1 : 0) : value\n\n\tswitch (operator) {\n\t\tcase '$eq':\n\t\t\tif (sqlValue === null) {\n\t\t\t\treturn `${fieldName} IS NULL`\n\t\t\t}\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} = ?`\n\t\tcase '$ne':\n\t\t\tif (sqlValue === null) {\n\t\t\t\treturn `${fieldName} IS NOT NULL`\n\t\t\t}\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} != ?`\n\t\tcase '$gt':\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} > ?`\n\t\tcase '$gte':\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} >= ?`\n\t\tcase '$lt':\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} < ?`\n\t\tcase '$lte':\n\t\t\tparams.push(sqlValue)\n\t\t\treturn `${fieldName} <= ?`\n\t\tcase '$in': {\n\t\t\tif (!Array.isArray(sqlValue)) {\n\t\t\t\tthrow new QueryError(`$in operator requires an array value for field \"${fieldName}\"`, {\n\t\t\t\t\tfield: fieldName,\n\t\t\t\t\treceived: typeof sqlValue,\n\t\t\t\t})\n\t\t\t}\n\t\t\tconst placeholders = sqlValue.map(() => '?')\n\t\t\tfor (const item of sqlValue) {\n\t\t\t\tparams.push(\n\t\t\t\t\tdescriptor?.kind === 'boolean' && typeof item === 'boolean' ? (item ? 1 : 0) : item,\n\t\t\t\t)\n\t\t\t}\n\t\t\treturn `${fieldName} IN (${placeholders.join(', ')})`\n\t\t}\n\t\tdefault:\n\t\t\tthrow new QueryError(`Unknown operator \"${operator}\"`, { operator })\n\t}\n}\n\nfunction validateFieldName(fieldName: string, fields: Record<string, FieldDescriptor>): void {\n\t// Allow schema fields plus metadata fields that map to query-able columns\n\tconst allowedFields = new Set([\n\t\t...Object.keys(fields),\n\t\t'id',\n\t\t'createdAt',\n\t\t'updatedAt',\n\t\t'_created_at',\n\t\t'_updated_at',\n\t])\n\tif (!allowedFields.has(fieldName)) {\n\t\tthrow new QueryError(\n\t\t\t`Unknown field \"${fieldName}\" in query. Available fields: ${[...allowedFields].join(', ')}`,\n\t\t\t{ field: fieldName },\n\t\t)\n\t}\n}\n","import type { StorageAdapter, Transaction } from '../types'\n\ninterface SequenceRow {\n\tsequence_number: number\n}\n\n/**\n * Atomically increment and return the next sequence number for a node.\n * Uses SQLite UPSERT + RETURNING so concurrent tabs do not reuse sequence slots.\n */\nexport async function allocateNextSequenceNumber(\n\tadapter: StorageAdapter,\n\tnodeId: string,\n): Promise<number> {\n\tconst rows = await adapter.query<SequenceRow>(\n\t\t`INSERT INTO _kora_version_vector (node_id, sequence_number) VALUES (?, 1)\n ON CONFLICT(node_id) DO UPDATE SET sequence_number = sequence_number + 1\n RETURNING sequence_number`,\n\t\t[nodeId],\n\t)\n\tconst seq = rows[0]?.sequence_number\n\tif (seq === undefined) {\n\t\tthrow new Error(`Failed to allocate sequence number for node \"${nodeId}\"`)\n\t}\n\treturn seq\n}\n\n/**\n * Allocate the next sequence inside an existing storage transaction.\n */\nexport async function allocateNextSequenceInTransaction(\n\ttx: Transaction,\n\tnodeId: string,\n): Promise<number> {\n\tconst rows = await tx.query<SequenceRow>(\n\t\t`INSERT INTO _kora_version_vector (node_id, sequence_number) VALUES (?, 1)\n ON CONFLICT(node_id) DO UPDATE SET sequence_number = sequence_number + 1\n RETURNING sequence_number`,\n\t\t[nodeId],\n\t)\n\tconst seq = rows[0]?.sequence_number\n\tif (seq === undefined) {\n\t\tthrow new Error(`Failed to allocate sequence number for node \"${nodeId}\" in transaction`)\n\t}\n\treturn seq\n}\n\n/**\n * Read the current sequence for a node without incrementing.\n */\nexport async function readSequenceNumber(adapter: StorageAdapter, nodeId: string): Promise<number> {\n\tconst rows = await adapter.query<SequenceRow>(\n\t\t'SELECT sequence_number FROM _kora_version_vector WHERE node_id = ?',\n\t\t[nodeId],\n\t)\n\treturn rows[0]?.sequence_number ?? 0\n}\n","import type { LocalMutationContext } from './types'\n\n/**\n * Merge explicit parent deps (e.g. referential cascade) with tracker-derived deps.\n */\nexport function resolveCausalDeps(ctx: LocalMutationContext): string[] {\n\tconst merged: string[] = []\n\tfor (const id of ctx.extraCausalDeps ?? []) {\n\t\tif (!merged.includes(id)) {\n\t\t\tmerged.push(id)\n\t\t}\n\t}\n\tfor (const id of ctx.causalTracker?.nextCausalDeps(ctx.collection, ctx.inTransaction) ?? []) {\n\t\tif (!merged.includes(id)) {\n\t\t\tmerged.push(id)\n\t\t}\n\t}\n\treturn merged\n}\n","import { createOperation, generateUUIDv7, validateRecord } from '@korajs/core'\nimport { serializeRowVersion } from '../lww/row-version'\nimport { buildInsertQuery } from '../query/sql-builder'\nimport { serializeOperation, serializeRecord } from '../serialization/serializer'\nimport { allocateNextSequenceInTransaction } from '../store/sequence-allocator'\nimport type { CollectionRecord } from '../types'\nimport { resolveCausalDeps } from './resolve-causal-deps'\nimport type { LocalMutationContext } from './types'\n\n/**\n * Insert a record and persist the operation log entry atomically.\n */\nexport async function executeInsert(\n\tctx: LocalMutationContext,\n\tdata: Record<string, unknown>,\n): Promise<CollectionRecord> {\n\tconst validated = validateRecord(ctx.collection, ctx.definition, data, 'insert')\n\tconst recordId = generateUUIDv7()\n\n\tfor (const [fieldName, descriptor] of Object.entries(ctx.definition.fields)) {\n\t\tif (descriptor.auto && descriptor.kind === 'timestamp') {\n\t\t\tvalidated[fieldName] = Date.now()\n\t\t}\n\t}\n\n\tconst causalDeps = resolveCausalDeps(ctx)\n\n\tlet operation!: Awaited<ReturnType<typeof createOperation>>\n\tlet record!: Record<string, unknown>\n\n\tawait ctx.adapter.transaction(async (tx) => {\n\t\tconst sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId)\n\t\toperation = await createOperation(\n\t\t\t{\n\t\t\t\tnodeId: ctx.nodeId,\n\t\t\t\ttype: 'insert',\n\t\t\t\tcollection: ctx.collection,\n\t\t\t\trecordId,\n\t\t\t\tdata: { ...validated },\n\t\t\t\tpreviousData: null,\n\t\t\t\tsequenceNumber,\n\t\t\t\tcausalDeps,\n\t\t\t\tschemaVersion: ctx.schema.version,\n\t\t\t},\n\t\t\tctx.clock,\n\t\t)\n\t\tctx.causalTracker?.afterOperation(ctx.collection, operation.id, ctx.inTransaction)\n\n\t\tconst serializedData = serializeRecord(validated, ctx.definition.fields)\n\t\tconst version = serializeRowVersion(operation.timestamp)\n\t\trecord = {\n\t\t\tid: recordId,\n\t\t\t...serializedData,\n\t\t\t_created_at: operation.timestamp.wallTime,\n\t\t\t_updated_at: operation.timestamp.wallTime,\n\t\t\t_version: version,\n\t\t}\n\n\t\tconst builtInsert = buildInsertQuery(ctx.collection, record)\n\t\tconst opInsert = buildInsertQuery(\n\t\t\t`_kora_ops_${ctx.collection}`,\n\t\t\tserializeOperation(operation) as unknown as Record<string, unknown>,\n\t\t)\n\n\t\tawait tx.execute(builtInsert.sql, builtInsert.params)\n\t\tawait tx.execute(opInsert.sql, opInsert.params)\n\t})\n\n\tctx.onMutation(ctx.collection, operation)\n\n\treturn {\n\t\tid: recordId,\n\t\t...validated,\n\t\tcreatedAt: operation.timestamp.wallTime,\n\t\tupdatedAt: operation.timestamp.wallTime,\n\t}\n}\n","import type { AtomicOp } from '@korajs/core'\nimport {\n\tcreateOperation,\n\tisAtomicOp,\n\tresolveAtomicOp,\n\ttoAtomicOp,\n\tvalidateRecord,\n} from '@korajs/core'\nimport { RecordNotFoundError } from '../errors'\nimport { serializeRowVersion } from '../lww/row-version'\nimport { buildInsertQuery, buildUpdateQuery } from '../query/sql-builder'\nimport { deserializeRecord, serializeOperation, serializeRecord } from '../serialization/serializer'\nimport { validateUpdateStateMachine } from '../state-machine/state-validator'\nimport { allocateNextSequenceInTransaction } from '../store/sequence-allocator'\nimport type { CollectionRecord, RawCollectionRow } from '../types'\nimport { resolveCausalDeps } from './resolve-causal-deps'\nimport type { LocalMutationContext } from './types'\n\n/**\n * Update a record and persist the operation log entry atomically.\n */\nexport async function executeUpdate(\n\tctx: LocalMutationContext,\n\tid: string,\n\tdata: Record<string, unknown>,\n): Promise<CollectionRecord> {\n\tconst currentRows = await ctx.adapter.query<RawCollectionRow>(\n\t\t`SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,\n\t\t[id],\n\t)\n\tconst currentRow = currentRows[0]\n\tif (!currentRow) {\n\t\tthrow new RecordNotFoundError(ctx.collection, id)\n\t}\n\n\tlet validated = validateRecord(ctx.collection, ctx.definition, data, 'update')\n\tconst currentRecord = deserializeRecord(currentRow, ctx.definition.fields)\n\tvalidated = validateUpdateStateMachine(\n\t\tctx.collection,\n\t\tid,\n\t\tctx.definition,\n\t\tcurrentRecord,\n\t\tvalidated,\n\t)\n\n\tif (Object.keys(validated).length === 0) {\n\t\treturn { ...currentRecord }\n\t}\n\n\tconst previousData: Record<string, unknown> = {}\n\tconst resolvedData: Record<string, unknown> = {}\n\tconst atomicOps: Record<string, AtomicOp> = {}\n\n\tfor (const key of Object.keys(validated)) {\n\t\tconst value = validated[key]\n\t\tpreviousData[key] = currentRecord[key]\n\n\t\tif (isAtomicOp(value)) {\n\t\t\tresolvedData[key] = resolveAtomicOp(currentRecord[key], value)\n\t\t\tatomicOps[key] = toAtomicOp(value)\n\t\t} else {\n\t\t\tresolvedData[key] = value\n\t\t}\n\t}\n\n\tconst hasAtomicOps = Object.keys(atomicOps).length > 0\n\tconst causalDeps = resolveCausalDeps(ctx)\n\tlet operation!: Awaited<ReturnType<typeof createOperation>>\n\n\tawait ctx.adapter.transaction(async (tx) => {\n\t\tconst sequenceNumber = await allocateNextSequenceInTransaction(tx, ctx.nodeId)\n\t\toperation = await createOperation(\n\t\t\t{\n\t\t\t\tnodeId: ctx.nodeId,\n\t\t\t\ttype: 'update',\n\t\t\t\tcollection: ctx.collection,\n\t\t\t\trecordId: id,\n\t\t\t\tdata: { ...resolvedData },\n\t\t\t\tpreviousData,\n\t\t\t\tsequenceNumber,\n\t\t\t\tcausalDeps,\n\t\t\t\tschemaVersion: ctx.schema.version,\n\t\t\t\t...(hasAtomicOps ? { atomicOps } : {}),\n\t\t\t},\n\t\t\tctx.clock,\n\t\t)\n\t\tctx.causalTracker?.afterOperation(ctx.collection, operation.id, ctx.inTransaction)\n\n\t\tconst serializedChanges = serializeRecord(resolvedData, ctx.definition.fields)\n\t\tconst version = serializeRowVersion(operation.timestamp)\n\t\tconst updateQuery = buildUpdateQuery(ctx.collection, id, {\n\t\t\t...serializedChanges,\n\t\t\t_updated_at: operation.timestamp.wallTime,\n\t\t\t_version: version,\n\t\t})\n\t\tconst opInsert = buildInsertQuery(\n\t\t\t`_kora_ops_${ctx.collection}`,\n\t\t\tserializeOperation(operation) as unknown as Record<string, unknown>,\n\t\t)\n\n\t\tawait tx.execute(updateQuery.sql, updateQuery.params)\n\t\tawait tx.execute(opInsert.sql, opInsert.params)\n\t})\n\n\tctx.onMutation(ctx.collection, operation)\n\n\tconst rows = await ctx.adapter.query<RawCollectionRow>(\n\t\t`SELECT * FROM ${ctx.collection} WHERE id = ? AND _deleted = 0`,\n\t\t[id],\n\t)\n\tconst row = rows[0]\n\tif (!row) {\n\t\tthrow new RecordNotFoundError(ctx.collection, id)\n\t}\n\treturn deserializeRecord(row, ctx.definition.fields)\n}\n","import { KoraError, validateTransition } from '@korajs/core'\nimport type {\n\tCollectionDefinition,\n\tStateMachineConstraint,\n\tStateMachineDefinition,\n} from '@korajs/core'\n\n/**\n * Error thrown when a local mutation attempts an invalid state transition.\n *\n * Contains enough context to debug without reproduction:\n * the collection, record, field, current state, attempted state, and allowed transitions.\n */\nexport class InvalidStateTransitionError extends KoraError {\n\tconstructor(\n\t\tpublic readonly collection: string,\n\t\tpublic readonly recordId: string,\n\t\tpublic readonly field: string,\n\t\tpublic readonly fromState: string,\n\t\tpublic readonly toState: string,\n\t\tpublic readonly allowedStates: string[],\n\t) {\n\t\tsuper(\n\t\t\t`Invalid state transition in collection \"${collection}\": ` +\n\t\t\t\t`cannot transition field \"${field}\" from \"${fromState}\" to \"${toState}\". ` +\n\t\t\t\t`Allowed transitions from \"${fromState}\": ${allowedStates.length > 0 ? allowedStates.join(', ') : '(none -- terminal state)'}`,\n\t\t\t'INVALID_STATE_TRANSITION',\n\t\t\t{ collection, recordId, field, fromState, toState, allowedStates },\n\t\t)\n\t\tthis.name = 'InvalidStateTransitionError'\n\t}\n}\n\n/**\n * Validates a state machine transition for a local mutation (insert or update).\n *\n * For inserts: validates that the initial state (from the data or the default value)\n * is a known state in the state machine.\n *\n * For updates: looks up the current state field value and checks whether\n * the transition to the new value is allowed.\n *\n * @param collectionName - Name of the collection\n * @param recordId - The record being mutated\n * @param stateMachine - The state machine definition\n * @param currentState - The current value of the state field (null for inserts)\n * @param newState - The new value being set for the state field\n * @returns An object indicating whether the transition is valid, and if not, the allowed states.\n * When `onInvalidTransition` is 'last-valid-state', callers should suppress the field update\n * rather than throwing.\n */\nexport function validateStateTransition(\n\tcollectionName: string,\n\trecordId: string,\n\tstateMachine: StateMachineDefinition,\n\tcurrentState: string | null,\n\tnewState: string,\n): { valid: boolean; allowedStates: string[] } {\n\t// For inserts, any valid enum value is acceptable as the initial state\n\t// (schema validation already ensures the value is a valid enum value)\n\tif (currentState === null) {\n\t\treturn { valid: true, allowedStates: [] }\n\t}\n\n\t// Same-state transitions are always valid (idempotent updates)\n\tif (currentState === newState) {\n\t\treturn { valid: true, allowedStates: stateMachine.transitions[currentState] ?? [] }\n\t}\n\n\tconst constraint: StateMachineConstraint = {\n\t\tfield: stateMachine.field,\n\t\tcollection: collectionName,\n\t\ttransitions: stateMachine.transitions,\n\t}\n\tconst transitionResult = validateTransition(constraint, currentState, newState)\n\tif (transitionResult.valid) {\n\t\treturn { valid: true, allowedStates: transitionResult.allowedTargets }\n\t}\n\n\tconst allowedStates = transitionResult.allowedTargets\n\n\tif (stateMachine.onInvalidTransition === 'reject') {\n\t\tthrow new InvalidStateTransitionError(\n\t\t\tcollectionName,\n\t\t\trecordId,\n\t\t\tstateMachine.field,\n\t\t\tcurrentState,\n\t\t\tnewState,\n\t\t\tallowedStates,\n\t\t)\n\t}\n\n\t// 'last-valid-state': return invalid so the caller can suppress the field update\n\treturn { valid: false, allowedStates }\n}\n\n/**\n * Checks whether a collection update data object contains a change to the state machine field,\n * and if so, validates the transition.\n *\n * If the state field is not in the update data, returns the data unchanged.\n * If the transition is invalid and mode is 'last-valid-state', removes the field from the data.\n * If the transition is invalid and mode is 'reject', throws InvalidStateTransitionError.\n *\n * @param collectionName - Name of the collection\n * @param recordId - The record being updated\n * @param collectionDef - The collection definition from the schema\n * @param currentRecord - The current record data (must include the state field)\n * @param updateData - The partial update data\n * @returns The update data, potentially with the state field removed if invalid and mode is 'last-valid-state'\n */\nexport function validateUpdateStateMachine(\n\tcollectionName: string,\n\trecordId: string,\n\tcollectionDef: CollectionDefinition,\n\tcurrentRecord: Record<string, unknown>,\n\tupdateData: Record<string, unknown>,\n): Record<string, unknown> {\n\tconst stateMachine = collectionDef.stateMachine\n\tif (stateMachine === undefined) {\n\t\treturn updateData\n\t}\n\n\tconst stateField = stateMachine.field\n\tif (!(stateField in updateData)) {\n\t\t// State field not being changed -- no validation needed\n\t\treturn updateData\n\t}\n\n\tconst currentState = currentRecord[stateField]\n\tconst newState = updateData[stateField]\n\n\t// Both values must be strings for state machine validation\n\tif (typeof currentState !== 'string' || typeof newState !== 'string') {\n\t\treturn updateData\n\t}\n\n\tconst result = validateStateTransition(\n\t\tcollectionName,\n\t\trecordId,\n\t\tstateMachine,\n\t\tcurrentState,\n\t\tnewState,\n\t)\n\n\tif (result.valid) {\n\t\treturn updateData\n\t}\n\n\t// Mode is 'last-valid-state': silently remove the state field from the update\n\tconst filtered = { ...updateData }\n\tdelete filtered[stateField]\n\n\t// If no fields remain after removing the state field, the update becomes a no-op.\n\t// Return the empty object -- the caller can decide whether to proceed.\n\treturn filtered\n}\n","import type {\n\tCausalTracker,\n\tCollectionDefinition,\n\tHybridLogicalClock,\n\tOperation,\n\tSchemaDefinition,\n} from '@korajs/core'\nimport { executeDelete } from '../mutations/execute-delete'\nimport { executeInsert } from '../mutations/execute-insert'\nimport { executeUpdate } from '../mutations/execute-update'\nimport type { LocalMutationContext } from '../mutations/types'\nimport type { RelationEnforcer } from '../relations/relation-enforcer'\nimport { deserializeRecord } from '../serialization/serializer'\nimport type {\n\tCollectionRecord,\n\tLocalMutationHandler,\n\tRawCollectionRow,\n\tStorageAdapter,\n} from '../types'\n\n/**\n * Callback invoked after a mutation so the Store can notify subscriptions.\n */\nexport type MutationCallback = (collection: string, operation: Operation) => void\n\n/**\n * Collection provides CRUD operations on a single schema collection.\n * Delegates to {@link LocalMutationHandler} when configured (unified apply pipeline),\n * otherwise uses shared mutation executors directly.\n */\nexport class Collection {\n\tconstructor(\n\t\tprivate readonly name: string,\n\t\tprivate readonly definition: CollectionDefinition,\n\t\tprivate readonly schema: SchemaDefinition,\n\t\tprivate readonly adapter: StorageAdapter,\n\t\tprivate readonly clock: HybridLogicalClock,\n\t\tprivate readonly nodeId: string,\n\t\tprivate readonly allocateSequenceNumber: () => Promise<number>,\n\t\tprivate readonly onMutation: MutationCallback,\n\t\tprivate readonly relationEnforcer: RelationEnforcer | null,\n\t\tprivate mutationHandler: LocalMutationHandler | null,\n\t\tprivate readonly causalTracker: CausalTracker | null,\n\t) {}\n\n\tprivate mutationContext(): LocalMutationContext {\n\t\treturn {\n\t\t\tcollection: this.name,\n\t\t\tdefinition: this.definition,\n\t\t\tschema: this.schema,\n\t\t\tadapter: this.adapter,\n\t\t\tclock: this.clock,\n\t\t\tnodeId: this.nodeId,\n\t\t\tallocateSequenceNumber: this.allocateSequenceNumber,\n\t\t\tonMutation: this.onMutation,\n\t\t\trelationEnforcer: this.relationEnforcer,\n\t\t\tcausalTracker: this.causalTracker,\n\t\t\tinTransaction: false,\n\t\t}\n\t}\n\n\tasync insert(data: Record<string, unknown>): Promise<CollectionRecord> {\n\t\tif (this.mutationHandler) {\n\t\t\treturn this.mutationHandler.insert(this.name, data)\n\t\t}\n\t\treturn executeInsert(this.mutationContext(), data)\n\t}\n\n\tasync findById(id: string): Promise<CollectionRecord | null> {\n\t\tconst rows = await this.adapter.query<RawCollectionRow>(\n\t\t\t`SELECT * FROM ${this.name} WHERE id = ? AND _deleted = 0`,\n\t\t\t[id],\n\t\t)\n\t\tconst row = rows[0]\n\t\tif (!row) return null\n\t\treturn deserializeRecord(row, this.definition.fields)\n\t}\n\n\tasync update(id: string, data: Record<string, unknown>): Promise<CollectionRecord> {\n\t\tif (this.mutationHandler) {\n\t\t\treturn this.mutationHandler.update(this.name, id, data)\n\t\t}\n\t\treturn executeUpdate(this.mutationContext(), id, data)\n\t}\n\n\tasync delete(id: string): Promise<void> {\n\t\tif (this.mutationHandler) {\n\t\t\tawait this.mutationHandler.delete(this.name, id)\n\t\t\treturn\n\t\t}\n\t\tawait executeDelete(this.mutationContext(), id)\n\t}\n\n\tgetName(): string {\n\t\treturn this.name\n\t}\n\n\tgetDefinition(): CollectionDefinition {\n\t\treturn this.definition\n\t}\n\n\t/** Replace the mutation handler (e.g. after ApplyPipeline is wired in createApp). */\n\tsetMutationHandler(handler: LocalMutationHandler | null): void {\n\t\tthis.mutationHandler = handler\n\t}\n}\n","import type { CollectionDefinition, SchemaDefinition } from '@korajs/core'\nimport { deserializeRecord } from '../serialization/serializer'\nimport type { SubscriptionManager } from '../subscription/subscription-manager'\nimport type {\n\tCollectionRecord,\n\tOrderByDirection,\n\tQueryDescriptor,\n\tRawCollectionRow,\n\tStorageAdapter,\n\tSubscriptionCallback,\n\tWhereClause,\n} from '../types'\nimport { pluralize, singularize } from './pluralize'\nimport { buildCountQuery, buildSelectQuery } from './sql-builder'\n\n/**\n * Fluent query builder for constructing and executing collection queries.\n * Supports where, orderBy, limit, offset, include, exec, count, and subscribe.\n *\n * The generic parameter `T` defaults to `CollectionRecord` for backward compatibility\n * but can be narrowed to a specific record type for full type inference.\n *\n * @example\n * ```typescript\n * const todos = await app.todos\n * .where({ completed: false })\n * .orderBy('createdAt', 'desc')\n * .limit(10)\n * .exec()\n * ```\n */\nexport class QueryBuilder<T = CollectionRecord> {\n\tprivate descriptor: QueryDescriptor\n\n\tconstructor(\n\t\tprivate readonly collectionName: string,\n\t\tprivate readonly definition: CollectionDefinition,\n\t\tprivate readonly adapter: StorageAdapter,\n\t\tprivate readonly subscriptionManager: SubscriptionManager,\n\t\tinitialWhere: WhereClause = {},\n\t\tprivate readonly schema?: SchemaDefinition,\n\t) {\n\t\tthis.descriptor = {\n\t\t\tcollection: collectionName,\n\t\t\twhere: { ...initialWhere },\n\t\t\torderBy: [],\n\t\t}\n\t}\n\n\t/**\n\t * Add WHERE conditions (AND semantics, merged with existing conditions).\n\t */\n\twhere(conditions: WhereClause): QueryBuilder<T> {\n\t\tconst clone = this.clone()\n\t\tclone.descriptor = {\n\t\t\t...clone.descriptor,\n\t\t\twhere: { ...clone.descriptor.where, ...conditions },\n\t\t}\n\t\treturn clone\n\t}\n\n\t/**\n\t * Add ORDER BY clause.\n\t */\n\torderBy(field: string, direction: OrderByDirection = 'asc'): QueryBuilder<T> {\n\t\tconst clone = this.clone()\n\t\tclone.descriptor = {\n\t\t\t...clone.descriptor,\n\t\t\torderBy: [...clone.descriptor.orderBy, { field, direction }],\n\t\t}\n\t\treturn clone\n\t}\n\n\t/**\n\t * Set result limit.\n\t */\n\tlimit(n: number): QueryBuilder<T> {\n\t\tconst clone = this.clone()\n\t\tclone.descriptor = { ...clone.descriptor, limit: n }\n\t\treturn clone\n\t}\n\n\t/**\n\t * Set result offset.\n\t */\n\toffset(n: number): QueryBuilder<T> {\n\t\tconst clone = this.clone()\n\t\tclone.descriptor = { ...clone.descriptor, offset: n }\n\t\treturn clone\n\t}\n\n\t/**\n\t * Include related records in the query results.\n\t * Follows relations defined in the schema to batch-fetch related data.\n\t *\n\t * @param targets - Relation target names (collection names or relation names)\n\t * @returns A new QueryBuilder with include targets added\n\t *\n\t * @example\n\t * ```typescript\n\t * const todosWithProject = await app.todos\n\t * .where({ completed: false })\n\t * .include('project')\n\t * .exec()\n\t * ```\n\t */\n\tinclude(...targets: string[]): QueryBuilder<T> {\n\t\tconst clone = this.clone()\n\t\tconst existing = clone.descriptor.include ?? []\n\t\tclone.descriptor = {\n\t\t\t...clone.descriptor,\n\t\t\tinclude: [...existing, ...targets],\n\t\t}\n\t\treturn clone\n\t}\n\n\t/**\n\t * Execute the query and return results.\n\t */\n\tasync exec(): Promise<T[]> {\n\t\tconst { sql, params } = buildSelectQuery(this.descriptor, this.definition.fields)\n\t\tconst rows = await this.adapter.query<RawCollectionRow>(sql, params)\n\t\tconst records = rows.map((row) => deserializeRecord(row, this.definition.fields))\n\n\t\t// Resolve includes if any\n\t\tif (this.descriptor.include && this.descriptor.include.length > 0 && this.schema) {\n\t\t\tawait this.resolveIncludes(records)\n\t\t}\n\n\t\treturn records as T[]\n\t}\n\n\t/**\n\t * Execute a COUNT query and return the count.\n\t */\n\tasync count(): Promise<number> {\n\t\tconst { sql, params } = buildCountQuery(this.descriptor, this.definition.fields)\n\t\tconst rows = await this.adapter.query<{ count: number }>(sql, params)\n\t\treturn rows[0]?.count ?? 0\n\t}\n\n\t/**\n\t * Subscribe to query results. Callback is called immediately with current results,\n\t * then again whenever the results change due to mutations.\n\t *\n\t * @returns An unsubscribe function\n\t */\n\tsubscribe(callback: SubscriptionCallback<T>): () => void {\n\t\tconst executeFn = () => this.exec()\n\n\t\t// Resolve includeCollections for subscription tracking\n\t\tconst descriptorCopy = { ...this.descriptor }\n\t\tif (descriptorCopy.include && descriptorCopy.include.length > 0 && this.schema) {\n\t\t\tdescriptorCopy.includeCollections = this.resolveIncludeCollections(descriptorCopy.include)\n\t\t}\n\n\t\t// Use registerAndFetch to execute immediately, set lastResults,\n\t\t// and call callback — ensuring subsequent flushes diff correctly.\n\t\treturn this.subscriptionManager.registerAndFetch(\n\t\t\tdescriptorCopy,\n\t\t\tcallback as SubscriptionCallback<CollectionRecord>,\n\t\t\texecuteFn as () => Promise<CollectionRecord[]>,\n\t\t)\n\t}\n\n\t/** Get the internal descriptor (for testing/debugging) */\n\tgetDescriptor(): QueryDescriptor {\n\t\treturn { ...this.descriptor }\n\t}\n\n\tprivate clone(): QueryBuilder<T> {\n\t\tconst qb = new QueryBuilder<T>(\n\t\t\tthis.collectionName,\n\t\t\tthis.definition,\n\t\t\tthis.adapter,\n\t\t\tthis.subscriptionManager,\n\t\t\t{},\n\t\t\tthis.schema,\n\t\t)\n\t\tqb.descriptor = {\n\t\t\t...this.descriptor,\n\t\t\twhere: { ...this.descriptor.where },\n\t\t\torderBy: [...this.descriptor.orderBy],\n\t\t\tinclude: this.descriptor.include ? [...this.descriptor.include] : undefined,\n\t\t\tincludeCollections: this.descriptor.includeCollections\n\t\t\t\t? [...this.descriptor.includeCollections]\n\t\t\t\t: undefined,\n\t\t}\n\t\treturn qb\n\t}\n\n\t/**\n\t * Resolve include targets to their actual collection names for subscription tracking.\n\t */\n\tprivate resolveIncludeCollections(targets: string[]): string[] {\n\t\tif (!this.schema) return []\n\t\tconst collections: string[] = []\n\n\t\tfor (const target of targets) {\n\t\t\tconst relation = this.findRelation(target)\n\t\t\tif (relation) {\n\t\t\t\t// For many-to-one: the related collection is `relation.to`\n\t\t\t\t// For one-to-many: the related collection is `relation.from`\n\t\t\t\tif (relation.from === this.collectionName) {\n\t\t\t\t\tcollections.push(relation.to)\n\t\t\t\t} else {\n\t\t\t\t\tcollections.push(relation.from)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\treturn collections\n\t}\n\n\t/**\n\t * Resolve includes after primary query, batch-fetching related records.\n\t */\n\tprivate async resolveIncludes(records: CollectionRecord[]): Promise<void> {\n\t\tif (!this.schema || !this.descriptor.include || records.length === 0) return\n\n\t\tfor (const target of this.descriptor.include) {\n\t\t\tconst relation = this.findRelation(target)\n\t\t\tif (!relation) {\n\t\t\t\tthrow new QueryError(\n\t\t\t\t\t`No relation found for include target \"${target}\" on collection \"${this.collectionName}\". ` +\n\t\t\t\t\t\t`Check that a relation is defined in your schema that connects \"${this.collectionName}\" to \"${target}\".`,\n\t\t\t\t)\n\t\t\t}\n\n\t\t\tif (relation.from === this.collectionName) {\n\t\t\t\t// Many-to-one or one-to-one: primary has FK → fetch parent records\n\t\t\t\tawait this.resolveManyToOneInclude(records, relation, target)\n\t\t\t} else {\n\t\t\t\t// One-to-many: related collection has FK → fetch children\n\t\t\t\tawait this.resolveOneToManyInclude(records, relation, target)\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Many-to-one: collect FK values from primary results, batch-fetch related, attach as singular.\n\t */\n\tprivate async resolveManyToOneInclude(\n\t\trecords: CollectionRecord[],\n\t\trelation: { from: string; to: string; field: string },\n\t\ttarget: string,\n\t): Promise<void> {\n\t\tconst fkField = relation.field\n\t\tconst fkValues = records\n\t\t\t.map((r) => r[fkField])\n\t\t\t.filter((v): v is string => v !== null && v !== undefined && typeof v === 'string')\n\n\t\tif (fkValues.length === 0) {\n\t\t\t// All null FKs — set property to null on all records\n\t\t\tconst propName = singularize(target)\n\t\t\tfor (const record of records) {\n\t\t\t\t;(record as Record<string, unknown>)[propName] = null\n\t\t\t}\n\t\t\treturn\n\t\t}\n\n\t\tconst uniqueFks = [...new Set(fkValues)]\n\t\tconst relatedCollection = relation.to\n\t\tconst relatedDef = this.schema?.collections[relatedCollection]\n\t\tif (!relatedDef) return\n\n\t\t// Batch fetch: SELECT * FROM <to> WHERE id IN (...) AND _deleted = 0\n\t\tconst placeholders = uniqueFks.map(() => '?').join(', ')\n\t\tconst sql = `SELECT * FROM ${relatedCollection} WHERE id IN (${placeholders}) AND _deleted = 0`\n\t\tconst rows = await this.adapter.query<RawCollectionRow>(sql, uniqueFks)\n\t\tconst relatedRecords = rows.map((row) => deserializeRecord(row, relatedDef.fields))\n\n\t\t// Build lookup\n\t\tconst lookup = new Map<string, CollectionRecord>()\n\t\tfor (const r of relatedRecords) {\n\t\t\tlookup.set(r.id, r)\n\t\t}\n\n\t\t// Attach as singular property\n\t\tconst propName = singularize(target)\n\t\tfor (const record of records) {\n\t\t\tconst fk = record[fkField] as string | null\n\t\t\t;(record as Record<string, unknown>)[propName] = fk ? (lookup.get(fk) ?? null) : null\n\t\t}\n\t}\n\n\t/**\n\t * One-to-many: collect primary IDs, batch-fetch children, attach as array.\n\t */\n\tprivate async resolveOneToManyInclude(\n\t\trecords: CollectionRecord[],\n\t\trelation: { from: string; to: string; field: string },\n\t\ttarget: string,\n\t): Promise<void> {\n\t\tconst primaryIds = records.map((r) => r.id)\n\t\tconst relatedCollection = relation.from\n\t\tconst relatedDef = this.schema?.collections[relatedCollection]\n\t\tif (!relatedDef) return\n\n\t\tconst fkField = relation.field\n\t\tconst placeholders = primaryIds.map(() => '?').join(', ')\n\t\tconst sql = `SELECT * FROM ${relatedCollection} WHERE ${fkField} IN (${placeholders}) AND _deleted = 0`\n\t\tconst rows = await this.adapter.query<RawCollectionRow>(sql, primaryIds)\n\t\tconst relatedRecords = rows.map((row) => deserializeRecord(row, relatedDef.fields))\n\n\t\t// Group by FK\n\t\tconst grouped = new Map<string, CollectionRecord[]>()\n\t\tfor (const r of relatedRecords) {\n\t\t\tconst fk = r[fkField] as string\n\t\t\tif (!grouped.has(fk)) {\n\t\t\t\tgrouped.set(fk, [])\n\t\t\t}\n\t\t\tgrouped.get(fk)?.push(r)\n\t\t}\n\n\t\t// Attach as array property\n\t\tconst propName = pluralize(target)\n\t\tfor (const record of records) {\n\t\t\t;(record as Record<string, unknown>)[propName] = grouped.get(record.id) ?? []\n\t\t}\n\t}\n\n\t/**\n\t * Find a relation definition matching the include target.\n\t * Searches by relation name, target collection name, and singularized/pluralized variants.\n\t */\n\tprivate findRelation(\n\t\ttarget: string,\n\t): { from: string; to: string; field: string; type: string } | null {\n\t\tif (!this.schema) return null\n\n\t\tfor (const [_name, rel] of Object.entries(this.schema.relations)) {\n\t\t\t// Direct match: target is the related collection name\n\t\t\tif (rel.from === this.collectionName && rel.to === target) return rel\n\t\t\tif (rel.to === this.collectionName && rel.from === target) return rel\n\n\t\t\t// Singularized match: \"project\" matches relation to \"projects\"\n\t\t\tif (rel.from === this.collectionName && rel.to === pluralize(target)) return rel\n\t\t\tif (rel.to === this.collectionName && rel.from === pluralize(target)) return rel\n\n\t\t\t// Pluralized match: \"todos\" matches relation from \"todos\"\n\t\t\tif (rel.from === this.collectionName && rel.to === singularize(target)) return rel\n\t\t\tif (rel.to === this.collectionName && rel.from === singularize(target)) return rel\n\t\t}\n\n\t\treturn null\n\t}\n}\n\n/**\n * Error thrown when a query encounters an invalid state.\n */\nclass QueryError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message)\n\t\tthis.name = 'QueryError'\n\t}\n}\n","/**\n * Minimal pluralization/singularization utilities for relation name resolution.\n * Handles common English patterns — not meant to be exhaustive.\n */\n\nfunction isVowel(char: string | undefined): boolean {\n\tif (!char) return false\n\treturn 'aeiouAEIOU'.includes(char)\n}\n\n/**\n * Pluralize a word using common English rules.\n *\n * @example\n * ```\n * pluralize('project') // 'projects'\n * pluralize('category') // 'categories'\n * pluralize('match') // 'matches'\n * ```\n */\nexport function pluralize(word: string): string {\n\tif (word.endsWith('s')) return word\n\tif (word.endsWith('y') && !isVowel(word[word.length - 2])) {\n\t\treturn `${word.slice(0, -1)}ies`\n\t}\n\tif (word.endsWith('sh') || word.endsWith('ch') || word.endsWith('x') || word.endsWith('z')) {\n\t\treturn `${word}es`\n\t}\n\treturn `${word}s`\n}\n\n/**\n * Singularize a word using common English rules.\n *\n * @example\n * ```\n * singularize('projects') // 'project'\n * singularize('categories') // 'category'\n * singularize('matches') // 'match'\n * ```\n */\nexport function singularize(word: string): string {\n\tif (word.endsWith('ies') && !isVowel(word[word.length - 4])) {\n\t\treturn `${word.slice(0, -3)}y`\n\t}\n\tif (\n\t\tword.endsWith('shes') ||\n\t\tword.endsWith('ches') ||\n\t\tword.endsWith('xes') ||\n\t\tword.endsWith('zes')\n\t) {\n\t\treturn word.slice(0, -2)\n\t}\n\tif (word.endsWith('ses')) {\n\t\treturn word.slice(0, -2)\n\t}\n\tif (word.endsWith('s') && !word.endsWith('ss')) {\n\t\treturn word.slice(0, -1)\n\t}\n\treturn word\n}\n","import type { HybridLogicalClock, Operation, SchemaDefinition } from '@korajs/core'\nimport { KoraError, createOperation } from '@korajs/core'\nimport { buildInsertQuery, buildSoftDeleteQuery, buildUpdateQuery } from '../query/sql-builder'\nimport { serializeOperation, serializeRecord } from '../serialization/serializer'\nimport { allocateNextSequenceInTransaction } from '../store/sequence-allocator'\nimport type { StorageAdapter, Transaction } from '../types'\nimport type { IncomingRelation } from './relation-lookup'\nimport { buildRelationLookup, getIncomingRelations } from './relation-lookup'\n\n/**\n * Error thrown when a delete is refused due to a 'restrict' referential integrity policy.\n * The error includes context about which relation caused the restriction and\n * how many referencing records exist.\n */\nexport class ReferentialIntegrityError extends KoraError {\n\tconstructor(\n\t\tcollection: string,\n\t\trecordId: string,\n\t\treferencingCollection: string,\n\t\trelationName: string,\n\t\treferencingCount: number,\n\t) {\n\t\tsuper(\n\t\t\t`Cannot delete record \"${recordId}\" from \"${collection}\": ${referencingCount} record(s) in \"${referencingCollection}\" reference it via relation \"${relationName}\" with onDelete: 'restrict'. Delete or reassign the referencing records first.`,\n\t\t\t'REFERENTIAL_INTEGRITY',\n\t\t\t{\n\t\t\t\tcollection,\n\t\t\t\trecordId,\n\t\t\t\treferencingCollection,\n\t\t\t\trelationName,\n\t\t\t\treferencingCount,\n\t\t\t},\n\t\t)\n\t\tthis.name = 'ReferentialIntegrityError'\n\t}\n}\n\n/**\n * Configuration for the RelationEnforcer.\n */\nexport interface RelationEnforcerConfig {\n\tschema: SchemaDefinition\n\tadapter: StorageAdapter\n\tclock: HybridLogicalClock\n\tnodeId: string\n}\n\n/**\n * Result of enforcing referential integrity on a delete operation.\n * Contains all additional operations that were created as side effects\n * (cascaded deletes and set-null updates).\n */\nexport interface EnforcementResult {\n\t/** Additional operations created by cascading deletes and set-null updates */\n\toperations: Operation[]\n}\n\n/**\n * Enforces referential integrity constraints during local delete operations.\n *\n * When a record is deleted, this enforcer checks all relations that reference\n * the deleted record's collection and applies the appropriate onDelete policy:\n *\n * - **cascade**: Recursively deletes all referencing records\n * - **set-null**: Sets the foreign key to null on all referencing records\n * - **restrict**: Throws a ReferentialIntegrityError if any references exist\n * - **no-action**: Does nothing (the foreign key is left dangling)\n *\n * The enforcer operates within a provided transaction to ensure atomicity.\n * All generated operations (cascaded deletes, set-null updates) share a\n * causal dependency chain through the original delete operation.\n *\n * @example\n * ```typescript\n * const enforcer = new RelationEnforcer({\n * schema, adapter, clock, nodeId,\n * allocateSequenceNumber: async () => ++seq,\n * })\n * const result = await enforcer.enforceDelete(\n * 'projects', 'proj-1', tx, ['delete-op-id']\n * )\n * // result.operations contains any cascaded delete/update ops\n * ```\n */\nexport class RelationEnforcer {\n\tprivate readonly lookup: Map<string, IncomingRelation[]>\n\tprivate readonly schema: SchemaDefinition\n\tprivate readonly adapter: StorageAdapter\n\tprivate readonly clock: HybridLogicalClock\n\tprivate readonly nodeId: string\n\n\tconstructor(config: RelationEnforcerConfig) {\n\t\tthis.schema = config.schema\n\t\tthis.adapter = config.adapter\n\t\tthis.clock = config.clock\n\t\tthis.nodeId = config.nodeId\n\t\tthis.lookup = buildRelationLookup(config.schema)\n\t}\n\n\t/**\n\t * Enforce referential integrity after deleting a record.\n\t *\n\t * Must be called within a transaction. The transaction handle is used\n\t * for all cascaded writes to ensure atomicity.\n\t *\n\t * @param collection - The collection the deleted record belongs to\n\t * @param recordId - The ID of the deleted record\n\t * @param tx - The active transaction handle\n\t * @param causalDeps - Causal dependencies for generated operations\n\t * @returns All additional operations created as side effects\n\t * @throws {ReferentialIntegrityError} If a 'restrict' policy is violated\n\t */\n\tasync enforceDelete(\n\t\tcollection: string,\n\t\trecordId: string,\n\t\ttx: Transaction,\n\t\tcausalDeps: string[],\n\t): Promise<EnforcementResult> {\n\t\tconst incomingRelations = getIncomingRelations(this.lookup, collection)\n\t\tif (incomingRelations.length === 0) {\n\t\t\treturn { operations: [] }\n\t\t}\n\n\t\tconst allOperations: Operation[] = []\n\n\t\t// Process relations in a deterministic order (sorted by relation name)\n\t\t// to ensure identical results regardless of Map iteration order.\n\t\tconst sortedRelations = [...incomingRelations].sort((a, b) =>\n\t\t\ta.relationName.localeCompare(b.relationName),\n\t\t)\n\n\t\tfor (const incoming of sortedRelations) {\n\t\t\tconst ops = await this.enforceRelation(incoming, recordId, tx, causalDeps)\n\t\t\tallOperations.push(...ops)\n\t\t}\n\n\t\treturn { operations: allOperations }\n\t}\n\n\t/**\n\t * Enforce a single relation's onDelete policy.\n\t */\n\tprivate async enforceRelation(\n\t\tincoming: IncomingRelation,\n\t\tdeletedRecordId: string,\n\t\ttx: Transaction,\n\t\tcausalDeps: string[],\n\t): Promise<Operation[]> {\n\t\tswitch (incoming.onDelete) {\n\t\t\tcase 'cascade':\n\t\t\t\treturn this.enforceCascade(incoming, deletedRecordId, tx, causalDeps)\n\t\t\tcase 'set-null':\n\t\t\t\treturn this.enforceSetNull(incoming, deletedRecordId, tx, causalDeps)\n\t\t\tcase 'restrict':\n\t\t\t\treturn this.enforceRestrict(incoming, deletedRecordId, tx)\n\t\t\tcase 'no-action':\n\t\t\t\treturn []\n\t\t}\n\t}\n\n\t/**\n\t * Cascade: delete all records in the source collection that reference\n\t * the deleted record, then recursively cascade those deletes.\n\t */\n\tprivate async enforceCascade(\n\t\tincoming: IncomingRelation,\n\t\tdeletedRecordId: string,\n\t\ttx: Transaction,\n\t\tcausalDeps: string[],\n\t): Promise<Operation[]> {\n\t\tconst { sourceCollection, foreignKeyField } = incoming\n\n\t\t// Find all non-deleted records that reference the deleted record\n\t\tconst referencingRows = await tx.query<{ id: string }>(\n\t\t\t`SELECT id FROM ${sourceCollection} WHERE ${foreignKeyField} = ? AND _deleted = 0`,\n\t\t\t[deletedRecordId],\n\t\t)\n\n\t\tif (referencingRows.length === 0) {\n\t\t\treturn []\n\t\t}\n\n\t\tconst operations: Operation[] = []\n\n\t\tfor (const row of referencingRows) {\n\t\t\tconst now = Date.now()\n\t\t\tconst sequenceNumber = await allocateNextSequenceInTransaction(tx, this.nodeId)\n\n\t\t\tconst operation = await createOperation(\n\t\t\t\t{\n\t\t\t\t\tnodeId: this.nodeId,\n\t\t\t\t\ttype: 'delete',\n\t\t\t\t\tcollection: sourceCollection,\n\t\t\t\t\trecordId: row.id,\n\t\t\t\t\tdata: null,\n\t\t\t\t\tpreviousData: null,\n\t\t\t\t\tsequenceNumber,\n\t\t\t\t\tcausalDeps: [...causalDeps],\n\t\t\t\t\tschemaVersion: this.schema.version,\n\t\t\t\t},\n\t\t\t\tthis.clock,\n\t\t\t)\n\n\t\t\tconst deleteQuery = buildSoftDeleteQuery(sourceCollection, row.id, now)\n\t\t\tawait tx.execute(deleteQuery.sql, deleteQuery.params)\n\n\t\t\tconst opInsert = buildInsertQuery(\n\t\t\t\t`_kora_ops_${sourceCollection}`,\n\t\t\t\tserializeOperation(operation) as unknown as Record<string, unknown>,\n\t\t\t)\n\t\t\tawait tx.execute(opInsert.sql, opInsert.params)\n\n\t\t\toperations.push(operation)\n\n\t\t\t// Recursively cascade: this deleted record might also be referenced\n\t\t\tconst cascadeResult = await this.enforceDelete(sourceCollection, row.id, tx, [operation.id])\n\t\t\toperations.push(...cascadeResult.operations)\n\t\t}\n\n\t\treturn operations\n\t}\n\n\t/**\n\t * Set-null: update all referencing records to set the foreign key to null.\n\t */\n\tprivate async enforceSetNull(\n\t\tincoming: IncomingRelation,\n\t\tdeletedRecordId: string,\n\t\ttx: Transaction,\n\t\tcausalDeps: string[],\n\t): Promise<Operation[]> {\n\t\tconst { sourceCollection, foreignKeyField } = incoming\n\t\tconst collectionDef = this.schema.collections[sourceCollection]\n\t\tif (!collectionDef) {\n\t\t\treturn []\n\t\t}\n\n\t\t// Find all non-deleted records that reference the deleted record\n\t\tconst referencingRows = await tx.query<{ id: string }>(\n\t\t\t`SELECT id FROM ${sourceCollection} WHERE ${foreignKeyField} = ? AND _deleted = 0`,\n\t\t\t[deletedRecordId],\n\t\t)\n\n\t\tif (referencingRows.length === 0) {\n\t\t\treturn []\n\t\t}\n\n\t\tconst operations: Operation[] = []\n\n\t\tfor (const row of referencingRows) {\n\t\t\tconst now = Date.now()\n\t\t\tconst sequenceNumber = await allocateNextSequenceInTransaction(tx, this.nodeId)\n\n\t\t\tconst updateData: Record<string, unknown> = { [foreignKeyField]: null }\n\t\t\tconst previousData: Record<string, unknown> = { [foreignKeyField]: deletedRecordId }\n\n\t\t\tconst operation = await createOperation(\n\t\t\t\t{\n\t\t\t\t\tnodeId: this.nodeId,\n\t\t\t\t\ttype: 'update',\n\t\t\t\t\tcollection: sourceCollection,\n\t\t\t\t\trecordId: row.id,\n\t\t\t\t\tdata: { ...updateData },\n\t\t\t\t\tpreviousData,\n\t\t\t\t\tsequenceNumber,\n\t\t\t\t\tcausalDeps: [...causalDeps],\n\t\t\t\t\tschemaVersion: this.schema.version,\n\t\t\t\t},\n\t\t\t\tthis.clock,\n\t\t\t)\n\n\t\t\t// Update the record's foreign key to null\n\t\t\tconst serializedChanges = serializeRecord(updateData, collectionDef.fields)\n\t\t\tconst updateQuery = buildUpdateQuery(sourceCollection, row.id, {\n\t\t\t\t...serializedChanges,\n\t\t\t\t_updated_at: now,\n\t\t\t})\n\t\t\tawait tx.execute(updateQuery.sql, updateQuery.params)\n\n\t\t\t// Persist the operation\n\t\t\tconst opRow = serializeOperation(operation)\n\t\t\tconst opInsert = buildInsertQuery(\n\t\t\t\t`_kora_ops_${sourceCollection}`,\n\t\t\t\topRow as unknown as Record<string, unknown>,\n\t\t\t)\n\t\t\tawait tx.execute(opInsert.sql, opInsert.params)\n\n\t\t\toperations.push(operation)\n\t\t}\n\n\t\treturn operations\n\t}\n\n\t/**\n\t * Restrict: refuse the delete if any referencing records exist.\n\t */\n\tprivate async enforceRestrict(\n\t\tincoming: IncomingRelation,\n\t\tdeletedRecordId: string,\n\t\ttx: Transaction,\n\t): Promise<Operation[]> {\n\t\tconst { sourceCollection, foreignKeyField, relationName } = incoming\n\n\t\tconst countRows = await tx.query<{ cnt: number }>(\n\t\t\t`SELECT COUNT(*) as cnt FROM ${sourceCollection} WHERE ${foreignKeyField} = ? AND _deleted = 0`,\n\t\t\t[deletedRecordId],\n\t\t)\n\t\tconst count = countRows[0]?.cnt ?? 0\n\n\t\tif (count > 0) {\n\t\t\t// Determine the target collection from the relation lookup\n\t\t\tconst targetCollection = incoming.relation.to\n\t\t\tthrow new ReferentialIntegrityError(\n\t\t\t\ttargetCollection,\n\t\t\t\tdeletedRecordId,\n\t\t\t\tsourceCollection,\n\t\t\t\trelationName,\n\t\t\t\tcount,\n\t\t\t)\n\t\t}\n\n\t\treturn []\n\t}\n\n\t/**\n\t * Get the relation lookup map for external use (e.g., by the merge engine).\n\t */\n\tgetRelationLookup(): Map<string, IncomingRelation[]> {\n\t\treturn this.lookup\n\t}\n}\n","import type { OnDeleteAction, RelationDefinition, SchemaDefinition } from '@korajs/core'\n\n/**\n * A resolved relation reference: describes a relation that points TO a given collection.\n * When a record in `targetCollection` is deleted, we must handle records in\n * `sourceCollection` that reference it via `foreignKeyField`.\n */\nexport interface IncomingRelation {\n\t/** Name of the relation in the schema */\n\trelationName: string\n\t/** Collection that holds the foreign key (the \"from\" side) */\n\tsourceCollection: string\n\t/** Field in the source collection that references the target */\n\tforeignKeyField: string\n\t/** What to do when the referenced record is deleted */\n\tonDelete: OnDeleteAction\n\t/** The full relation definition */\n\trelation: RelationDefinition\n}\n\n/**\n * Builds an efficient lookup from target collection to all relations that reference it.\n *\n * Given a schema with relations like:\n * ```\n * todoBelongsToProject: { from: 'todos', to: 'projects', field: 'projectId', onDelete: 'cascade' }\n * ```\n *\n * The lookup for 'projects' would return:\n * ```\n * [{ sourceCollection: 'todos', foreignKeyField: 'projectId', onDelete: 'cascade', ... }]\n * ```\n *\n * This enables O(1) lookup when deleting a record to find all dependent relations.\n *\n * @param schema - The full schema definition containing relations\n * @returns A map from target collection name to its incoming relations\n */\nexport function buildRelationLookup(schema: SchemaDefinition): Map<string, IncomingRelation[]> {\n\tconst lookup = new Map<string, IncomingRelation[]>()\n\n\tfor (const [relationName, relation] of Object.entries(schema.relations)) {\n\t\tconst targetCollection = relation.to\n\t\tconst existing = lookup.get(targetCollection) ?? []\n\t\texisting.push({\n\t\t\trelationName,\n\t\t\tsourceCollection: relation.from,\n\t\t\tforeignKeyField: relation.field,\n\t\t\tonDelete: relation.onDelete,\n\t\t\trelation,\n\t\t})\n\t\tlookup.set(targetCollection, existing)\n\t}\n\n\treturn lookup\n}\n\n/**\n * Get all incoming relations for a given collection.\n * Returns an empty array if no relations reference the collection.\n *\n * @param lookup - The pre-built relation lookup map\n * @param collection - The target collection name\n * @returns Array of incoming relations\n */\nexport function getIncomingRelations(\n\tlookup: Map<string, IncomingRelation[]>,\n\tcollection: string,\n): IncomingRelation[] {\n\treturn lookup.get(collection) ?? []\n}\n","import { OperationError } from '@korajs/core'\nimport type { CollectionDefinition, Operation, SchemaDefinition } from '@korajs/core'\nimport { topologicalSort } from '@korajs/core/internal'\nimport type { CollectionRecord } from '../types'\n\n/**\n * In-memory materialized state at a causal cut in the operation log.\n * Read-only — does not mutate the live store.\n */\nexport interface ReplaySnapshot {\n\t/** The operation whose causal past was replayed (inclusive). */\n\ttargetOperation: Operation\n\t/** Operations applied in causal order (ancestors + target). */\n\toperationsApplied: Operation[]\n\t/** Non-deleted records per collection after replay. */\n\tcollections: Record<string, CollectionRecord[]>\n\t/** Look up a single record at the replay cut. Returns null if deleted or missing. */\n\tfindRecord(collection: string, recordId: string): CollectionRecord | null\n}\n\ninterface MutableReplayRecord {\n\tid: string\n\tfields: Record<string, unknown>\n\tdeleted: boolean\n\tcreatedAt: number\n\tupdatedAt: number\n}\n\ntype ReplayMemoryState = Map<string, Map<string, MutableReplayRecord>>\n\n/**\n * Collect the target operation and all causal ancestors present in `allOps`.\n */\nexport function collectCausalClosure(allOps: Operation[], targetOperationId: string): Operation[] {\n\tconst opMap = new Map<string, Operation>()\n\tfor (const op of allOps) {\n\t\topMap.set(op.id, op)\n\t}\n\n\tconst target = opMap.get(targetOperationId)\n\tif (!target) {\n\t\tthrow new OperationError(\n\t\t\t`Operation \"${targetOperationId}\" not found in the local operation log`,\n\t\t\t{\n\t\t\t\toperationId: targetOperationId,\n\t\t\t},\n\t\t)\n\t}\n\n\tconst included = new Set<string>()\n\tconst stack: string[] = [targetOperationId]\n\n\twhile (stack.length > 0) {\n\t\tconst id = stack.pop()\n\t\tif (id === undefined || included.has(id)) {\n\t\t\tcontinue\n\t\t}\n\t\tincluded.add(id)\n\t\tconst op = opMap.get(id)\n\t\tif (!op) {\n\t\t\tcontinue\n\t\t}\n\t\tfor (const depId of op.causalDeps) {\n\t\t\tif (opMap.has(depId)) {\n\t\t\t\tstack.push(depId)\n\t\t\t}\n\t\t}\n\t}\n\n\tconst subset = allOps.filter((op) => included.has(op.id))\n\treturn topologicalSort(subset)\n}\n\n/**\n * Replay a causal subset of operations into an in-memory materialized snapshot.\n * Does not use the merge engine — concurrent ops outside the causal cut are excluded.\n */\nexport function buildReplaySnapshot(\n\tschema: SchemaDefinition,\n\tallOps: Operation[],\n\ttargetOperationId: string,\n): ReplaySnapshot {\n\tconst operationsApplied = collectCausalClosure(allOps, targetOperationId)\n\tconst targetOperation = operationsApplied.find((op) => op.id === targetOperationId)\n\tif (!targetOperation) {\n\t\tthrow new OperationError(`Operation \"${targetOperationId}\" not found after causal sort`, {\n\t\t\toperationId: targetOperationId,\n\t\t})\n\t}\n\n\tconst memory: ReplayMemoryState = new Map()\n\tfor (const op of operationsApplied) {\n\t\tapplyOperationToMemory(memory, op, schema)\n\t}\n\n\tconst collections = materializeCollections(schema, memory)\n\n\treturn {\n\t\ttargetOperation,\n\t\toperationsApplied,\n\t\tcollections,\n\t\tfindRecord(collection: string, recordId: string): CollectionRecord | null {\n\t\t\tconst colMap = memory.get(collection)\n\t\t\tconst record = colMap?.get(recordId)\n\t\t\tif (!record || record.deleted) {\n\t\t\t\treturn null\n\t\t\t}\n\t\t\tconst definition = schema.collections[collection]\n\t\t\tif (!definition) {\n\t\t\t\treturn null\n\t\t\t}\n\t\t\treturn toCollectionRecord(record, definition)\n\t\t},\n\t}\n}\n\nfunction applyOperationToMemory(\n\tstate: ReplayMemoryState,\n\top: Operation,\n\tschema: SchemaDefinition,\n): void {\n\tconst definition = schema.collections[op.collection]\n\tif (!definition) {\n\t\treturn\n\t}\n\n\tlet colMap = state.get(op.collection)\n\tif (!colMap) {\n\t\tcolMap = new Map()\n\t\tstate.set(op.collection, colMap)\n\t}\n\n\tconst wallTime = op.timestamp.wallTime\n\n\tswitch (op.type) {\n\t\tcase 'insert': {\n\t\t\tif (!op.data) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcolMap.set(op.recordId, {\n\t\t\t\tid: op.recordId,\n\t\t\t\tfields: { ...op.data },\n\t\t\t\tdeleted: false,\n\t\t\t\tcreatedAt: wallTime,\n\t\t\t\tupdatedAt: wallTime,\n\t\t\t})\n\t\t\tbreak\n\t\t}\n\t\tcase 'update': {\n\t\t\tif (!op.data) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tconst existing = colMap.get(op.recordId)\n\t\t\tif (existing && !existing.deleted) {\n\t\t\t\texisting.fields = { ...existing.fields, ...op.data }\n\t\t\t\texisting.updatedAt = wallTime\n\t\t\t\tbreak\n\t\t\t}\n\t\t\tif (existing?.deleted) {\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcolMap.set(op.recordId, {\n\t\t\t\tid: op.recordId,\n\t\t\t\tfields: { ...op.data },\n\t\t\t\tdeleted: false,\n\t\t\t\tcreatedAt: wallTime,\n\t\t\t\tupdatedAt: wallTime,\n\t\t\t})\n\t\t\tbreak\n\t\t}\n\t\tcase 'delete': {\n\t\t\tconst existing = colMap.get(op.recordId)\n\t\t\tif (existing) {\n\t\t\t\texisting.deleted = true\n\t\t\t\texisting.updatedAt = wallTime\n\t\t\t\treturn\n\t\t\t}\n\t\t\tcolMap.set(op.recordId, {\n\t\t\t\tid: op.recordId,\n\t\t\t\tfields: {},\n\t\t\t\tdeleted: true,\n\t\t\t\tcreatedAt: wallTime,\n\t\t\t\tupdatedAt: wallTime,\n\t\t\t})\n\t\t\tbreak\n\t\t}\n\t}\n}\n\nfunction materializeCollections(\n\tschema: SchemaDefinition,\n\tmemory: ReplayMemoryState,\n): Record<string, CollectionRecord[]> {\n\tconst collections: Record<string, CollectionRecord[]> = {}\n\n\tfor (const [collectionName, definition] of Object.entries(schema.collections)) {\n\t\tconst colMap = memory.get(collectionName)\n\t\tconst records: CollectionRecord[] = []\n\t\tif (colMap) {\n\t\t\tfor (const record of colMap.values()) {\n\t\t\t\tif (!record.deleted) {\n\t\t\t\t\trecords.push(toCollectionRecord(record, definition))\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tcollections[collectionName] = records\n\t}\n\n\treturn collections\n}\n\nfunction toCollectionRecord(\n\trecord: MutableReplayRecord,\n\tdefinition: CollectionDefinition,\n): CollectionRecord {\n\tconst result: CollectionRecord = {\n\t\tid: record.id,\n\t\tcreatedAt: record.createdAt,\n\t\tupdatedAt: record.updatedAt,\n\t}\n\tfor (const [fieldName] of Object.entries(definition.fields)) {\n\t\tif (fieldName in record.fields) {\n\t\t\tresult[fieldName] = record.fields[fieldName]\n\t\t}\n\t}\n\treturn result\n}\n","import type { SequenceConfig } from '@korajs/core'\nimport { defaultSequenceFormat, formatSequenceValue } from '@korajs/core'\nimport type { StorageAdapter } from '../types'\n\ninterface SequenceRow {\n\tcounter: number\n}\n\n/**\n * Manages offline-safe sequences backed by a `_kora_sequences` table.\n *\n * Each sequence counter is scoped by (name, scope, nodeId), ensuring\n * that different devices never collide. The counter is atomically\n * incremented within a database transaction.\n *\n * @example\n * ```typescript\n * const mgr = new SequenceManager(adapter, 'node-abc')\n *\n * const receipt = await mgr.next('receipt', {\n * scope: 'store-1',\n * format: 'S-{date}-{node4}-{seq}',\n * })\n * // → \"S-20260508-node-0001\"\n * ```\n */\nexport class SequenceManager {\n\tprivate readonly adapter: StorageAdapter\n\tprivate readonly nodeId: string\n\n\tconstructor(adapter: StorageAdapter, nodeId: string) {\n\t\tthis.adapter = adapter\n\t\tthis.nodeId = nodeId\n\t}\n\n\t/**\n\t * Get the next value in a sequence, atomically incrementing the counter.\n\t *\n\t * @param name - The sequence name (e.g., 'receipt', 'invoice')\n\t * @param config - Optional configuration for scope, format, and starting value\n\t * @returns The formatted sequence value\n\t */\n\tasync next(name: string, config?: SequenceConfig): Promise<string> {\n\t\tconst scope = config?.scope ?? ''\n\t\tconst startAt = config?.startAt ?? 1\n\t\tconst format = config?.format ?? defaultSequenceFormat(name)\n\n\t\tlet counter = 0\n\n\t\tawait this.adapter.transaction(async (tx) => {\n\t\t\t// Try to read the current counter\n\t\t\tconst rows = await tx.query<SequenceRow>(\n\t\t\t\t'SELECT counter FROM _kora_sequences WHERE name = ? AND scope = ? AND node_id = ?',\n\t\t\t\t[name, scope, this.nodeId],\n\t\t\t)\n\n\t\t\tif (rows.length > 0) {\n\t\t\t\tconst row = rows[0] as SequenceRow\n\t\t\t\tcounter = row.counter + 1\n\t\t\t\tawait tx.execute(\n\t\t\t\t\t'UPDATE _kora_sequences SET counter = ? WHERE name = ? AND scope = ? AND node_id = ?',\n\t\t\t\t\t[counter, name, scope, this.nodeId],\n\t\t\t\t)\n\t\t\t} else {\n\t\t\t\t// First use — initialize with startAt\n\t\t\t\tcounter = startAt\n\t\t\t\tawait tx.execute(\n\t\t\t\t\t'INSERT INTO _kora_sequences (name, scope, node_id, counter) VALUES (?, ?, ?, ?)',\n\t\t\t\t\t[name, scope, this.nodeId, counter],\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\n\t\treturn formatSequenceValue(format, counter, this.nodeId)\n\t}\n\n\t/**\n\t * Get the current counter value without incrementing.\n\t *\n\t * @param name - The sequence name\n\t * @param config - Optional scope\n\t * @returns The current counter value, or 0 if the sequence has never been used\n\t */\n\tasync current(name: string, config?: { scope?: string }): Promise<number> {\n\t\tconst scope = config?.scope ?? ''\n\n\t\tconst rows = await this.adapter.query<SequenceRow>(\n\t\t\t'SELECT counter FROM _kora_sequences WHERE name = ? AND scope = ? AND node_id = ?',\n\t\t\t[name, scope, this.nodeId],\n\t\t)\n\n\t\tif (rows.length > 0) {\n\t\t\treturn (rows[0] as SequenceRow).counter\n\t\t}\n\t\treturn 0\n\t}\n\n\t/**\n\t * Reset a sequence counter.\n\t *\n\t * @param name - The sequence name\n\t * @param config - Optional scope and target value\n\t */\n\tasync reset(name: string, config?: { scope?: string; to?: number }): Promise<void> {\n\t\tconst scope = config?.scope ?? ''\n\t\tconst to = config?.to ?? 0\n\n\t\tawait this.adapter.execute(\n\t\t\t'DELETE FROM _kora_sequences WHERE name = ? AND scope = ? AND node_id = ?',\n\t\t\t[name, scope, this.nodeId],\n\t\t)\n\n\t\tif (to > 0) {\n\t\t\tawait this.adapter.execute(\n\t\t\t\t'INSERT INTO _kora_sequences (name, scope, node_id, counter) VALUES (?, ?, ?, ?)',\n\t\t\t\t[name, scope, this.nodeId, to],\n\t\t\t)\n\t\t}\n\t}\n}\n","/**\n * Bloom filter for fast subscription dependency pre-checking.\n *\n * When the SubscriptionManager has many active subscriptions, checking whether a\n * mutation might affect any subscription requires scanning all subscriptions.\n * This bloom filter provides a fast O(k) pre-check: if the filter says \"definitely\n * not present\", we can skip the subscription entirely. If it says \"maybe present\",\n * we fall through to the precise collection/field matching.\n *\n * Uses FNV-1a for hashing and Kirsch-Mitzenmacker double hashing to derive k\n * hash functions from two base hashes.\n */\n\n// FNV-1a constants for 32-bit hashes\nconst FNV_OFFSET_BASIS = 0x811c9dc5\nconst FNV_PRIME = 0x01000193\n\n/**\n * Compute FNV-1a 32-bit hash of a string.\n *\n * FNV-1a is a non-cryptographic hash function optimized for speed and distribution.\n * It processes one byte at a time: XOR then multiply, which gives better avalanche\n * behavior than the original FNV-1 (multiply then XOR).\n *\n * @param input - The string to hash\n * @returns A 32-bit unsigned integer hash\n */\nexport function fnv1a32(input: string): number {\n\tlet hash = FNV_OFFSET_BASIS\n\tfor (let i = 0; i < input.length; i++) {\n\t\thash ^= input.charCodeAt(i)\n\t\t// Math.imul gives true 32-bit integer multiplication without overflow issues\n\t\thash = Math.imul(hash, FNV_PRIME)\n\t}\n\t// Ensure unsigned 32-bit result\n\treturn hash >>> 0\n}\n\n/**\n * Calculate the optimal number of bits for a bloom filter.\n *\n * Formula: m = -(n * ln(p)) / (ln(2)^2)\n * Rounded up to the nearest multiple of 32 for Uint32Array alignment.\n *\n * @param expectedItems - Expected number of items to insert\n * @param falsePositiveRate - Desired false positive rate (0-1)\n * @returns Number of bits, rounded to nearest 32-bit multiple (minimum 32)\n */\nexport function optimalBitCount(expectedItems: number, falsePositiveRate: number): number {\n\tif (expectedItems <= 0) return 32\n\tif (falsePositiveRate <= 0 || falsePositiveRate >= 1) return 32\n\n\tconst ln2Squared = Math.LN2 * Math.LN2\n\tconst rawBits = -(expectedItems * Math.log(falsePositiveRate)) / ln2Squared\n\n\t// Round up to nearest 32-bit multiple for Uint32Array alignment\n\tconst aligned = Math.ceil(rawBits / 32) * 32\n\treturn Math.max(32, aligned)\n}\n\n/**\n * Calculate the optimal number of hash functions for a bloom filter.\n *\n * Formula: k = (m/n) * ln(2)\n * Clamped to [1, 30] to avoid degenerate cases.\n *\n * @param bitCount - Total number of bits in the filter\n * @param expectedItems - Expected number of items to insert\n * @returns Number of hash functions, clamped to [1, 30]\n */\nexport function optimalHashCount(bitCount: number, expectedItems: number): number {\n\tif (expectedItems <= 0) return 1\n\tconst raw = (bitCount / expectedItems) * Math.LN2\n\treturn Math.max(1, Math.min(30, Math.round(raw)))\n}\n\n/**\n * Bloom filter for subscription dependency tracking.\n *\n * Each subscription adds its watched collection (and optionally fields) to the\n * filter. When a mutation arrives, we check \"collection\" and \"collection:field\"\n * keys against the filter. A negative result means no subscription cares about\n * this mutation, so we can skip the expensive per-subscription scan.\n *\n * @example\n * ```typescript\n * const filter = new SubscriptionBloomFilter(100, 0.01)\n * filter.add('todos')\n * filter.add('todos', 'completed')\n *\n * filter.mightContain('todos') // true (definitely added)\n * filter.mightContain('projects') // false (definitely not added)\n * filter.mightContain('todos', 'completed') // true (definitely added)\n * ```\n */\nexport class SubscriptionBloomFilter {\n\tprivate bits: Uint32Array\n\tprivate readonly bitCount: number\n\tprivate readonly hashCount: number\n\tprivate itemCount = 0\n\n\tconstructor(expectedItems: number, falsePositiveRate = 0.01) {\n\t\tthis.bitCount = optimalBitCount(expectedItems, falsePositiveRate)\n\t\tthis.hashCount = optimalHashCount(this.bitCount, expectedItems)\n\t\tthis.bits = new Uint32Array(this.bitCount / 32)\n\t}\n\n\t/**\n\t * Add a collection (and optional field) to the bloom filter.\n\t *\n\t * @param collection - Collection name (e.g., \"todos\")\n\t * @param field - Optional field name (e.g., \"completed\")\n\t */\n\tadd(collection: string, field?: string): void {\n\t\tconst key = field !== undefined ? `${collection}:${field}` : collection\n\t\tconst positions = this.getPositions(key)\n\t\tfor (const pos of positions) {\n\t\t\tconst wordIndex = pos >>> 5 // equivalent to Math.floor(pos / 32)\n\t\t\tconst bitIndex = pos & 31 // equivalent to pos % 32\n\t\t\tconst current = this.bits[wordIndex] ?? 0\n\t\t\tthis.bits[wordIndex] = current | (1 << bitIndex)\n\t\t}\n\t\tthis.itemCount++\n\t}\n\n\t/**\n\t * Check if a collection (and optional field) might be in the filter.\n\t *\n\t * A return value of `false` means the item is DEFINITELY NOT in the filter.\n\t * A return value of `true` means the item MIGHT be in the filter (possible false positive).\n\t *\n\t * @param collection - Collection name to check\n\t * @param field - Optional field name to check\n\t * @returns false = definitely absent, true = possibly present\n\t */\n\tmightContain(collection: string, field?: string): boolean {\n\t\tconst key = field !== undefined ? `${collection}:${field}` : collection\n\t\tconst positions = this.getPositions(key)\n\t\tfor (const pos of positions) {\n\t\t\tconst wordIndex = pos >>> 5\n\t\t\tconst bitIndex = pos & 31\n\t\t\tif (((this.bits[wordIndex] ?? 0) & (1 << bitIndex)) === 0) {\n\t\t\t\treturn false\n\t\t\t}\n\t\t}\n\t\treturn true\n\t}\n\n\t/**\n\t * Reset the bloom filter, clearing all bits and the item count.\n\t */\n\tclear(): void {\n\t\tthis.bits.fill(0)\n\t\tthis.itemCount = 0\n\t}\n\n\t/**\n\t * Estimate the current false positive rate based on the number of items inserted.\n\t *\n\t * Formula: (1 - e^(-kn/m))^k\n\t * where k = hash count, n = item count, m = bit count.\n\t *\n\t * @returns Estimated false positive rate as a number between 0 and 1\n\t */\n\testimatedFalsePositiveRate(): number {\n\t\tif (this.itemCount === 0) return 0\n\t\tconst exponent = -(this.hashCount * this.itemCount) / this.bitCount\n\t\treturn (1 - Math.exp(exponent)) ** this.hashCount\n\t}\n\n\t/**\n\t * @returns The number of items that have been added to the filter\n\t */\n\tgetItemCount(): number {\n\t\treturn this.itemCount\n\t}\n\n\t/**\n\t * @returns The total number of bits in the filter\n\t */\n\tgetBitCount(): number {\n\t\treturn this.bitCount\n\t}\n\n\t/**\n\t * @returns The number of hash functions used\n\t */\n\tgetHashCount(): number {\n\t\treturn this.hashCount\n\t}\n\n\t/**\n\t * Count the number of bits currently set to 1.\n\t * Uses Brian Kernighan's algorithm: each iteration clears the lowest set bit,\n\t * so the loop runs exactly as many times as there are set bits.\n\t *\n\t * @returns Number of bits set to 1\n\t */\n\tgetSetBitCount(): number {\n\t\tlet count = 0\n\t\tfor (let i = 0; i < this.bits.length; i++) {\n\t\t\tlet word = this.bits[i] ?? 0\n\t\t\twhile (word !== 0) {\n\t\t\t\t// Clear the lowest set bit: n & (n - 1) removes the rightmost 1-bit\n\t\t\t\tword &= word - 1\n\t\t\t\tcount++\n\t\t\t}\n\t\t}\n\t\treturn count\n\t}\n\n\t/**\n\t * Compute k bit positions for a given key using Kirsch-Mitzenmacker double hashing.\n\t *\n\t * Instead of computing k independent hash functions, we compute two base hashes\n\t * (h1 and h2) and derive the rest as: h_i = (h1 + i * h2) mod m.\n\t * This technique is proven to have the same asymptotic false positive rate as\n\t * k independent hash functions.\n\t *\n\t * We derive h1 and h2 from a single FNV-1a hash by splitting and mixing:\n\t * h1 = fnv1a(key), h2 = fnv1a(key + \"\\0salt\") to ensure independence.\n\t */\n\tprivate getPositions(key: string): number[] {\n\t\tconst h1 = fnv1a32(key)\n\t\t// Use a null-byte separator + salt suffix to derive a second independent hash\n\t\tconst h2 = fnv1a32(`${key}\\0bloom`)\n\n\t\tconst positions = new Array<number>(this.hashCount)\n\t\tfor (let i = 0; i < this.hashCount; i++) {\n\t\t\t// Combine h1 and h2 with Kirsch-Mitzenmacker technique\n\t\t\tpositions[i] = ((h1 + Math.imul(i, h2)) >>> 0) % this.bitCount\n\t\t}\n\t\treturn positions\n\t}\n}\n","import type { Operation } from '@korajs/core'\nimport type {\n\tCollectionRecord,\n\tQueryDescriptor,\n\tSubscription,\n\tSubscriptionCallback,\n} from '../types'\nimport { SubscriptionBloomFilter } from './bloom-filter'\n\nlet nextSubId = 0\n\n/**\n * Default threshold for activating bloom filter-based dependency tracking.\n * Below this count, linear scanning is faster due to bloom filter rebuild overhead.\n */\nconst DEFAULT_BLOOM_THRESHOLD = 100\n\n/**\n * Default expected items for bloom filter sizing.\n * Sized to handle typical subscription dependency counts with headroom.\n */\nconst DEFAULT_BLOOM_EXPECTED_ITEMS = 500\n\n/**\n * Default false positive rate for bloom filter.\n * 1% provides a good balance between filter size and accuracy.\n */\nconst DEFAULT_BLOOM_FALSE_POSITIVE_RATE = 0.01\n\n/**\n * Configuration options for the SubscriptionManager.\n */\nexport interface SubscriptionManagerOptions {\n\t/**\n\t * Minimum number of subscriptions before activating bloom filter.\n\t * Below this threshold, linear scanning is used (bloom filter overhead not worth it).\n\t * @default 100\n\t */\n\tbloomThreshold?: number\n\n\t/**\n\t * Expected number of unique collection+field dependencies for bloom filter sizing.\n\t * @default 500\n\t */\n\tbloomExpectedItems?: number\n\n\t/**\n\t * Target false positive rate for the bloom filter.\n\t * Lower values require more memory but reduce unnecessary precise checks.\n\t * @default 0.01\n\t */\n\tbloomFalsePositiveRate?: number\n\n\t/** Called when a query subscription is registered (e.g. to register sync query subsets). */\n\tonQuerySubscribed?: (descriptor: QueryDescriptor) => () => void\n}\n\n/**\n * Performance statistics for monitoring subscription checking efficiency.\n */\nexport interface SubscriptionStats {\n\t/** Total number of mutation notifications processed */\n\ttotalChecks: number\n\t/** Number of times bloom filter said \"maybe\" (proceeded to precise check) */\n\tbloomFilterHits: number\n\t/** Number of times bloom filter said \"definitely not\" (skipped all subscriptions) */\n\tbloomFilterMisses: number\n\t/** Number of times bloom filter said \"maybe\" but precise check found no match */\n\tfalsePositives: number\n\t/** Average time per check in milliseconds */\n\taverageCheckTimeMs: number\n\t/** Whether bloom filter is currently active */\n\tbloomFilterActive: boolean\n\t/** Current subscription count */\n\tsubscriptionCount: number\n}\n\n/**\n * Manages reactive subscriptions with two-level dependency checking.\n *\n * When a mutation occurs on a collection, affected subscriptions are re-evaluated\n * in a microtask batch and callbacks are invoked only if results actually changed.\n *\n * For large subscription counts (>= bloomThreshold), a bloom filter provides O(k)\n * pre-filtering to avoid scanning all subscriptions on every mutation:\n *\n * Level 1 (Bloom filter): Fast O(k) check -- does this mutation potentially affect\n * any subscription? If NO: skip all subscriptions (guaranteed correct).\n * If MAYBE: proceed to Level 2.\n *\n * Level 2 (Precise check): Only evaluate subscriptions that match the mutated\n * collection, including included (related) collection tracking.\n */\nexport class SubscriptionManager {\n\tprivate subscriptions = new Map<string, Subscription>()\n\tprivate pendingCollections = new Set<string>()\n\tprivate flushScheduled = false\n\tprivate readonly onQuerySubscribed?: (descriptor: QueryDescriptor) => () => void\n\n\t// Bloom filter state\n\tprivate bloomFilter: SubscriptionBloomFilter | null = null\n\tprivate bloomDirty = false\n\tprivate readonly bloomThreshold: number\n\tprivate readonly bloomExpectedItems: number\n\tprivate readonly bloomFalsePositiveRate: number\n\n\t// Performance stats\n\tprivate totalChecks = 0\n\tprivate bloomFilterHits = 0\n\tprivate bloomFilterMisses = 0\n\tprivate falsePositives = 0\n\tprivate totalCheckTimeMs = 0\n\n\tconstructor(options?: SubscriptionManagerOptions) {\n\t\tthis.bloomThreshold = options?.bloomThreshold ?? DEFAULT_BLOOM_THRESHOLD\n\t\tthis.bloomExpectedItems = options?.bloomExpectedItems ?? DEFAULT_BLOOM_EXPECTED_ITEMS\n\t\tthis.bloomFalsePositiveRate =\n\t\t\toptions?.bloomFalsePositiveRate ?? DEFAULT_BLOOM_FALSE_POSITIVE_RATE\n\t\tthis.onQuerySubscribed = options?.onQuerySubscribed\n\t}\n\n\t/**\n\t * Register a new subscription.\n\t *\n\t * @param descriptor - The query descriptor defining what this subscription watches\n\t * @param callback - Called with results whenever they change\n\t * @param executeFn - Function to re-execute the query and get current results\n\t * @returns An unsubscribe function\n\t */\n\tregister(\n\t\tdescriptor: QueryDescriptor,\n\t\tcallback: SubscriptionCallback<CollectionRecord>,\n\t\texecuteFn: () => Promise<CollectionRecord[]>,\n\t): () => void {\n\t\tconst id = `sub_${++nextSubId}`\n\t\tconst subscription: Subscription = {\n\t\t\tid,\n\t\t\tdescriptor,\n\t\t\tcallback,\n\t\t\texecuteFn,\n\t\t\tlastResults: [],\n\t\t}\n\t\tthis.subscriptions.set(id, subscription)\n\n\t\t// Mark bloom filter as needing rebuild since dependencies changed\n\t\tthis.bloomDirty = true\n\n\t\tconst externalCleanup = this.onQuerySubscribed?.(descriptor)\n\n\t\treturn () => {\n\t\t\tthis.subscriptions.delete(id)\n\t\t\tthis.bloomDirty = true\n\t\t\texternalCleanup?.()\n\t\t}\n\t}\n\n\t/**\n\t * Register a subscription and immediately execute the query.\n\t * The initial results are stored as lastResults so subsequent flushes\n\t * correctly diff against the initial state.\n\t *\n\t * @returns An unsubscribe function\n\t */\n\tregisterAndFetch(\n\t\tdescriptor: QueryDescriptor,\n\t\tcallback: SubscriptionCallback<CollectionRecord>,\n\t\texecuteFn: () => Promise<CollectionRecord[]>,\n\t): () => void {\n\t\tconst id = `sub_${++nextSubId}`\n\t\tconst subscription: Subscription = {\n\t\t\tid,\n\t\t\tdescriptor,\n\t\t\tcallback,\n\t\t\texecuteFn,\n\t\t\tlastResults: [],\n\t\t}\n\t\tthis.subscriptions.set(id, subscription)\n\n\t\t// Mark bloom filter as needing rebuild since dependencies changed\n\t\tthis.bloomDirty = true\n\n\t\tconst externalCleanup = this.onQuerySubscribed?.(descriptor)\n\n\t\t// Execute immediately, set lastResults, and call callback\n\t\texecuteFn().then((results) => {\n\t\t\t// Guard: subscription may have been removed before the async fetch completes\n\t\t\tif (this.subscriptions.has(id)) {\n\t\t\t\tsubscription.lastResults = results\n\t\t\t\tcallback(results)\n\t\t\t}\n\t\t})\n\n\t\treturn () => {\n\t\t\tthis.subscriptions.delete(id)\n\t\t\tthis.bloomDirty = true\n\t\t\texternalCleanup?.()\n\t\t}\n\t}\n\n\t/**\n\t * Notify the manager that a mutation occurred on a collection.\n\t * Schedules a microtask flush to batch multiple mutations in the same tick.\n\t */\n\tnotify(collection: string, _operation: Operation): void {\n\t\tthis.pendingCollections.add(collection)\n\t\tthis.scheduleFlush()\n\t}\n\n\t/**\n\t * Immediately flush all pending notifications.\n\t * Useful for testing. In production, flushing happens via microtask.\n\t */\n\tasync flush(): Promise<void> {\n\t\tif (this.pendingCollections.size === 0) return\n\n\t\tconst collections = new Set(this.pendingCollections)\n\t\tthis.pendingCollections.clear()\n\t\tthis.flushScheduled = false\n\n\t\tconst affected = this.findAffectedSubscriptions(collections)\n\n\t\t// Re-execute and diff\n\t\tfor (const sub of affected) {\n\t\t\ttry {\n\t\t\t\tconst newResults = await sub.executeFn()\n\t\t\t\tif (!this.resultsEqual(sub.lastResults, newResults)) {\n\t\t\t\t\tsub.lastResults = newResults\n\t\t\t\t\tsub.callback(newResults)\n\t\t\t\t}\n\t\t\t} catch {\n\t\t\t\t// Subscription re-execution failed -- skip silently for now.\n\t\t\t\t// In future, we could emit an error event for DevTools.\n\t\t\t}\n\t\t}\n\t}\n\n\t/**\n\t * Remove all subscriptions. Called on store close.\n\t */\n\tclear(): void {\n\t\tthis.subscriptions.clear()\n\t\tthis.pendingCollections.clear()\n\t\tthis.flushScheduled = false\n\t\tthis.bloomFilter = null\n\t\tthis.bloomDirty = false\n\t\tthis.totalChecks = 0\n\t\tthis.bloomFilterHits = 0\n\t\tthis.bloomFilterMisses = 0\n\t\tthis.falsePositives = 0\n\t\tthis.totalCheckTimeMs = 0\n\t}\n\n\t/** Number of active subscriptions (for testing/debugging) */\n\tget size(): number {\n\t\treturn this.subscriptions.size\n\t}\n\n\t/**\n\t * Get performance statistics for monitoring subscription checking efficiency.\n\t * Useful for DevTools integration and performance tuning.\n\t */\n\tgetStats(): SubscriptionStats {\n\t\treturn {\n\t\t\ttotalChecks: this.totalChecks,\n\t\t\tbloomFilterHits: this.bloomFilterHits,\n\t\t\tbloomFilterMisses: this.bloomFilterMisses,\n\t\t\tfalsePositives: this.falsePositives,\n\t\t\taverageCheckTimeMs: this.totalChecks > 0 ? this.totalCheckTimeMs / this.totalChecks : 0,\n\t\t\tbloomFilterActive: this.isBloomActive(),\n\t\t\tsubscriptionCount: this.subscriptions.size,\n\t\t}\n\t}\n\n\t/**\n\t * Check if bloom filter is currently active.\n\t * Active when subscription count meets or exceeds the threshold.\n\t */\n\tisBloomActive(): boolean {\n\t\treturn this.subscriptions.size >= this.bloomThreshold\n\t}\n\n\t/**\n\t * Find subscriptions affected by mutations to the given collections.\n\t * Uses two-level checking when bloom filter is active:\n\t *\n\t * Level 1: Bloom filter pre-check -- if no subscription depends on any\n\t * of the mutated collections, skip everything (O(k) per collection).\n\t *\n\t * Level 2: Precise check -- linear scan of subscriptions, matching\n\t * against the mutated collections.\n\t */\n\tprivate findAffectedSubscriptions(collections: Set<string>): Subscription[] {\n\t\tconst startTime = performance.now()\n\t\tthis.totalChecks++\n\n\t\tconst useBloom = this.isBloomActive()\n\n\t\tif (useBloom) {\n\t\t\t// Rebuild bloom filter if dependencies have changed\n\t\t\tif (this.bloomDirty || this.bloomFilter === null) {\n\t\t\t\tthis.rebuildBloomFilter()\n\t\t\t}\n\n\t\t\tconst filter = this.bloomFilter\n\t\t\tif (filter !== null) {\n\t\t\t\t// Level 1: Bloom filter pre-check\n\t\t\t\tlet anyPossibleMatch = false\n\t\t\t\tfor (const col of collections) {\n\t\t\t\t\tif (filter.mightContain(col)) {\n\t\t\t\t\t\tanyPossibleMatch = true\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (!anyPossibleMatch) {\n\t\t\t\t\t// Bloom filter guarantees no subscription depends on these collections\n\t\t\t\t\tthis.bloomFilterMisses++\n\t\t\t\t\tthis.totalCheckTimeMs += performance.now() - startTime\n\t\t\t\t\treturn []\n\t\t\t\t}\n\n\t\t\t\tthis.bloomFilterHits++\n\t\t\t}\n\t\t}\n\n\t\t// Level 2: Precise check (or only check when bloom is not active)\n\t\tconst affected: Subscription[] = []\n\t\tlet anyPreciseMatch = false\n\n\t\tfor (const sub of this.subscriptions.values()) {\n\t\t\tif (collections.has(sub.descriptor.collection)) {\n\t\t\t\taffected.push(sub)\n\t\t\t\tanyPreciseMatch = true\n\t\t\t} else if (sub.descriptor.includeCollections) {\n\t\t\t\t// Re-evaluate if a mutation affects an included (related) collection\n\t\t\t\tfor (const incCol of sub.descriptor.includeCollections) {\n\t\t\t\t\tif (collections.has(incCol)) {\n\t\t\t\t\t\taffected.push(sub)\n\t\t\t\t\t\tanyPreciseMatch = true\n\t\t\t\t\t\tbreak\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Track false positives: bloom said \"maybe\" but precise check found nothing\n\t\tif (useBloom && !anyPreciseMatch) {\n\t\t\tthis.falsePositives++\n\t\t}\n\n\t\tthis.totalCheckTimeMs += performance.now() - startTime\n\t\treturn affected\n\t}\n\n\t/**\n\t * Rebuild the bloom filter from all current subscriptions.\n\t * Adds collection-level dependencies for every subscription, plus\n\t * any included collection dependencies.\n\t */\n\tprivate rebuildBloomFilter(): void {\n\t\tconst filter = new SubscriptionBloomFilter(this.bloomExpectedItems, this.bloomFalsePositiveRate)\n\n\t\tfor (const sub of this.subscriptions.values()) {\n\t\t\t// Add the primary collection dependency\n\t\t\tfilter.add(sub.descriptor.collection)\n\n\t\t\t// Add included (related) collection dependencies\n\t\t\tif (sub.descriptor.includeCollections) {\n\t\t\t\tfor (const incCol of sub.descriptor.includeCollections) {\n\t\t\t\t\tfilter.add(incCol)\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tthis.bloomFilter = filter\n\t\tthis.bloomDirty = false\n\t}\n\n\tprivate scheduleFlush(): void {\n\t\tif (this.flushScheduled) return\n\t\tthis.flushScheduled = true\n\t\tqueueMicrotask(() => {\n\t\t\tthis.flush()\n\t\t})\n\t}\n\n\t/**\n\t * Compare two result sets. Uses ID-based comparison first (same length, same IDs),\n\t * then deep comparison of remaining fields when IDs match.\n\t * Avoids JSON.stringify which is O(n) and fragile to field ordering differences.\n\t */\n\tprivate resultsEqual(prev: CollectionRecord[], next: CollectionRecord[]): boolean {\n\t\tif (prev.length !== next.length) return false\n\t\tif (prev.length === 0) return true\n\n\t\t// Fast ID-based check: same records in same order means same result set\n\t\tfor (let i = 0; i < prev.length; i++) {\n\t\t\tif (prev[i]?.id !== next[i]?.id) return false\n\t\t}\n\n\t\t// IDs match — do a field-level comparison for safety\n\t\tfor (let i = 0; i < prev.length; i++) {\n\t\t\tconst a = prev[i] as Record<string, unknown>\n\t\t\tconst b = next[i] as Record<string, unknown>\n\t\t\tconst keysA = Object.keys(a)\n\t\t\tconst keysB = Object.keys(b)\n\t\t\tif (keysA.length !== keysB.length) return false\n\t\t\tfor (const key of keysA) {\n\t\t\t\tif (a[key] !== b[key]) return false\n\t\t\t}\n\t\t}\n\n\t\treturn true\n\t}\n}\n","import type {\n\tAtomicOp,\n\tCausalTracker,\n\tCollectionDefinition,\n\tHybridLogicalClock,\n\tOperation,\n\tSchemaDefinition,\n} from '@korajs/core'\nimport {\n\tcreateOperation,\n\tgenerateUUIDv7,\n\tisAtomicOp,\n\tresolveAtomicOp,\n\ttoAtomicOp,\n\tvalidateRecord,\n} from '@korajs/core'\nimport { RecordNotFoundError } from '../errors'\nimport { serializeRowVersion } from '../lww/row-version'\nimport { buildInsertQuery, buildSoftDeleteQuery, buildUpdateQuery } from '../query/sql-builder'\nimport type { RelationEnforcer } from '../relations/relation-enforcer'\nimport { deserializeRecord, serializeOperation, serializeRecord } from '../serialization/serializer'\nimport type {\n\tCollectionRecord,\n\tLocalMutationHandler,\n\tRawCollectionRow,\n\tStorageAdapter,\n\tTransaction,\n} from '../types'\n\n/**\n * A buffered SQL command to be executed during commit.\n */\ninterface BufferedCommand {\n\tsql: string\n\tparams: unknown[]\n}\n\n/**\n * A buffered operation with its associated SQL commands.\n */\ninterface BufferedEntry {\n\toperation: Operation\n\tcommands: BufferedCommand[]\n\tcollection: string\n}\n\n/**\n * Internal configuration for creating a TransactionContext.\n * Passed from Store to avoid exposing Store internals publicly.\n */\nexport interface TransactionContextConfig {\n\tschema: SchemaDefinition\n\tadapter: StorageAdapter\n\tclock: HybridLogicalClock\n\tnodeId: string\n\tsequenceAllocator: import('./transaction-sequence').TransactionSequenceAllocator\n\trelationEnforcer: RelationEnforcer | null\n\tcausalTracker: CausalTracker | null\n\tlocalMutationHandler: LocalMutationHandler | null\n}\n\n/**\n * A collection accessor within a transaction.\n * Operations are buffered and committed atomically when the transaction completes.\n */\nexport interface TransactionCollectionAccessor {\n\tinsert(data: Record<string, unknown>): Promise<CollectionRecord>\n\tupdate(id: string, data: Record<string, unknown>): Promise<CollectionRecord>\n\tdelete(id: string): Promise<void>\n\tfindById(id: string): Promise<CollectionRecord | null>\n}\n\n/**\n * TransactionContext provides atomic multi-collection operations.\n *\n * All mutations are buffered and committed in a single StorageAdapter.transaction()\n * call. All operations share the same transactionId (UUID v7).\n *\n * Subscription notifications are deferred until after commit.\n *\n * @example\n * ```typescript\n * const { operations, affectedCollections } = await txContext.commit()\n * // Notify subscriptions after commit\n * for (const op of operations) {\n * subscriptionManager.notify(op.collection, op)\n * }\n * ```\n */\nexport class TransactionContext {\n\tprivate readonly transactionId: string\n\tprivate mutationName: string | undefined\n\tprivate readonly buffer: BufferedEntry[] = []\n\tprivate committed = false\n\tprivate rolledBack = false\n\tprivate readonly config: TransactionContextConfig\n\n\tconstructor(config: TransactionContextConfig) {\n\t\tthis.config = config\n\t\tthis.transactionId = generateUUIDv7()\n\t}\n\n\t/**\n\t * Set a human-readable mutation name for this transaction.\n\t * Propagated to all operations for DevTools display.\n\t */\n\tsetMutationName(name: string): void {\n\t\tthis.mutationName = name\n\t}\n\n\t/**\n\t * Get the mutation name, if set.\n\t */\n\tgetMutationName(): string | undefined {\n\t\treturn this.mutationName\n\t}\n\n\t/**\n\t * Get a collection accessor for buffered operations within this transaction.\n\t */\n\tcollection(name: string): TransactionCollectionAccessor {\n\t\tconst definition = this.config.schema.collections[name]\n\t\tif (!definition) {\n\t\t\tthrow new Error(\n\t\t\t\t`Unknown collection \"${name}\". Available: ${Object.keys(this.config.schema.collections).join(', ')}`,\n\t\t\t)\n\t\t}\n\n\t\treturn {\n\t\t\tinsert: (data: Record<string, unknown>) => this.insert(name, definition, data),\n\t\t\tupdate: (id: string, data: Record<string, unknown>) =>\n\t\t\t\tthis.update(name, definition, id, data),\n\t\t\tdelete: (id: string) => this.deleteRecord(name, definition, id),\n\t\t\tfindById: (id: string) => this.findById(name, definition, id),\n\t\t}\n\t}\n\n\t/**\n\t * Commit all buffered operations atomically.\n\t * Returns the list of operations and affected collections for subscription notification.\n\t */\n\tasync commit(): Promise<{ operations: Operation[]; affectedCollections: Set<string> }> {\n\t\tif (this.committed) {\n\t\t\tthrow new Error('Transaction already committed.')\n\t\t}\n\t\tif (this.rolledBack) {\n\t\t\tthrow new Error('Transaction was rolled back and cannot be committed.')\n\t\t}\n\n\t\tthis.committed = true\n\n\t\tif (this.buffer.length === 0) {\n\t\t\treturn { operations: [], affectedCollections: new Set() }\n\t\t}\n\n\t\tconst handler = this.config.localMutationHandler\n\t\tif (handler?.commitTransaction) {\n\t\t\treturn handler.commitTransaction({\n\t\t\t\tentries: this.buffer.map((entry) => ({\n\t\t\t\t\toperation: entry.operation,\n\t\t\t\t\tcommands: entry.commands,\n\t\t\t\t\tcollection: entry.collection,\n\t\t\t\t})),\n\t\t\t\ttransactionId: this.transactionId,\n\t\t\t\t...(this.mutationName !== undefined ? { mutationName: this.mutationName } : {}),\n\t\t\t})\n\t\t}\n\n\t\tconst operations: Operation[] = []\n\t\tconst affectedCollections = new Set<string>()\n\n\t\t// Execute all buffered commands in a single adapter transaction\n\t\tawait this.config.adapter.transaction(async (tx: Transaction) => {\n\t\t\tfor (const entry of this.buffer) {\n\t\t\t\tif (entry.operation.type === 'delete' && this.config.relationEnforcer) {\n\t\t\t\t\tconst cascadeResult = await this.config.relationEnforcer.enforceDelete(\n\t\t\t\t\t\tentry.collection,\n\t\t\t\t\t\tentry.operation.recordId,\n\t\t\t\t\t\ttx,\n\t\t\t\t\t\t[entry.operation.id],\n\t\t\t\t\t)\n\t\t\t\t\tfor (const cascadedOp of cascadeResult.operations) {\n\t\t\t\t\t\toperations.push(cascadedOp)\n\t\t\t\t\t\taffectedCollections.add(cascadedOp.collection)\n\t\t\t\t\t\tthis.config.causalTracker?.afterOperation(cascadedOp.collection, cascadedOp.id, true)\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tfor (const cmd of entry.commands) {\n\t\t\t\t\tawait tx.execute(cmd.sql, cmd.params)\n\t\t\t\t}\n\t\t\t\toperations.push(entry.operation)\n\t\t\t\taffectedCollections.add(entry.collection)\n\t\t\t}\n\n\t\t\tconst finalSeq = this.config.sequenceAllocator.getHighWaterMark()\n\t\t\tif (finalSeq > 0) {\n\t\t\t\tawait tx.execute(\n\t\t\t\t\t'INSERT OR REPLACE INTO _kora_version_vector (node_id, sequence_number) VALUES (?, ?)',\n\t\t\t\t\t[this.config.nodeId, finalSeq],\n\t\t\t\t)\n\t\t\t}\n\t\t})\n\n\t\treturn { operations, affectedCollections }\n\t}\n\n\t/**\n\t * Mark the transaction as rolled back. No operations will be committed.\n\t */\n\trollback(): void {\n\t\tthis.rolledBack = true\n\t\tthis.buffer.length = 0\n\t}\n\n\t/**\n\t * Get the transaction ID shared by all operations in this transaction.\n\t */\n\tgetTransactionId(): string {\n\t\treturn this.transactionId\n\t}\n\n\tprivate ensureActive(): void {\n\t\tif (this.committed) {\n\t\t\tthrow new Error('Cannot perform operations on a committed transaction.')\n\t\t}\n\t\tif (this.rolledBack) {\n\t\t\tthrow new Error('Cannot perform operations on a rolled-back transaction.')\n\t\t}\n\t}\n\n\tprivate async insert(\n\t\tcollectionName: string,\n\t\tdefinition: CollectionDefinition,\n\t\tdata: Record<string, unknown>,\n\t): Promise<CollectionRecord> {\n\t\tthis.ensureActive()\n\n\t\tconst validated = validateRecord(collectionName, definition, data, 'insert')\n\t\tconst recordId = generateUUIDv7()\n\t\tconst now = Date.now()\n\n\t\t// Set auto timestamp fields\n\t\tfor (const [fieldName, descriptor] of Object.entries(definition.fields)) {\n\t\t\tif (descriptor.auto && descriptor.kind === 'timestamp') {\n\t\t\t\tvalidated[fieldName] = now\n\t\t\t}\n\t\t}\n\n\t\tconst sequenceNumber = await this.config.sequenceAllocator.allocate()\n\t\tconst causalDeps = this.config.causalTracker?.nextCausalDeps(collectionName, true) ?? []\n\t\tconst operation = await createOperation(\n\t\t\t{\n\t\t\t\tnodeId: this.config.nodeId,\n\t\t\t\ttype: 'insert',\n\t\t\t\tcollection: collectionName,\n\t\t\t\trecordId,\n\t\t\t\tdata: { ...validated },\n\t\t\t\tpreviousData: null,\n\t\t\t\tsequenceNumber,\n\t\t\t\tcausalDeps,\n\t\t\t\tschemaVersion: this.config.schema.version,\n\t\t\t\ttransactionId: this.transactionId,\n\t\t\t\t...(this.mutationName !== undefined ? { mutationName: this.mutationName } : {}),\n\t\t\t},\n\t\t\tthis.config.clock,\n\t\t)\n\t\tthis.config.causalTracker?.afterOperation(collectionName, operation.id, true)\n\n\t\tconst serializedData = serializeRecord(validated, definition.fields)\n\t\tconst version = serializeRowVersion(operation.timestamp)\n\t\tconst record: Record<string, unknown> = {\n\t\t\tid: recordId,\n\t\t\t...serializedData,\n\t\t\t_created_at: operation.timestamp.wallTime,\n\t\t\t_updated_at: operation.timestamp.wallTime,\n\t\t\t_version: version,\n\t\t}\n\n\t\tconst insertQuery = buildInsertQuery(collectionName, record)\n\t\tconst opRow = serializeOperation(operation)\n\t\tconst opInsert = buildInsertQuery(\n\t\t\t`_kora_ops_${collectionName}`,\n\t\t\topRow as unknown as Record<string, unknown>,\n\t\t)\n\n\t\tthis.buffer.push({\n\t\t\toperation,\n\t\t\tcollection: collectionName,\n\t\t\tcommands: [\n\t\t\t\t{ sql: insertQuery.sql, params: insertQuery.params },\n\t\t\t\t{ sql: opInsert.sql, params: opInsert.params },\n\t\t\t],\n\t\t})\n\n\t\treturn {\n\t\t\tid: recordId,\n\t\t\t...validated,\n\t\t\tcreatedAt: now,\n\t\t\tupdatedAt: now,\n\t\t}\n\t}\n\n\tprivate async update(\n\t\tcollectionName: string,\n\t\tdefinition: CollectionDefinition,\n\t\tid: string,\n\t\tdata: Record<string, unknown>,\n\t): Promise<CollectionRecord> {\n\t\tthis.ensureActive()\n\n\t\t// Check for buffered inserts/updates for this record to get current state\n\t\tconst currentRecord = await this.getEffectiveRecord(collectionName, definition, id)\n\t\tif (!currentRecord) {\n\t\t\tthrow new RecordNotFoundError(collectionName, id)\n\t\t}\n\n\t\tconst validated = validateRecord(collectionName, definition, data, 'update')\n\t\tconst now = Date.now()\n\n\t\tconst previousData: Record<string, unknown> = {}\n\t\tconst resolvedData: Record<string, unknown> = {}\n\t\tconst atomicOps: Record<string, AtomicOp> = {}\n\n\t\tfor (const key of Object.keys(validated)) {\n\t\t\tconst value = validated[key]\n\t\t\tpreviousData[key] = currentRecord[key]\n\n\t\t\tif (isAtomicOp(value)) {\n\t\t\t\tresolvedData[key] = resolveAtomicOp(currentRecord[key], value)\n\t\t\t\tatomicOps[key] = toAtomicOp(value)\n\t\t\t} else {\n\t\t\t\tresolvedData[key] = value\n\t\t\t}\n\t\t}\n\n\t\tconst hasAtomicOps = Object.keys(atomicOps).length > 0\n\n\t\tconst sequenceNumber = await this.config.sequenceAllocator.allocate()\n\t\tconst causalDeps = this.config.causalTracker?.nextCausalDeps(collectionName, true) ?? []\n\t\tconst operation = await createOperation(\n\t\t\t{\n\t\t\t\tnodeId: this.config.nodeId,\n\t\t\t\ttype: 'update',\n\t\t\t\tcollection: collectionName,\n\t\t\t\trecordId: id,\n\t\t\t\tdata: { ...resolvedData },\n\t\t\t\tpreviousData,\n\t\t\t\tsequenceNumber,\n\t\t\t\tcausalDeps,\n\t\t\t\tschemaVersion: this.config.schema.version,\n\t\t\t\ttransactionId: this.transactionId,\n\t\t\t\t...(this.mutationName !== undefined ? { mutationName: this.mutationName } : {}),\n\t\t\t\t...(hasAtomicOps ? { atomicOps } : {}),\n\t\t\t},\n\t\t\tthis.config.clock,\n\t\t)\n\t\tthis.config.causalTracker?.afterOperation(collectionName, operation.id, true)\n\n\t\tconst serializedChanges = serializeRecord(resolvedData, definition.fields)\n\t\tconst version = serializeRowVersion(operation.timestamp)\n\t\tconst updateQuery = buildUpdateQuery(collectionName, id, {\n\t\t\t...serializedChanges,\n\t\t\t_updated_at: operation.timestamp.wallTime,\n\t\t\t_version: version,\n\t\t})\n\t\tconst opRow = serializeOperation(operation)\n\t\tconst opInsert = buildInsertQuery(\n\t\t\t`_kora_ops_${collectionName}`,\n\t\t\topRow as unknown as Record<string, unknown>,\n\t\t)\n\n\t\tthis.buffer.push({\n\t\t\toperation,\n\t\t\tcollection: collectionName,\n\t\t\tcommands: [\n\t\t\t\t{ sql: updateQuery.sql, params: updateQuery.params },\n\t\t\t\t{ sql: opInsert.sql, params: opInsert.params },\n\t\t\t],\n\t\t})\n\n\t\t// Merge current record with resolved changes for return value\n\t\treturn {\n\t\t\t...currentRecord,\n\t\t\t...resolvedData,\n\t\t\tupdatedAt: now,\n\t\t} as CollectionRecord\n\t}\n\n\tprivate async deleteRecord(\n\t\tcollectionName: string,\n\t\tdefinition: CollectionDefinition,\n\t\tid: string,\n\t): Promise<void> {\n\t\tthis.ensureActive()\n\n\t\tconst currentRecord = await this.getEffectiveRecord(collectionName, definition, id)\n\t\tif (!currentRecord) {\n\t\t\tthrow new RecordNotFoundError(collectionName, id)\n\t\t}\n\n\t\tconst now = Date.now()\n\t\tconst sequenceNumber = await this.config.sequenceAllocator.allocate()\n\t\tconst causalDeps = this.config.causalTracker?.nextCausalDeps(collectionName, true) ?? []\n\t\tconst operation = await createOperation(\n\t\t\t{\n\t\t\t\tnodeId: this.config.nodeId,\n\t\t\t\ttype: 'delete',\n\t\t\t\tcollection: collectionName,\n\t\t\t\trecordId: id,\n\t\t\t\tdata: null,\n\t\t\t\tpreviousData: null,\n\t\t\t\tsequenceNumber,\n\t\t\t\tcausalDeps,\n\t\t\t\tschemaVersion: this.config.schema.version,\n\t\t\t\ttransactionId: this.transactionId,\n\t\t\t\t...(this.mutationName !== undefined ? { mutationName: this.mutationName } : {}),\n\t\t\t},\n\t\t\tthis.config.clock,\n\t\t)\n\t\tthis.config.causalTracker?.afterOperation(collectionName, operation.id, true)\n\n\t\tconst deleteQuery = buildSoftDeleteQuery(collectionName, id, now)\n\t\tconst opRow = serializeOperation(operation)\n\t\tconst opInsert = buildInsertQuery(\n\t\t\t`_kora_ops_${collectionName}`,\n\t\t\topRow as unknown as Record<string, unknown>,\n\t\t)\n\n\t\tthis.buffer.push({\n\t\t\toperation,\n\t\t\tcollection: collectionName,\n\t\t\tcommands: [\n\t\t\t\t{ sql: deleteQuery.sql, params: deleteQuery.params },\n\t\t\t\t{ sql: opInsert.sql, params: opInsert.params },\n\t\t\t],\n\t\t})\n\t}\n\n\tprivate async findById(\n\t\tcollectionName: string,\n\t\tdefinition: CollectionDefinition,\n\t\tid: string,\n\t): Promise<CollectionRecord | null> {\n\t\treturn this.getEffectiveRecord(collectionName, definition, id)\n\t}\n\n\t/**\n\t * Get the effective state of a record, considering both the database and buffered operations.\n\t * Buffered inserts/updates take precedence over the database state.\n\t */\n\tprivate async getEffectiveRecord(\n\t\tcollectionName: string,\n\t\tdefinition: CollectionDefinition,\n\t\tid: string,\n\t): Promise<CollectionRecord | null> {\n\t\t// Check if there's a buffered delete for this record (scan from newest to oldest)\n\t\tfor (let i = this.buffer.length - 1; i >= 0; i--) {\n\t\t\tconst entry = this.buffer[i] as BufferedEntry\n\t\t\tif (entry.collection === collectionName && entry.operation.recordId === id) {\n\t\t\t\tif (entry.operation.type === 'delete') {\n\t\t\t\t\treturn null\n\t\t\t\t}\n\t\t\t\t// For buffered inserts/updates, reconstruct the record from the database + buffered changes\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n\n\t\t// Read from database first\n\t\tconst rows = await this.config.adapter.query<RawCollectionRow>(\n\t\t\t`SELECT * FROM ${collectionName} WHERE id = ? AND _deleted = 0`,\n\t\t\t[id],\n\t\t)\n\n\t\tlet record: CollectionRecord | null = rows[0]\n\t\t\t? deserializeRecord(rows[0], definition.fields)\n\t\t\t: null\n\n\t\t// Apply buffered operations on top\n\t\tfor (const entry of this.buffer) {\n\t\t\tif (entry.collection !== collectionName || entry.operation.recordId !== id) {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\tif (entry.operation.type === 'insert' && entry.operation.data) {\n\t\t\t\trecord = {\n\t\t\t\t\tid,\n\t\t\t\t\t...entry.operation.data,\n\t\t\t\t\tcreatedAt: entry.operation.timestamp.wallTime,\n\t\t\t\t\tupdatedAt: entry.operation.timestamp.wallTime,\n\t\t\t\t}\n\t\t\t} else if (entry.operation.type === 'update' && entry.operation.data && record) {\n\t\t\t\trecord = {\n\t\t\t\t\t...record,\n\t\t\t\t\t...entry.operation.data,\n\t\t\t\t\tupdatedAt: entry.operation.timestamp.wallTime,\n\t\t\t\t}\n\t\t\t} else if (entry.operation.type === 'delete') {\n\t\t\t\trecord = null\n\t\t\t}\n\t\t}\n\n\t\treturn record\n\t}\n}\n","import { readSequenceNumber } from '../store/sequence-allocator'\nimport type { StorageAdapter } from '../types'\n\n/**\n * Allocates monotonic sequence numbers for a single open transaction.\n * Loads the current DB watermark once, then increments in memory until commit.\n */\nexport class TransactionSequenceAllocator {\n\tprivate loaded = false\n\tprivate watermark = 0\n\n\tconstructor(\n\t\tprivate readonly adapter: StorageAdapter,\n\t\tprivate readonly nodeId: string,\n\t) {}\n\n\tasync allocate(): Promise<number> {\n\t\tif (!this.loaded) {\n\t\t\tthis.watermark = await readSequenceNumber(this.adapter, this.nodeId)\n\t\t\tthis.loaded = true\n\t\t}\n\t\tthis.watermark++\n\t\treturn this.watermark\n\t}\n\n\tgetHighWaterMark(): number {\n\t\treturn this.watermark\n\t}\n}\n","import { generateUUIDv7 } from '@korajs/core'\n\nexport type StoreIsolation = 'shared' | 'per-tab'\n\nfunction perTabStorageKey(dbName: string): string {\n\treturn `kora-tab-node-${dbName}`\n}\n\n/**\n * Load a per-tab node id from sessionStorage (browser only).\n */\nexport function loadPerTabNodeId(dbName: string): string | null {\n\tif (typeof sessionStorage === 'undefined') {\n\t\treturn null\n\t}\n\treturn sessionStorage.getItem(perTabStorageKey(dbName))\n}\n\n/**\n * Persist a per-tab node id for this browser tab.\n */\nexport function savePerTabNodeId(dbName: string, nodeId: string): void {\n\tif (typeof sessionStorage === 'undefined') {\n\t\treturn\n\t}\n\tsessionStorage.setItem(perTabStorageKey(dbName), nodeId)\n}\n\n/**\n * Resolve or create a node id for `per-tab` isolation.\n */\nexport function resolvePerTabNodeId(dbName: string): string {\n\tconst existing = loadPerTabNodeId(dbName)\n\tif (existing) {\n\t\treturn existing\n\t}\n\tconst nodeId = generateUUIDv7()\n\tsavePerTabNodeId(dbName, nodeId)\n\treturn nodeId\n}\n","export { exportBackup, readBackupManifest, restoreBackup, verifyBackupChecksum } from './backup'\nexport type {\n\tBackupManifest,\n\tBackupOptions,\n\tBackupProgress,\n\tRestoreOptions,\n\tRestoreResult,\n} from './types'\n","export {\n\tappendAuditTrace,\n\tauditTraceIndexFields,\n\tcountAuditTraces,\n\tpersistedAuditTraceFromEvent,\n\treadAuditTraces,\n} from './audit-trace-store'\nexport {\n\tauditJsonReplacer,\n\tauditJsonReviver,\n\tdeserializeAuditJson,\n\tserializeAuditJson,\n} from './audit-json'\nexport {\n\tdecodeAuditExport,\n\texportAudit,\n\treadAuditExportManifest,\n\tverifyAuditExportChecksum,\n} from './export-audit'\nexport type {\n\tAuditExportManifest,\n\tAuditExportOptions,\n\tAuditExportPayload,\n\tAuditExportProgress,\n\tAuditTraceQuery,\n\tPersistedAuditTrace,\n} from './types'\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AASO,SAAS,eAAe,OAAyC;AACvE,MAAI,UAAU,QAAQ,UAAU,QAAW;AAC1C,WAAO;AAAA,EACR;AAEA,MAAI,OAAO,UAAU,UAAU;AAC9B,UAAM,MAAM,IAAM,MAAI;AACtB,QAAI,QAAQ,QAAQ,EAAE,OAAO,GAAG,KAAK;AACrC,WAAS,sBAAoB,GAAG;AAAA,EACjC;AAEA,MAAI,iBAAiB,YAAY;AAChC,WAAO;AAAA,EACR;AAEA,MAAI,iBAAiB,aAAa;AACjC,WAAO,IAAI,WAAW,KAAK;AAAA,EAC5B;AAEA,QAAM,IAAI,MAAM,+EAA+E;AAChG;AAKO,SAAS,eAAe,OAAmC;AACjE,MAAI,UAAU,QAAQ,UAAU,QAAW;AAC1C,WAAO;AAAA,EACR;AAEA,MAA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,MACvF;AAAA,IACD,CAAC;AACD,WAAO;AAAA,EACR;AAAA,EAEA,MAAc,qBAAsC;AACnD,UAAM,OAAO,MAAM,KAAK,QAAQ;AAAA,MAC/B;AAAA,MACA,CAAC,KAAK,MAAM;AAAA,IACb;AACA,WAAO,KAAK,CAAC,GAAG,mBAAmB;AAAA,EACpC;AAAA,EAEA,MAAc,oBAA4C;AACzD,UAAM,OAAO,MAAM,KAAK,QAAQ;AAAA,MAC/B;AAAA,IACD;AACA,UAAM,aAAS,mCAAoB;AACnC,eAAW,OAAO,MAAM;AACvB,aAAO,IAAI,IAAI,SAAS,IAAI,eAAe;AAAA,IAC5C;AACA,WAAO;AAAA,EACR;AAAA,EAEQ,aAAmB;AAC1B,QAAI,CAAC,KAAK,QAAQ;AACjB,YAAM,IAAI,kBAAkB;AAAA,IAC7B;AAAA,EACD;AACD;;;AL10BA;;;A0B3FA;;;ACAA;AAOA;AAMA;","names":["import_core","import_core","import_core","encodeSection","encodeJsonSection","readUint32","parseSections","findSection","computeSha256","readAllOperations","import_core","import_core","import_core","import_core","import_core","import_core","import_core","import_core","QueryError","propName","import_core","import_core","import_core","import_core","import_core","existing","readAuditTraces"]}
|