@tanstack/electric-db-collection 0.1.3 → 0.1.5

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.
@@ -8,6 +8,9 @@ const debug = DebugModule.debug(`ts/db:electric`);
8
8
  function isUpToDateMessage(message) {
9
9
  return client.isControlMessage(message) && message.headers.control === `up-to-date`;
10
10
  }
11
+ function isMustRefetchMessage(message) {
12
+ return client.isControlMessage(message) && message.headers.control === `must-refetch`;
13
+ }
11
14
  function hasTxids(message) {
12
15
  return `txids` in message.headers && Array.isArray(message.headers.txids);
13
16
  }
@@ -119,7 +122,7 @@ function createElectricSync(shapeOptions, options) {
119
122
  let unsubscribeStream;
120
123
  return {
121
124
  sync: (params) => {
122
- const { begin, write, commit, markReady } = params;
125
+ const { begin, write, commit, markReady, truncate } = params;
123
126
  const stream = new client.ShapeStream({
124
127
  ...shapeOptions,
125
128
  signal: abortController.signal,
@@ -159,6 +162,17 @@ function createElectricSync(shapeOptions, options) {
159
162
  });
160
163
  } else if (isUpToDateMessage(message)) {
161
164
  hasUpToDate = true;
165
+ } else if (isMustRefetchMessage(message)) {
166
+ debug(
167
+ `Received must-refetch message, starting transaction with truncate`
168
+ );
169
+ if (!transactionStarted) {
170
+ begin();
171
+ transactionStarted = true;
172
+ }
173
+ truncate();
174
+ commit();
175
+ transactionStarted = false;
162
176
  }
163
177
  }
164
178
  if (hasUpToDate) {
@@ -1 +1 @@
1
- {"version":3,"file":"electric.cjs","sources":["../../src/electric.ts"],"sourcesContent":["import {\n ShapeStream,\n isChangeMessage,\n isControlMessage,\n} from \"@electric-sql/client\"\nimport { Store } from \"@tanstack/store\"\nimport DebugModule from \"debug\"\nimport {\n ElectricDeleteHandlerMustReturnTxIdError,\n ElectricInsertHandlerMustReturnTxIdError,\n ElectricUpdateHandlerMustReturnTxIdError,\n ExpectedNumberInAwaitTxIdError,\n TimeoutWaitingForTxIdError,\n} from \"./errors\"\nimport type {\n CollectionConfig,\n DeleteMutationFnParams,\n InsertMutationFnParams,\n SyncConfig,\n UpdateMutationFnParams,\n UtilsRecord,\n} from \"@tanstack/db\"\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\"\nimport type {\n ControlMessage,\n GetExtensions,\n Message,\n Row,\n ShapeStreamOptions,\n} from \"@electric-sql/client\"\n\nconst debug = DebugModule.debug(`ts/db:electric`)\n\n/**\n * Type representing a transaction ID in ElectricSQL\n */\nexport type Txid = number\n\n// The `InferSchemaOutput` and `ResolveType` are copied from the `@tanstack/db` package\n// but we modified `InferSchemaOutput` slightly to restrict the schema output to `Row<unknown>`\n// This is needed in order for `GetExtensions` to be able to infer the parser extensions type from the schema\ntype InferSchemaOutput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferOutput<T> extends Row<unknown>\n ? StandardSchemaV1.InferOutput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\ntype ResolveType<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends object = Record<string, unknown>,\n> =\n unknown extends GetExtensions<TExplicit>\n ? [TSchema] extends [never]\n ? TFallback\n : InferSchemaOutput<TSchema>\n : TExplicit\n\n/**\n * Configuration interface for Electric collection options\n * @template TExplicit - The explicit type of items in the collection (highest priority)\n * @template TSchema - The schema type for validation and type inference (second priority)\n * @template TFallback - The fallback type if no explicit or schema type is provided\n *\n * @remarks\n * Type resolution follows a priority order:\n * 1. If you provide an explicit type via generic parameter, it will be used\n * 2. If no explicit type is provided but a schema is, the schema's output type will be inferred\n * 3. If neither explicit type nor schema is provided, the fallback type will be used\n *\n * You should provide EITHER an explicit type OR a schema, but not both, as they would conflict.\n */\nexport interface ElectricCollectionConfig<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends Row<unknown> = Row<unknown>,\n> {\n /**\n * Configuration options for the ElectricSQL ShapeStream\n */\n shapeOptions: ShapeStreamOptions<\n GetExtensions<ResolveType<TExplicit, TSchema, TFallback>>\n >\n\n /**\n * All standard Collection configuration properties\n */\n id?: string\n schema?: TSchema\n getKey: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`getKey`]\n sync?: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`sync`]\n\n /**\n * Optional asynchronous handler function called before an insert operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric insert handler - MUST return { txid: number }\n * onInsert: async ({ transaction }) => {\n * const newItem = transaction.mutations[0].modified\n * const result = await api.todos.create({\n * data: newItem\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Insert handler with multiple items - return array of txids\n * onInsert: async ({ transaction }) => {\n * const items = transaction.mutations.map(m => m.modified)\n * const results = await Promise.all(\n * items.map(item => api.todos.create({ data: item }))\n * )\n * return { txid: results.map(r => r.txid) } // Array of txids\n * }\n *\n * @example\n * // Insert handler with error handling\n * onInsert: async ({ transaction }) => {\n * try {\n * const newItem = transaction.mutations[0].modified\n * const result = await api.createTodo(newItem)\n * return { txid: result.txid }\n * } catch (error) {\n * console.error('Insert failed:', error)\n * throw error // This will cause the transaction to fail\n * }\n * }\n *\n * @example\n * // Insert handler with batch operation - single txid\n * onInsert: async ({ transaction }) => {\n * const items = transaction.mutations.map(m => m.modified)\n * const result = await api.todos.createMany({\n * data: items\n * })\n * return { txid: result.txid } // Single txid for batch operation\n * }\n */\n onInsert?: (\n params: InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n\n /**\n * Optional asynchronous handler function called before an update operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric update handler - MUST return { txid: number }\n * onUpdate: async ({ transaction }) => {\n * const { original, changes } = transaction.mutations[0]\n * const result = await api.todos.update({\n * where: { id: original.id },\n * data: changes // Only the changed fields\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Update handler with multiple items - return array of txids\n * onUpdate: async ({ transaction }) => {\n * const updates = await Promise.all(\n * transaction.mutations.map(m =>\n * api.todos.update({\n * where: { id: m.original.id },\n * data: m.changes\n * })\n * )\n * )\n * return { txid: updates.map(u => u.txid) } // Array of txids\n * }\n *\n * @example\n * // Update handler with optimistic rollback\n * onUpdate: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * try {\n * const result = await api.updateTodo(mutation.original.id, mutation.changes)\n * return { txid: result.txid }\n * } catch (error) {\n * // Transaction will automatically rollback optimistic changes\n * console.error('Update failed, rolling back:', error)\n * throw error\n * }\n * }\n */\n onUpdate?: (\n params: UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n\n /**\n * Optional asynchronous handler function called before a delete operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric delete handler - MUST return { txid: number }\n * onDelete: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * const result = await api.todos.delete({\n * id: mutation.original.id\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Delete handler with multiple items - return array of txids\n * onDelete: async ({ transaction }) => {\n * const deletes = await Promise.all(\n * transaction.mutations.map(m =>\n * api.todos.delete({\n * where: { id: m.key }\n * })\n * )\n * )\n * return { txid: deletes.map(d => d.txid) } // Array of txids\n * }\n *\n * @example\n * // Delete handler with batch operation - single txid\n * onDelete: async ({ transaction }) => {\n * const idsToDelete = transaction.mutations.map(m => m.original.id)\n * const result = await api.todos.deleteMany({\n * ids: idsToDelete\n * })\n * return { txid: result.txid } // Single txid for batch operation\n * }\n *\n * @example\n * // Delete handler with optimistic rollback\n * onDelete: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * try {\n * const result = await api.deleteTodo(mutation.original.id)\n * return { txid: result.txid }\n * } catch (error) {\n * // Transaction will automatically rollback optimistic changes\n * console.error('Delete failed, rolling back:', error)\n * throw error\n * }\n * }\n *\n */\n onDelete?: (\n params: DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n}\n\nfunction isUpToDateMessage<T extends Row<unknown>>(\n message: Message<T>\n): message is ControlMessage & { up_to_date: true } {\n return isControlMessage(message) && message.headers.control === `up-to-date`\n}\n\n// Check if a message contains txids in its headers\nfunction hasTxids<T extends Row<unknown>>(\n message: Message<T>\n): message is Message<T> & { headers: { txids?: Array<Txid> } } {\n return `txids` in message.headers && Array.isArray(message.headers.txids)\n}\n\n/**\n * Type for the awaitTxId utility function\n */\nexport type AwaitTxIdFn = (txId: Txid, timeout?: number) => Promise<boolean>\n\n/**\n * Electric collection utilities type\n */\nexport interface ElectricCollectionUtils extends UtilsRecord {\n awaitTxId: AwaitTxIdFn\n}\n\n/**\n * Creates Electric collection options for use with a standard Collection\n *\n * @template TExplicit - The explicit type of items in the collection (highest priority)\n * @template TSchema - The schema type for validation and type inference (second priority)\n * @template TFallback - The fallback type if no explicit or schema type is provided\n * @param config - Configuration options for the Electric collection\n * @returns Collection options with utilities\n */\nexport function electricCollectionOptions<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends Row<unknown> = Row<unknown>,\n>(config: ElectricCollectionConfig<TExplicit, TSchema, TFallback>) {\n const seenTxids = new Store<Set<Txid>>(new Set([]))\n const sync = createElectricSync<ResolveType<TExplicit, TSchema, TFallback>>(\n config.shapeOptions,\n {\n seenTxids,\n }\n )\n\n /**\n * Wait for a specific transaction ID to be synced\n * @param txId The transaction ID to wait for as a number\n * @param timeout Optional timeout in milliseconds (defaults to 30000ms)\n * @returns Promise that resolves when the txId is synced\n */\n const awaitTxId: AwaitTxIdFn = async (\n txId: Txid,\n timeout: number = 30000\n ): Promise<boolean> => {\n debug(`awaitTxId called with txid %d`, txId)\n if (typeof txId !== `number`) {\n throw new ExpectedNumberInAwaitTxIdError(typeof txId)\n }\n\n const hasTxid = seenTxids.state.has(txId)\n if (hasTxid) return true\n\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n unsubscribe()\n reject(new TimeoutWaitingForTxIdError(txId))\n }, timeout)\n\n const unsubscribe = seenTxids.subscribe(() => {\n if (seenTxids.state.has(txId)) {\n debug(`awaitTxId found match for txid %o`, txId)\n clearTimeout(timeoutId)\n unsubscribe()\n resolve(true)\n }\n })\n })\n }\n\n // Create wrapper handlers for direct persistence operations that handle txid awaiting\n const wrappedOnInsert = config.onInsert\n ? async (\n params: InsertMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n // Runtime check (that doesn't follow type)\n // eslint-disable-next-line\n const handlerResult = (await config.onInsert!(params)) ?? {}\n const txid = (handlerResult as { txid?: Txid | Array<Txid> }).txid\n\n if (!txid) {\n throw new ElectricInsertHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(txid)) {\n await Promise.all(txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(txid)\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnUpdate = config.onUpdate\n ? async (\n params: UpdateMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n // Runtime check (that doesn't follow type)\n // eslint-disable-next-line\n const handlerResult = (await config.onUpdate!(params)) ?? {}\n const txid = (handlerResult as { txid?: Txid | Array<Txid> }).txid\n\n if (!txid) {\n throw new ElectricUpdateHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(txid)) {\n await Promise.all(txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(txid)\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnDelete = config.onDelete\n ? async (\n params: DeleteMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n const handlerResult = await config.onDelete!(params)\n if (!handlerResult.txid) {\n throw new ElectricDeleteHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(handlerResult.txid)) {\n await Promise.all(handlerResult.txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(handlerResult.txid)\n }\n\n return handlerResult\n }\n : undefined\n\n // Extract standard Collection config properties\n const {\n shapeOptions: _shapeOptions,\n onInsert: _onInsert,\n onUpdate: _onUpdate,\n onDelete: _onDelete,\n ...restConfig\n } = config\n\n return {\n ...restConfig,\n sync,\n onInsert: wrappedOnInsert,\n onUpdate: wrappedOnUpdate,\n onDelete: wrappedOnDelete,\n utils: {\n awaitTxId,\n },\n }\n}\n\n/**\n * Internal function to create ElectricSQL sync configuration\n */\nfunction createElectricSync<T extends Row<unknown>>(\n shapeOptions: ShapeStreamOptions<GetExtensions<T>>,\n options: {\n seenTxids: Store<Set<Txid>>\n }\n): SyncConfig<T> {\n const { seenTxids } = options\n\n // Store for the relation schema information\n const relationSchema = new Store<string | undefined>(undefined)\n\n /**\n * Get the sync metadata for insert operations\n * @returns Record containing relation information\n */\n const getSyncMetadata = (): Record<string, unknown> => {\n // Use the stored schema if available, otherwise default to 'public'\n const schema = relationSchema.state || `public`\n\n return {\n relation: shapeOptions.params?.table\n ? [schema, shapeOptions.params.table]\n : undefined,\n }\n }\n\n // Abort controller for the stream - wraps the signal if provided\n const abortController = new AbortController()\n if (shapeOptions.signal) {\n shapeOptions.signal.addEventListener(`abort`, () => {\n abortController.abort()\n })\n if (shapeOptions.signal.aborted) {\n abortController.abort()\n }\n }\n\n let unsubscribeStream: () => void\n\n return {\n sync: (params: Parameters<SyncConfig<T>[`sync`]>[0]) => {\n const { begin, write, commit, markReady } = params\n const stream = new ShapeStream({\n ...shapeOptions,\n signal: abortController.signal,\n onError: (errorParams) => {\n // Just immediately mark ready if there's an error to avoid blocking\n // apps waiting for `.preload()` to finish.\n markReady()\n\n if (shapeOptions.onError) {\n return shapeOptions.onError(errorParams)\n }\n\n return\n },\n })\n let transactionStarted = false\n const newTxids = new Set<Txid>()\n\n unsubscribeStream = stream.subscribe((messages: Array<Message<T>>) => {\n let hasUpToDate = false\n\n for (const message of messages) {\n // Check for txids in the message and add them to our store\n if (hasTxids(message)) {\n message.headers.txids?.forEach((txid) => newTxids.add(txid))\n }\n\n if (isChangeMessage(message)) {\n // Check if the message contains schema information\n const schema = message.headers.schema\n if (schema && typeof schema === `string`) {\n // Store the schema for future use if it's a valid string\n relationSchema.setState(() => schema)\n }\n\n if (!transactionStarted) {\n begin()\n transactionStarted = true\n }\n\n write({\n type: message.headers.operation,\n value: message.value,\n // Include the primary key and relation info in the metadata\n metadata: {\n ...message.headers,\n },\n })\n } else if (isUpToDateMessage(message)) {\n hasUpToDate = true\n }\n }\n\n if (hasUpToDate) {\n // Commit transaction if one was started\n if (transactionStarted) {\n commit()\n transactionStarted = false\n }\n\n // Mark the collection as ready now that sync is up to date\n markReady()\n\n // Always commit txids when we receive up-to-date, regardless of transaction state\n seenTxids.setState((currentTxids) => {\n const clonedSeen = new Set<Txid>(currentTxids)\n if (newTxids.size > 0) {\n debug(`new txids synced from pg %O`, Array.from(newTxids))\n }\n newTxids.forEach((txid) => clonedSeen.add(txid))\n newTxids.clear()\n return clonedSeen\n })\n }\n })\n\n // Return the unsubscribe function\n return () => {\n // Unsubscribe from the stream\n unsubscribeStream()\n // Abort the abort controller to stop the stream\n abortController.abort()\n }\n },\n // Expose the getSyncMetadata function\n getSyncMetadata,\n }\n}\n"],"names":["isControlMessage","Store","ExpectedNumberInAwaitTxIdError","TimeoutWaitingForTxIdError","ElectricInsertHandlerMustReturnTxIdError","ElectricUpdateHandlerMustReturnTxIdError","ElectricDeleteHandlerMustReturnTxIdError","ShapeStream","isChangeMessage"],"mappings":";;;;;;AA+BA,MAAM,QAAQ,YAAY,MAAM,gBAAgB;AA2NhD,SAAS,kBACP,SACkD;AAClD,SAAOA,OAAAA,iBAAiB,OAAO,KAAK,QAAQ,QAAQ,YAAY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AQ;AAAA,cAAA;AAAA,YACb,CACD;AAAA,UACH,WAAW,kBAAkB,OAAO,GAAG;AACrC,0BAAc;AAAA,UAChB;AAAA,QACF;AAEA,YAAI,aAAa;AAEf,cAAI,oBAAoB;AACtB,mBAAA;AACA,iCAAqB;AAAA,UACvB;AAGA,oBAAA;AAGA,oBAAU,SAAS,CAAC,iBAAiB;AACnC,kBAAM,aAAa,IAAI,IAAU,YAAY;AAC7C,gBAAI,SAAS,OAAO,GAAG;AACrB,oBAAM,+BAA+B,MAAM,KAAK,QAAQ,CAAC;AAAA,YAC3D;AACA,qBAAS,QAAQ,CAAC,SAAS,WAAW,IAAI,IAAI,CAAC;AAC/C,qBAAS,MAAA;AACT,mBAAO;AAAA,UACT,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAGD,aAAO,MAAM;AAEX,0BAAA;AAEA,wBAAgB,MAAA;AAAA,MAClB;AAAA,IACF;AAAA;AAAA,IAEA;AAAA,EAAA;AAEJ;;"}
1
+ {"version":3,"file":"electric.cjs","sources":["../../src/electric.ts"],"sourcesContent":["import {\n ShapeStream,\n isChangeMessage,\n isControlMessage,\n} from \"@electric-sql/client\"\nimport { Store } from \"@tanstack/store\"\nimport DebugModule from \"debug\"\nimport {\n ElectricDeleteHandlerMustReturnTxIdError,\n ElectricInsertHandlerMustReturnTxIdError,\n ElectricUpdateHandlerMustReturnTxIdError,\n ExpectedNumberInAwaitTxIdError,\n TimeoutWaitingForTxIdError,\n} from \"./errors\"\nimport type {\n CollectionConfig,\n DeleteMutationFnParams,\n InsertMutationFnParams,\n SyncConfig,\n UpdateMutationFnParams,\n UtilsRecord,\n} from \"@tanstack/db\"\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\"\nimport type {\n ControlMessage,\n GetExtensions,\n Message,\n Row,\n ShapeStreamOptions,\n} from \"@electric-sql/client\"\n\nconst debug = DebugModule.debug(`ts/db:electric`)\n\n/**\n * Type representing a transaction ID in ElectricSQL\n */\nexport type Txid = number\n\n// The `InferSchemaOutput` and `ResolveType` are copied from the `@tanstack/db` package\n// but we modified `InferSchemaOutput` slightly to restrict the schema output to `Row<unknown>`\n// This is needed in order for `GetExtensions` to be able to infer the parser extensions type from the schema\ntype InferSchemaOutput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferOutput<T> extends Row<unknown>\n ? StandardSchemaV1.InferOutput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\ntype ResolveType<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends object = Record<string, unknown>,\n> =\n unknown extends GetExtensions<TExplicit>\n ? [TSchema] extends [never]\n ? TFallback\n : InferSchemaOutput<TSchema>\n : TExplicit\n\n/**\n * Configuration interface for Electric collection options\n * @template TExplicit - The explicit type of items in the collection (highest priority)\n * @template TSchema - The schema type for validation and type inference (second priority)\n * @template TFallback - The fallback type if no explicit or schema type is provided\n *\n * @remarks\n * Type resolution follows a priority order:\n * 1. If you provide an explicit type via generic parameter, it will be used\n * 2. If no explicit type is provided but a schema is, the schema's output type will be inferred\n * 3. If neither explicit type nor schema is provided, the fallback type will be used\n *\n * You should provide EITHER an explicit type OR a schema, but not both, as they would conflict.\n */\nexport interface ElectricCollectionConfig<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends Row<unknown> = Row<unknown>,\n> {\n /**\n * Configuration options for the ElectricSQL ShapeStream\n */\n shapeOptions: ShapeStreamOptions<\n GetExtensions<ResolveType<TExplicit, TSchema, TFallback>>\n >\n\n /**\n * All standard Collection configuration properties\n */\n id?: string\n schema?: TSchema\n getKey: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`getKey`]\n sync?: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`sync`]\n\n /**\n * Optional asynchronous handler function called before an insert operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric insert handler - MUST return { txid: number }\n * onInsert: async ({ transaction }) => {\n * const newItem = transaction.mutations[0].modified\n * const result = await api.todos.create({\n * data: newItem\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Insert handler with multiple items - return array of txids\n * onInsert: async ({ transaction }) => {\n * const items = transaction.mutations.map(m => m.modified)\n * const results = await Promise.all(\n * items.map(item => api.todos.create({ data: item }))\n * )\n * return { txid: results.map(r => r.txid) } // Array of txids\n * }\n *\n * @example\n * // Insert handler with error handling\n * onInsert: async ({ transaction }) => {\n * try {\n * const newItem = transaction.mutations[0].modified\n * const result = await api.createTodo(newItem)\n * return { txid: result.txid }\n * } catch (error) {\n * console.error('Insert failed:', error)\n * throw error // This will cause the transaction to fail\n * }\n * }\n *\n * @example\n * // Insert handler with batch operation - single txid\n * onInsert: async ({ transaction }) => {\n * const items = transaction.mutations.map(m => m.modified)\n * const result = await api.todos.createMany({\n * data: items\n * })\n * return { txid: result.txid } // Single txid for batch operation\n * }\n */\n onInsert?: (\n params: InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n\n /**\n * Optional asynchronous handler function called before an update operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric update handler - MUST return { txid: number }\n * onUpdate: async ({ transaction }) => {\n * const { original, changes } = transaction.mutations[0]\n * const result = await api.todos.update({\n * where: { id: original.id },\n * data: changes // Only the changed fields\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Update handler with multiple items - return array of txids\n * onUpdate: async ({ transaction }) => {\n * const updates = await Promise.all(\n * transaction.mutations.map(m =>\n * api.todos.update({\n * where: { id: m.original.id },\n * data: m.changes\n * })\n * )\n * )\n * return { txid: updates.map(u => u.txid) } // Array of txids\n * }\n *\n * @example\n * // Update handler with optimistic rollback\n * onUpdate: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * try {\n * const result = await api.updateTodo(mutation.original.id, mutation.changes)\n * return { txid: result.txid }\n * } catch (error) {\n * // Transaction will automatically rollback optimistic changes\n * console.error('Update failed, rolling back:', error)\n * throw error\n * }\n * }\n */\n onUpdate?: (\n params: UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n\n /**\n * Optional asynchronous handler function called before a delete operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric delete handler - MUST return { txid: number }\n * onDelete: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * const result = await api.todos.delete({\n * id: mutation.original.id\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Delete handler with multiple items - return array of txids\n * onDelete: async ({ transaction }) => {\n * const deletes = await Promise.all(\n * transaction.mutations.map(m =>\n * api.todos.delete({\n * where: { id: m.key }\n * })\n * )\n * )\n * return { txid: deletes.map(d => d.txid) } // Array of txids\n * }\n *\n * @example\n * // Delete handler with batch operation - single txid\n * onDelete: async ({ transaction }) => {\n * const idsToDelete = transaction.mutations.map(m => m.original.id)\n * const result = await api.todos.deleteMany({\n * ids: idsToDelete\n * })\n * return { txid: result.txid } // Single txid for batch operation\n * }\n *\n * @example\n * // Delete handler with optimistic rollback\n * onDelete: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * try {\n * const result = await api.deleteTodo(mutation.original.id)\n * return { txid: result.txid }\n * } catch (error) {\n * // Transaction will automatically rollback optimistic changes\n * console.error('Delete failed, rolling back:', error)\n * throw error\n * }\n * }\n *\n */\n onDelete?: (\n params: DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n}\n\nfunction isUpToDateMessage<T extends Row<unknown>>(\n message: Message<T>\n): message is ControlMessage & { up_to_date: true } {\n return isControlMessage(message) && message.headers.control === `up-to-date`\n}\n\nfunction isMustRefetchMessage<T extends Row<unknown>>(\n message: Message<T>\n): message is ControlMessage & { headers: { control: `must-refetch` } } {\n return isControlMessage(message) && message.headers.control === `must-refetch`\n}\n\n// Check if a message contains txids in its headers\nfunction hasTxids<T extends Row<unknown>>(\n message: Message<T>\n): message is Message<T> & { headers: { txids?: Array<Txid> } } {\n return `txids` in message.headers && Array.isArray(message.headers.txids)\n}\n\n/**\n * Type for the awaitTxId utility function\n */\nexport type AwaitTxIdFn = (txId: Txid, timeout?: number) => Promise<boolean>\n\n/**\n * Electric collection utilities type\n */\nexport interface ElectricCollectionUtils extends UtilsRecord {\n awaitTxId: AwaitTxIdFn\n}\n\n/**\n * Creates Electric collection options for use with a standard Collection\n *\n * @template TExplicit - The explicit type of items in the collection (highest priority)\n * @template TSchema - The schema type for validation and type inference (second priority)\n * @template TFallback - The fallback type if no explicit or schema type is provided\n * @param config - Configuration options for the Electric collection\n * @returns Collection options with utilities\n */\nexport function electricCollectionOptions<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends Row<unknown> = Row<unknown>,\n>(config: ElectricCollectionConfig<TExplicit, TSchema, TFallback>) {\n const seenTxids = new Store<Set<Txid>>(new Set([]))\n const sync = createElectricSync<ResolveType<TExplicit, TSchema, TFallback>>(\n config.shapeOptions,\n {\n seenTxids,\n }\n )\n\n /**\n * Wait for a specific transaction ID to be synced\n * @param txId The transaction ID to wait for as a number\n * @param timeout Optional timeout in milliseconds (defaults to 30000ms)\n * @returns Promise that resolves when the txId is synced\n */\n const awaitTxId: AwaitTxIdFn = async (\n txId: Txid,\n timeout: number = 30000\n ): Promise<boolean> => {\n debug(`awaitTxId called with txid %d`, txId)\n if (typeof txId !== `number`) {\n throw new ExpectedNumberInAwaitTxIdError(typeof txId)\n }\n\n const hasTxid = seenTxids.state.has(txId)\n if (hasTxid) return true\n\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n unsubscribe()\n reject(new TimeoutWaitingForTxIdError(txId))\n }, timeout)\n\n const unsubscribe = seenTxids.subscribe(() => {\n if (seenTxids.state.has(txId)) {\n debug(`awaitTxId found match for txid %o`, txId)\n clearTimeout(timeoutId)\n unsubscribe()\n resolve(true)\n }\n })\n })\n }\n\n // Create wrapper handlers for direct persistence operations that handle txid awaiting\n const wrappedOnInsert = config.onInsert\n ? async (\n params: InsertMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n // Runtime check (that doesn't follow type)\n // eslint-disable-next-line\n const handlerResult = (await config.onInsert!(params)) ?? {}\n const txid = (handlerResult as { txid?: Txid | Array<Txid> }).txid\n\n if (!txid) {\n throw new ElectricInsertHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(txid)) {\n await Promise.all(txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(txid)\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnUpdate = config.onUpdate\n ? async (\n params: UpdateMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n // Runtime check (that doesn't follow type)\n // eslint-disable-next-line\n const handlerResult = (await config.onUpdate!(params)) ?? {}\n const txid = (handlerResult as { txid?: Txid | Array<Txid> }).txid\n\n if (!txid) {\n throw new ElectricUpdateHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(txid)) {\n await Promise.all(txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(txid)\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnDelete = config.onDelete\n ? async (\n params: DeleteMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n const handlerResult = await config.onDelete!(params)\n if (!handlerResult.txid) {\n throw new ElectricDeleteHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(handlerResult.txid)) {\n await Promise.all(handlerResult.txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(handlerResult.txid)\n }\n\n return handlerResult\n }\n : undefined\n\n // Extract standard Collection config properties\n const {\n shapeOptions: _shapeOptions,\n onInsert: _onInsert,\n onUpdate: _onUpdate,\n onDelete: _onDelete,\n ...restConfig\n } = config\n\n return {\n ...restConfig,\n sync,\n onInsert: wrappedOnInsert,\n onUpdate: wrappedOnUpdate,\n onDelete: wrappedOnDelete,\n utils: {\n awaitTxId,\n },\n }\n}\n\n/**\n * Internal function to create ElectricSQL sync configuration\n */\nfunction createElectricSync<T extends Row<unknown>>(\n shapeOptions: ShapeStreamOptions<GetExtensions<T>>,\n options: {\n seenTxids: Store<Set<Txid>>\n }\n): SyncConfig<T> {\n const { seenTxids } = options\n\n // Store for the relation schema information\n const relationSchema = new Store<string | undefined>(undefined)\n\n /**\n * Get the sync metadata for insert operations\n * @returns Record containing relation information\n */\n const getSyncMetadata = (): Record<string, unknown> => {\n // Use the stored schema if available, otherwise default to 'public'\n const schema = relationSchema.state || `public`\n\n return {\n relation: shapeOptions.params?.table\n ? [schema, shapeOptions.params.table]\n : undefined,\n }\n }\n\n // Abort controller for the stream - wraps the signal if provided\n const abortController = new AbortController()\n if (shapeOptions.signal) {\n shapeOptions.signal.addEventListener(`abort`, () => {\n abortController.abort()\n })\n if (shapeOptions.signal.aborted) {\n abortController.abort()\n }\n }\n\n let unsubscribeStream: () => void\n\n return {\n sync: (params: Parameters<SyncConfig<T>[`sync`]>[0]) => {\n const { begin, write, commit, markReady, truncate } = params\n const stream = new ShapeStream({\n ...shapeOptions,\n signal: abortController.signal,\n onError: (errorParams) => {\n // Just immediately mark ready if there's an error to avoid blocking\n // apps waiting for `.preload()` to finish.\n markReady()\n\n if (shapeOptions.onError) {\n return shapeOptions.onError(errorParams)\n }\n\n return\n },\n })\n let transactionStarted = false\n const newTxids = new Set<Txid>()\n\n unsubscribeStream = stream.subscribe((messages: Array<Message<T>>) => {\n let hasUpToDate = false\n\n for (const message of messages) {\n // Check for txids in the message and add them to our store\n if (hasTxids(message)) {\n message.headers.txids?.forEach((txid) => newTxids.add(txid))\n }\n\n if (isChangeMessage(message)) {\n // Check if the message contains schema information\n const schema = message.headers.schema\n if (schema && typeof schema === `string`) {\n // Store the schema for future use if it's a valid string\n relationSchema.setState(() => schema)\n }\n\n if (!transactionStarted) {\n begin()\n transactionStarted = true\n }\n\n write({\n type: message.headers.operation,\n value: message.value,\n // Include the primary key and relation info in the metadata\n metadata: {\n ...message.headers,\n },\n })\n } else if (isUpToDateMessage(message)) {\n hasUpToDate = true\n } else if (isMustRefetchMessage(message)) {\n debug(\n `Received must-refetch message, starting transaction with truncate`\n )\n\n // Start a transaction and truncate the collection\n if (!transactionStarted) {\n begin()\n transactionStarted = true\n }\n\n truncate()\n\n // Commit the truncate transaction immediately\n commit()\n transactionStarted = false\n }\n }\n\n if (hasUpToDate) {\n // Commit transaction if one was started\n if (transactionStarted) {\n commit()\n transactionStarted = false\n }\n\n // Mark the collection as ready now that sync is up to date\n markReady()\n\n // Always commit txids when we receive up-to-date, regardless of transaction state\n seenTxids.setState((currentTxids) => {\n const clonedSeen = new Set<Txid>(currentTxids)\n if (newTxids.size > 0) {\n debug(`new txids synced from pg %O`, Array.from(newTxids))\n }\n newTxids.forEach((txid) => clonedSeen.add(txid))\n newTxids.clear()\n return clonedSeen\n })\n }\n })\n\n // Return the unsubscribe function\n return () => {\n // Unsubscribe from the stream\n unsubscribeStream()\n // Abort the abort controller to stop the stream\n abortController.abort()\n }\n },\n // Expose the getSyncMetadata function\n getSyncMetadata,\n }\n}\n"],"names":["isControlMessage","Store","ExpectedNumberInAwaitTxIdError","TimeoutWaitingForTxIdError","ElectricInsertHandlerMustReturnTxIdError","ElectricUpdateHandlerMustReturnTxIdError","ElectricDeleteHandlerMustReturnTxIdError","ShapeStream","isChangeMessage"],"mappings":";;;;;;AA+BA,MAAM,QAAQ,YAAY,MAAM,gBAAgB;AA2NhD,SAAS,kBACP,SACkD;AAClD,SAAOA,OAAAA,iBAAiB,OAAO,KAAK,QAAQ,QAAQ,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AAA,cACJ,MAAM,QAAQ,QAAQ;AAAA,cACtB,OAAO,QAAQ;AAAA;AAAA,cAEf,UAAU;AAAA,gBACR,GAAG,QAAQ;AAAA,cAAA;AAAA,YACb,CACD;AAAA,UACH,WAAW,kBAAkB,OAAO,GAAG;AACrC,0BAAc;AAAA,UAChB,WAAW,qBAAqB,OAAO,GAAG;AACxC;AAAA,cACE;AAAA,YAAA;AAIF,gBAAI,CAAC,oBAAoB;AACvB,oBAAA;AACA,mCAAqB;AAAA,YACvB;AAEA,qBAAA;AAGA,mBAAA;AACA,iCAAqB;AAAA,UACvB;AAAA,QACF;AAEA,YAAI,aAAa;AAEf,cAAI,oBAAoB;AACtB,mBAAA;AACA,iCAAqB;AAAA,UACvB;AAGA,oBAAA;AAGA,oBAAU,SAAS,CAAC,iBAAiB;AACnC,kBAAM,aAAa,IAAI,IAAU,YAAY;AAC7C,gBAAI,SAAS,OAAO,GAAG;AACrB,oBAAM,+BAA+B,MAAM,KAAK,QAAQ,CAAC;AAAA,YAC3D;AACA,qBAAS,QAAQ,CAAC,SAAS,WAAW,IAAI,IAAI,CAAC;AAC/C,qBAAS,MAAA;AACT,mBAAO;AAAA,UACT,CAAC;AAAA,QACH;AAAA,MACF,CAAC;AAGD,aAAO,MAAM;AAEX,0BAAA;AAEA,wBAAgB,MAAA;AAAA,MAClB;AAAA,IACF;AAAA;AAAA,IAEA;AAAA,EAAA;AAEJ;;"}
@@ -1,2 +1,2 @@
1
- export { electricCollectionOptions, type ElectricCollectionConfig, type ElectricCollectionUtils, type Txid, } from './electric.cjs';
1
+ export { electricCollectionOptions, type ElectricCollectionConfig, type ElectricCollectionUtils, type Txid, type AwaitTxIdFn, } from './electric.cjs';
2
2
  export * from './errors.cjs';
@@ -6,6 +6,9 @@ const debug = DebugModule.debug(`ts/db:electric`);
6
6
  function isUpToDateMessage(message) {
7
7
  return isControlMessage(message) && message.headers.control === `up-to-date`;
8
8
  }
9
+ function isMustRefetchMessage(message) {
10
+ return isControlMessage(message) && message.headers.control === `must-refetch`;
11
+ }
9
12
  function hasTxids(message) {
10
13
  return `txids` in message.headers && Array.isArray(message.headers.txids);
11
14
  }
@@ -117,7 +120,7 @@ function createElectricSync(shapeOptions, options) {
117
120
  let unsubscribeStream;
118
121
  return {
119
122
  sync: (params) => {
120
- const { begin, write, commit, markReady } = params;
123
+ const { begin, write, commit, markReady, truncate } = params;
121
124
  const stream = new ShapeStream({
122
125
  ...shapeOptions,
123
126
  signal: abortController.signal,
@@ -157,6 +160,17 @@ function createElectricSync(shapeOptions, options) {
157
160
  });
158
161
  } else if (isUpToDateMessage(message)) {
159
162
  hasUpToDate = true;
163
+ } else if (isMustRefetchMessage(message)) {
164
+ debug(
165
+ `Received must-refetch message, starting transaction with truncate`
166
+ );
167
+ if (!transactionStarted) {
168
+ begin();
169
+ transactionStarted = true;
170
+ }
171
+ truncate();
172
+ commit();
173
+ transactionStarted = false;
160
174
  }
161
175
  }
162
176
  if (hasUpToDate) {
@@ -1 +1 @@
1
- {"version":3,"file":"electric.js","sources":["../../src/electric.ts"],"sourcesContent":["import {\n ShapeStream,\n isChangeMessage,\n isControlMessage,\n} from \"@electric-sql/client\"\nimport { Store } from \"@tanstack/store\"\nimport DebugModule from \"debug\"\nimport {\n ElectricDeleteHandlerMustReturnTxIdError,\n ElectricInsertHandlerMustReturnTxIdError,\n ElectricUpdateHandlerMustReturnTxIdError,\n ExpectedNumberInAwaitTxIdError,\n TimeoutWaitingForTxIdError,\n} from \"./errors\"\nimport type {\n CollectionConfig,\n DeleteMutationFnParams,\n InsertMutationFnParams,\n SyncConfig,\n UpdateMutationFnParams,\n UtilsRecord,\n} from \"@tanstack/db\"\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\"\nimport type {\n ControlMessage,\n GetExtensions,\n Message,\n Row,\n ShapeStreamOptions,\n} from \"@electric-sql/client\"\n\nconst debug = DebugModule.debug(`ts/db:electric`)\n\n/**\n * Type representing a transaction ID in ElectricSQL\n */\nexport type Txid = number\n\n// The `InferSchemaOutput` and `ResolveType` are copied from the `@tanstack/db` package\n// but we modified `InferSchemaOutput` slightly to restrict the schema output to `Row<unknown>`\n// This is needed in order for `GetExtensions` to be able to infer the parser extensions type from the schema\ntype InferSchemaOutput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferOutput<T> extends Row<unknown>\n ? StandardSchemaV1.InferOutput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\ntype ResolveType<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends object = Record<string, unknown>,\n> =\n unknown extends GetExtensions<TExplicit>\n ? [TSchema] extends [never]\n ? TFallback\n : InferSchemaOutput<TSchema>\n : TExplicit\n\n/**\n * Configuration interface for Electric collection options\n * @template TExplicit - The explicit type of items in the collection (highest priority)\n * @template TSchema - The schema type for validation and type inference (second priority)\n * @template TFallback - The fallback type if no explicit or schema type is provided\n *\n * @remarks\n * Type resolution follows a priority order:\n * 1. If you provide an explicit type via generic parameter, it will be used\n * 2. If no explicit type is provided but a schema is, the schema's output type will be inferred\n * 3. If neither explicit type nor schema is provided, the fallback type will be used\n *\n * You should provide EITHER an explicit type OR a schema, but not both, as they would conflict.\n */\nexport interface ElectricCollectionConfig<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends Row<unknown> = Row<unknown>,\n> {\n /**\n * Configuration options for the ElectricSQL ShapeStream\n */\n shapeOptions: ShapeStreamOptions<\n GetExtensions<ResolveType<TExplicit, TSchema, TFallback>>\n >\n\n /**\n * All standard Collection configuration properties\n */\n id?: string\n schema?: TSchema\n getKey: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`getKey`]\n sync?: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`sync`]\n\n /**\n * Optional asynchronous handler function called before an insert operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric insert handler - MUST return { txid: number }\n * onInsert: async ({ transaction }) => {\n * const newItem = transaction.mutations[0].modified\n * const result = await api.todos.create({\n * data: newItem\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Insert handler with multiple items - return array of txids\n * onInsert: async ({ transaction }) => {\n * const items = transaction.mutations.map(m => m.modified)\n * const results = await Promise.all(\n * items.map(item => api.todos.create({ data: item }))\n * )\n * return { txid: results.map(r => r.txid) } // Array of txids\n * }\n *\n * @example\n * // Insert handler with error handling\n * onInsert: async ({ transaction }) => {\n * try {\n * const newItem = transaction.mutations[0].modified\n * const result = await api.createTodo(newItem)\n * return { txid: result.txid }\n * } catch (error) {\n * console.error('Insert failed:', error)\n * throw error // This will cause the transaction to fail\n * }\n * }\n *\n * @example\n * // Insert handler with batch operation - single txid\n * onInsert: async ({ transaction }) => {\n * const items = transaction.mutations.map(m => m.modified)\n * const result = await api.todos.createMany({\n * data: items\n * })\n * return { txid: result.txid } // Single txid for batch operation\n * }\n */\n onInsert?: (\n params: InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n\n /**\n * Optional asynchronous handler function called before an update operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric update handler - MUST return { txid: number }\n * onUpdate: async ({ transaction }) => {\n * const { original, changes } = transaction.mutations[0]\n * const result = await api.todos.update({\n * where: { id: original.id },\n * data: changes // Only the changed fields\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Update handler with multiple items - return array of txids\n * onUpdate: async ({ transaction }) => {\n * const updates = await Promise.all(\n * transaction.mutations.map(m =>\n * api.todos.update({\n * where: { id: m.original.id },\n * data: m.changes\n * })\n * )\n * )\n * return { txid: updates.map(u => u.txid) } // Array of txids\n * }\n *\n * @example\n * // Update handler with optimistic rollback\n * onUpdate: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * try {\n * const result = await api.updateTodo(mutation.original.id, mutation.changes)\n * return { txid: result.txid }\n * } catch (error) {\n * // Transaction will automatically rollback optimistic changes\n * console.error('Update failed, rolling back:', error)\n * throw error\n * }\n * }\n */\n onUpdate?: (\n params: UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n\n /**\n * Optional asynchronous handler function called before a delete operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric delete handler - MUST return { txid: number }\n * onDelete: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * const result = await api.todos.delete({\n * id: mutation.original.id\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Delete handler with multiple items - return array of txids\n * onDelete: async ({ transaction }) => {\n * const deletes = await Promise.all(\n * transaction.mutations.map(m =>\n * api.todos.delete({\n * where: { id: m.key }\n * })\n * )\n * )\n * return { txid: deletes.map(d => d.txid) } // Array of txids\n * }\n *\n * @example\n * // Delete handler with batch operation - single txid\n * onDelete: async ({ transaction }) => {\n * const idsToDelete = transaction.mutations.map(m => m.original.id)\n * const result = await api.todos.deleteMany({\n * ids: idsToDelete\n * })\n * return { txid: result.txid } // Single txid for batch operation\n * }\n *\n * @example\n * // Delete handler with optimistic rollback\n * onDelete: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * try {\n * const result = await api.deleteTodo(mutation.original.id)\n * return { txid: result.txid }\n * } catch (error) {\n * // Transaction will automatically rollback optimistic changes\n * console.error('Delete failed, rolling back:', error)\n * throw error\n * }\n * }\n *\n */\n onDelete?: (\n params: DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n}\n\nfunction isUpToDateMessage<T extends Row<unknown>>(\n message: Message<T>\n): message is ControlMessage & { up_to_date: true } {\n return isControlMessage(message) && message.headers.control === `up-to-date`\n}\n\n// Check if a message contains txids in its headers\nfunction hasTxids<T extends Row<unknown>>(\n message: Message<T>\n): message is Message<T> & { headers: { txids?: Array<Txid> } } {\n return `txids` in message.headers && Array.isArray(message.headers.txids)\n}\n\n/**\n * Type for the awaitTxId utility function\n */\nexport type AwaitTxIdFn = (txId: Txid, timeout?: number) => Promise<boolean>\n\n/**\n * Electric collection utilities type\n */\nexport interface ElectricCollectionUtils extends UtilsRecord {\n awaitTxId: AwaitTxIdFn\n}\n\n/**\n * Creates Electric collection options for use with a standard Collection\n *\n * @template TExplicit - The explicit type of items in the collection (highest priority)\n * @template TSchema - The schema type for validation and type inference (second priority)\n * @template TFallback - The fallback type if no explicit or schema type is provided\n * @param config - Configuration options for the Electric collection\n * @returns Collection options with utilities\n */\nexport function electricCollectionOptions<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends Row<unknown> = Row<unknown>,\n>(config: ElectricCollectionConfig<TExplicit, TSchema, TFallback>) {\n const seenTxids = new Store<Set<Txid>>(new Set([]))\n const sync = createElectricSync<ResolveType<TExplicit, TSchema, TFallback>>(\n config.shapeOptions,\n {\n seenTxids,\n }\n )\n\n /**\n * Wait for a specific transaction ID to be synced\n * @param txId The transaction ID to wait for as a number\n * @param timeout Optional timeout in milliseconds (defaults to 30000ms)\n * @returns Promise that resolves when the txId is synced\n */\n const awaitTxId: AwaitTxIdFn = async (\n txId: Txid,\n timeout: number = 30000\n ): Promise<boolean> => {\n debug(`awaitTxId called with txid %d`, txId)\n if (typeof txId !== `number`) {\n throw new ExpectedNumberInAwaitTxIdError(typeof txId)\n }\n\n const hasTxid = seenTxids.state.has(txId)\n if (hasTxid) return true\n\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n unsubscribe()\n reject(new TimeoutWaitingForTxIdError(txId))\n }, timeout)\n\n const unsubscribe = seenTxids.subscribe(() => {\n if (seenTxids.state.has(txId)) {\n debug(`awaitTxId found match for txid %o`, txId)\n clearTimeout(timeoutId)\n unsubscribe()\n resolve(true)\n }\n })\n })\n }\n\n // Create wrapper handlers for direct persistence operations that handle txid awaiting\n const wrappedOnInsert = config.onInsert\n ? async (\n params: InsertMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n // Runtime check (that doesn't follow type)\n // eslint-disable-next-line\n const handlerResult = (await config.onInsert!(params)) ?? {}\n const txid = (handlerResult as { txid?: Txid | Array<Txid> }).txid\n\n if (!txid) {\n throw new ElectricInsertHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(txid)) {\n await Promise.all(txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(txid)\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnUpdate = config.onUpdate\n ? async (\n params: UpdateMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n // Runtime check (that doesn't follow type)\n // eslint-disable-next-line\n const handlerResult = (await config.onUpdate!(params)) ?? {}\n const txid = (handlerResult as { txid?: Txid | Array<Txid> }).txid\n\n if (!txid) {\n throw new ElectricUpdateHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(txid)) {\n await Promise.all(txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(txid)\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnDelete = config.onDelete\n ? async (\n params: DeleteMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n const handlerResult = await config.onDelete!(params)\n if (!handlerResult.txid) {\n throw new ElectricDeleteHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(handlerResult.txid)) {\n await Promise.all(handlerResult.txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(handlerResult.txid)\n }\n\n return handlerResult\n }\n : undefined\n\n // Extract standard Collection config properties\n const {\n shapeOptions: _shapeOptions,\n onInsert: _onInsert,\n onUpdate: _onUpdate,\n onDelete: _onDelete,\n ...restConfig\n } = config\n\n return {\n ...restConfig,\n sync,\n onInsert: wrappedOnInsert,\n onUpdate: wrappedOnUpdate,\n onDelete: wrappedOnDelete,\n utils: {\n awaitTxId,\n },\n }\n}\n\n/**\n * Internal function to create ElectricSQL sync configuration\n */\nfunction createElectricSync<T extends Row<unknown>>(\n shapeOptions: ShapeStreamOptions<GetExtensions<T>>,\n options: {\n seenTxids: Store<Set<Txid>>\n }\n): SyncConfig<T> {\n const { seenTxids } = options\n\n // Store for the relation schema information\n const relationSchema = new Store<string | undefined>(undefined)\n\n /**\n * Get the sync metadata for insert operations\n * @returns Record containing relation information\n */\n const getSyncMetadata = (): Record<string, unknown> => {\n // Use the stored schema if available, otherwise default to 'public'\n const schema = relationSchema.state || `public`\n\n return {\n relation: shapeOptions.params?.table\n ? [schema, shapeOptions.params.table]\n : undefined,\n }\n }\n\n // Abort controller for the stream - wraps the signal if provided\n const abortController = new AbortController()\n if (shapeOptions.signal) {\n shapeOptions.signal.addEventListener(`abort`, () => {\n abortController.abort()\n })\n if (shapeOptions.signal.aborted) {\n abortController.abort()\n }\n }\n\n let unsubscribeStream: () => void\n\n return {\n sync: (params: Parameters<SyncConfig<T>[`sync`]>[0]) => {\n const { begin, write, commit, markReady } = params\n const stream = new ShapeStream({\n ...shapeOptions,\n signal: abortController.signal,\n onError: (errorParams) => {\n // Just immediately mark ready if there's an error to avoid blocking\n // apps waiting for `.preload()` to finish.\n markReady()\n\n if (shapeOptions.onError) {\n return shapeOptions.onError(errorParams)\n }\n\n return\n },\n })\n let transactionStarted = false\n const newTxids = new Set<Txid>()\n\n unsubscribeStream = stream.subscribe((messages: Array<Message<T>>) => {\n let hasUpToDate = false\n\n for (const message of messages) {\n // Check for txids in the message and add them to our store\n if (hasTxids(message)) {\n message.headers.txids?.forEach((txid) => newTxids.add(txid))\n }\n\n if (isChangeMessage(message)) {\n // Check if the message contains schema information\n const schema = message.headers.schema\n if (schema && typeof schema === `string`) {\n // Store the schema for future use if it's a valid string\n relationSchema.setState(() => schema)\n }\n\n if (!transactionStarted) {\n begin()\n transactionStarted = true\n }\n\n write({\n type: message.headers.operation,\n value: message.value,\n // Include the primary key and relation info in the metadata\n metadata: {\n ...message.headers,\n },\n })\n } else if (isUpToDateMessage(message)) {\n hasUpToDate = true\n }\n }\n\n if (hasUpToDate) {\n // Commit transaction if one was started\n if (transactionStarted) {\n commit()\n transactionStarted = false\n }\n\n // Mark the collection as ready now that sync is up to date\n markReady()\n\n // Always commit txids when we receive up-to-date, regardless of transaction state\n seenTxids.setState((currentTxids) => {\n const clonedSeen = new Set<Txid>(currentTxids)\n if (newTxids.size > 0) {\n debug(`new txids synced from pg %O`, Array.from(newTxids))\n }\n newTxids.forEach((txid) => clonedSeen.add(txid))\n newTxids.clear()\n return clonedSeen\n })\n }\n })\n\n // Return the unsubscribe function\n return () => {\n // Unsubscribe from the stream\n unsubscribeStream()\n // Abort the abort controller to stop the stream\n abortController.abort()\n }\n },\n // Expose the getSyncMetadata function\n getSyncMetadata,\n }\n}\n"],"names":[],"mappings":";;;;AA+BA,MAAM,QAAQ,YAAY,MAAM,gBAAgB;AA2NhD,SAAS,kBACP,SACkD;AAClD,SAAO,iBAAiB,OAAO,KAAK,QAAQ,QAAQ,YAAY;AAClE;AAGA,SAAS,SACP,SAC8D;AAC9D,SAAO,WAAW,QAAQ,WAAW,MAAM,QAAQ,QAAQ,QAAQ,KAAK;AAC1E;AAuBO,SAAS,0BAId,QAAiE;AACjE,QAAM,YAAY,IAAI,0BAAqB,IAAI,CAAA,CAAE,CAAC;AAClD,QAAM,OAAO;AAAA,IACX,OAAO;AAAA,IACP;AAAA,MACE;AAAA,IAAA;AAAA,EACF;AASF,QAAM,YAAyB,OAC7B,MACA,UAAkB,QACG;AACrB,UAAM,iCAAiC,IAAI;AAC3C,QAAI,OAAO,SAAS,UAAU;AAC5B,YAAM,IAAI,+BAA+B,OAAO,IAAI;AAAA,IACtD;AAEA,UAAM,UAAU,UAAU,MAAM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AAEX,0BAAA;AAEA,wBAAgB,MAAA;AAAA,MAClB;AAAA,IACF;AAAA;AAAA,IAEA;AAAA,EAAA;AAEJ;"}
1
+ {"version":3,"file":"electric.js","sources":["../../src/electric.ts"],"sourcesContent":["import {\n ShapeStream,\n isChangeMessage,\n isControlMessage,\n} from \"@electric-sql/client\"\nimport { Store } from \"@tanstack/store\"\nimport DebugModule from \"debug\"\nimport {\n ElectricDeleteHandlerMustReturnTxIdError,\n ElectricInsertHandlerMustReturnTxIdError,\n ElectricUpdateHandlerMustReturnTxIdError,\n ExpectedNumberInAwaitTxIdError,\n TimeoutWaitingForTxIdError,\n} from \"./errors\"\nimport type {\n CollectionConfig,\n DeleteMutationFnParams,\n InsertMutationFnParams,\n SyncConfig,\n UpdateMutationFnParams,\n UtilsRecord,\n} from \"@tanstack/db\"\nimport type { StandardSchemaV1 } from \"@standard-schema/spec\"\nimport type {\n ControlMessage,\n GetExtensions,\n Message,\n Row,\n ShapeStreamOptions,\n} from \"@electric-sql/client\"\n\nconst debug = DebugModule.debug(`ts/db:electric`)\n\n/**\n * Type representing a transaction ID in ElectricSQL\n */\nexport type Txid = number\n\n// The `InferSchemaOutput` and `ResolveType` are copied from the `@tanstack/db` package\n// but we modified `InferSchemaOutput` slightly to restrict the schema output to `Row<unknown>`\n// This is needed in order for `GetExtensions` to be able to infer the parser extensions type from the schema\ntype InferSchemaOutput<T> = T extends StandardSchemaV1\n ? StandardSchemaV1.InferOutput<T> extends Row<unknown>\n ? StandardSchemaV1.InferOutput<T>\n : Record<string, unknown>\n : Record<string, unknown>\n\ntype ResolveType<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends object = Record<string, unknown>,\n> =\n unknown extends GetExtensions<TExplicit>\n ? [TSchema] extends [never]\n ? TFallback\n : InferSchemaOutput<TSchema>\n : TExplicit\n\n/**\n * Configuration interface for Electric collection options\n * @template TExplicit - The explicit type of items in the collection (highest priority)\n * @template TSchema - The schema type for validation and type inference (second priority)\n * @template TFallback - The fallback type if no explicit or schema type is provided\n *\n * @remarks\n * Type resolution follows a priority order:\n * 1. If you provide an explicit type via generic parameter, it will be used\n * 2. If no explicit type is provided but a schema is, the schema's output type will be inferred\n * 3. If neither explicit type nor schema is provided, the fallback type will be used\n *\n * You should provide EITHER an explicit type OR a schema, but not both, as they would conflict.\n */\nexport interface ElectricCollectionConfig<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends Row<unknown> = Row<unknown>,\n> {\n /**\n * Configuration options for the ElectricSQL ShapeStream\n */\n shapeOptions: ShapeStreamOptions<\n GetExtensions<ResolveType<TExplicit, TSchema, TFallback>>\n >\n\n /**\n * All standard Collection configuration properties\n */\n id?: string\n schema?: TSchema\n getKey: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`getKey`]\n sync?: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`sync`]\n\n /**\n * Optional asynchronous handler function called before an insert operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric insert handler - MUST return { txid: number }\n * onInsert: async ({ transaction }) => {\n * const newItem = transaction.mutations[0].modified\n * const result = await api.todos.create({\n * data: newItem\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Insert handler with multiple items - return array of txids\n * onInsert: async ({ transaction }) => {\n * const items = transaction.mutations.map(m => m.modified)\n * const results = await Promise.all(\n * items.map(item => api.todos.create({ data: item }))\n * )\n * return { txid: results.map(r => r.txid) } // Array of txids\n * }\n *\n * @example\n * // Insert handler with error handling\n * onInsert: async ({ transaction }) => {\n * try {\n * const newItem = transaction.mutations[0].modified\n * const result = await api.createTodo(newItem)\n * return { txid: result.txid }\n * } catch (error) {\n * console.error('Insert failed:', error)\n * throw error // This will cause the transaction to fail\n * }\n * }\n *\n * @example\n * // Insert handler with batch operation - single txid\n * onInsert: async ({ transaction }) => {\n * const items = transaction.mutations.map(m => m.modified)\n * const result = await api.todos.createMany({\n * data: items\n * })\n * return { txid: result.txid } // Single txid for batch operation\n * }\n */\n onInsert?: (\n params: InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n\n /**\n * Optional asynchronous handler function called before an update operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric update handler - MUST return { txid: number }\n * onUpdate: async ({ transaction }) => {\n * const { original, changes } = transaction.mutations[0]\n * const result = await api.todos.update({\n * where: { id: original.id },\n * data: changes // Only the changed fields\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Update handler with multiple items - return array of txids\n * onUpdate: async ({ transaction }) => {\n * const updates = await Promise.all(\n * transaction.mutations.map(m =>\n * api.todos.update({\n * where: { id: m.original.id },\n * data: m.changes\n * })\n * )\n * )\n * return { txid: updates.map(u => u.txid) } // Array of txids\n * }\n *\n * @example\n * // Update handler with optimistic rollback\n * onUpdate: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * try {\n * const result = await api.updateTodo(mutation.original.id, mutation.changes)\n * return { txid: result.txid }\n * } catch (error) {\n * // Transaction will automatically rollback optimistic changes\n * console.error('Update failed, rolling back:', error)\n * throw error\n * }\n * }\n */\n onUpdate?: (\n params: UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n\n /**\n * Optional asynchronous handler function called before a delete operation\n * Must return an object containing a txid number or array of txids\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to an object with txid or txids\n * @example\n * // Basic Electric delete handler - MUST return { txid: number }\n * onDelete: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * const result = await api.todos.delete({\n * id: mutation.original.id\n * })\n * return { txid: result.txid } // Required for Electric sync matching\n * }\n *\n * @example\n * // Delete handler with multiple items - return array of txids\n * onDelete: async ({ transaction }) => {\n * const deletes = await Promise.all(\n * transaction.mutations.map(m =>\n * api.todos.delete({\n * where: { id: m.key }\n * })\n * )\n * )\n * return { txid: deletes.map(d => d.txid) } // Array of txids\n * }\n *\n * @example\n * // Delete handler with batch operation - single txid\n * onDelete: async ({ transaction }) => {\n * const idsToDelete = transaction.mutations.map(m => m.original.id)\n * const result = await api.todos.deleteMany({\n * ids: idsToDelete\n * })\n * return { txid: result.txid } // Single txid for batch operation\n * }\n *\n * @example\n * // Delete handler with optimistic rollback\n * onDelete: async ({ transaction }) => {\n * const mutation = transaction.mutations[0]\n * try {\n * const result = await api.deleteTodo(mutation.original.id)\n * return { txid: result.txid }\n * } catch (error) {\n * // Transaction will automatically rollback optimistic changes\n * console.error('Delete failed, rolling back:', error)\n * throw error\n * }\n * }\n *\n */\n onDelete?: (\n params: DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>\n ) => Promise<{ txid: Txid | Array<Txid> }>\n}\n\nfunction isUpToDateMessage<T extends Row<unknown>>(\n message: Message<T>\n): message is ControlMessage & { up_to_date: true } {\n return isControlMessage(message) && message.headers.control === `up-to-date`\n}\n\nfunction isMustRefetchMessage<T extends Row<unknown>>(\n message: Message<T>\n): message is ControlMessage & { headers: { control: `must-refetch` } } {\n return isControlMessage(message) && message.headers.control === `must-refetch`\n}\n\n// Check if a message contains txids in its headers\nfunction hasTxids<T extends Row<unknown>>(\n message: Message<T>\n): message is Message<T> & { headers: { txids?: Array<Txid> } } {\n return `txids` in message.headers && Array.isArray(message.headers.txids)\n}\n\n/**\n * Type for the awaitTxId utility function\n */\nexport type AwaitTxIdFn = (txId: Txid, timeout?: number) => Promise<boolean>\n\n/**\n * Electric collection utilities type\n */\nexport interface ElectricCollectionUtils extends UtilsRecord {\n awaitTxId: AwaitTxIdFn\n}\n\n/**\n * Creates Electric collection options for use with a standard Collection\n *\n * @template TExplicit - The explicit type of items in the collection (highest priority)\n * @template TSchema - The schema type for validation and type inference (second priority)\n * @template TFallback - The fallback type if no explicit or schema type is provided\n * @param config - Configuration options for the Electric collection\n * @returns Collection options with utilities\n */\nexport function electricCollectionOptions<\n TExplicit extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n TFallback extends Row<unknown> = Row<unknown>,\n>(config: ElectricCollectionConfig<TExplicit, TSchema, TFallback>) {\n const seenTxids = new Store<Set<Txid>>(new Set([]))\n const sync = createElectricSync<ResolveType<TExplicit, TSchema, TFallback>>(\n config.shapeOptions,\n {\n seenTxids,\n }\n )\n\n /**\n * Wait for a specific transaction ID to be synced\n * @param txId The transaction ID to wait for as a number\n * @param timeout Optional timeout in milliseconds (defaults to 30000ms)\n * @returns Promise that resolves when the txId is synced\n */\n const awaitTxId: AwaitTxIdFn = async (\n txId: Txid,\n timeout: number = 30000\n ): Promise<boolean> => {\n debug(`awaitTxId called with txid %d`, txId)\n if (typeof txId !== `number`) {\n throw new ExpectedNumberInAwaitTxIdError(typeof txId)\n }\n\n const hasTxid = seenTxids.state.has(txId)\n if (hasTxid) return true\n\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n unsubscribe()\n reject(new TimeoutWaitingForTxIdError(txId))\n }, timeout)\n\n const unsubscribe = seenTxids.subscribe(() => {\n if (seenTxids.state.has(txId)) {\n debug(`awaitTxId found match for txid %o`, txId)\n clearTimeout(timeoutId)\n unsubscribe()\n resolve(true)\n }\n })\n })\n }\n\n // Create wrapper handlers for direct persistence operations that handle txid awaiting\n const wrappedOnInsert = config.onInsert\n ? async (\n params: InsertMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n // Runtime check (that doesn't follow type)\n // eslint-disable-next-line\n const handlerResult = (await config.onInsert!(params)) ?? {}\n const txid = (handlerResult as { txid?: Txid | Array<Txid> }).txid\n\n if (!txid) {\n throw new ElectricInsertHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(txid)) {\n await Promise.all(txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(txid)\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnUpdate = config.onUpdate\n ? async (\n params: UpdateMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n // Runtime check (that doesn't follow type)\n // eslint-disable-next-line\n const handlerResult = (await config.onUpdate!(params)) ?? {}\n const txid = (handlerResult as { txid?: Txid | Array<Txid> }).txid\n\n if (!txid) {\n throw new ElectricUpdateHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(txid)) {\n await Promise.all(txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(txid)\n }\n\n return handlerResult\n }\n : undefined\n\n const wrappedOnDelete = config.onDelete\n ? async (\n params: DeleteMutationFnParams<\n ResolveType<TExplicit, TSchema, TFallback>\n >\n ) => {\n const handlerResult = await config.onDelete!(params)\n if (!handlerResult.txid) {\n throw new ElectricDeleteHandlerMustReturnTxIdError()\n }\n\n // Handle both single txid and array of txids\n if (Array.isArray(handlerResult.txid)) {\n await Promise.all(handlerResult.txid.map((id) => awaitTxId(id)))\n } else {\n await awaitTxId(handlerResult.txid)\n }\n\n return handlerResult\n }\n : undefined\n\n // Extract standard Collection config properties\n const {\n shapeOptions: _shapeOptions,\n onInsert: _onInsert,\n onUpdate: _onUpdate,\n onDelete: _onDelete,\n ...restConfig\n } = config\n\n return {\n ...restConfig,\n sync,\n onInsert: wrappedOnInsert,\n onUpdate: wrappedOnUpdate,\n onDelete: wrappedOnDelete,\n utils: {\n awaitTxId,\n },\n }\n}\n\n/**\n * Internal function to create ElectricSQL sync configuration\n */\nfunction createElectricSync<T extends Row<unknown>>(\n shapeOptions: ShapeStreamOptions<GetExtensions<T>>,\n options: {\n seenTxids: Store<Set<Txid>>\n }\n): SyncConfig<T> {\n const { seenTxids } = options\n\n // Store for the relation schema information\n const relationSchema = new Store<string | undefined>(undefined)\n\n /**\n * Get the sync metadata for insert operations\n * @returns Record containing relation information\n */\n const getSyncMetadata = (): Record<string, unknown> => {\n // Use the stored schema if available, otherwise default to 'public'\n const schema = relationSchema.state || `public`\n\n return {\n relation: shapeOptions.params?.table\n ? [schema, shapeOptions.params.table]\n : undefined,\n }\n }\n\n // Abort controller for the stream - wraps the signal if provided\n const abortController = new AbortController()\n if (shapeOptions.signal) {\n shapeOptions.signal.addEventListener(`abort`, () => {\n abortController.abort()\n })\n if (shapeOptions.signal.aborted) {\n abortController.abort()\n }\n }\n\n let unsubscribeStream: () => void\n\n return {\n sync: (params: Parameters<SyncConfig<T>[`sync`]>[0]) => {\n const { begin, write, commit, markReady, truncate } = params\n const stream = new ShapeStream({\n ...shapeOptions,\n signal: abortController.signal,\n onError: (errorParams) => {\n // Just immediately mark ready if there's an error to avoid blocking\n // apps waiting for `.preload()` to finish.\n markReady()\n\n if (shapeOptions.onError) {\n return shapeOptions.onError(errorParams)\n }\n\n return\n },\n })\n let transactionStarted = false\n const newTxids = new Set<Txid>()\n\n unsubscribeStream = stream.subscribe((messages: Array<Message<T>>) => {\n let hasUpToDate = false\n\n for (const message of messages) {\n // Check for txids in the message and add them to our store\n if (hasTxids(message)) {\n message.headers.txids?.forEach((txid) => newTxids.add(txid))\n }\n\n if (isChangeMessage(message)) {\n // Check if the message contains schema information\n const schema = message.headers.schema\n if (schema && typeof schema === `string`) {\n // Store the schema for future use if it's a valid string\n relationSchema.setState(() => schema)\n }\n\n if (!transactionStarted) {\n begin()\n transactionStarted = true\n }\n\n write({\n type: message.headers.operation,\n value: message.value,\n // Include the primary key and relation info in the metadata\n metadata: {\n ...message.headers,\n },\n })\n } else if (isUpToDateMessage(message)) {\n hasUpToDate = true\n } else if (isMustRefetchMessage(message)) {\n debug(\n `Received must-refetch message, starting transaction with truncate`\n )\n\n // Start a transaction and truncate the collection\n if (!transactionStarted) {\n begin()\n transactionStarted = true\n }\n\n truncate()\n\n // Commit the truncate transaction immediately\n commit()\n transactionStarted = false\n }\n }\n\n if (hasUpToDate) {\n // Commit transaction if one was started\n if (transactionStarted) {\n commit()\n transactionStarted = false\n }\n\n // Mark the collection as ready now that sync is up to date\n markReady()\n\n // Always commit txids when we receive up-to-date, regardless of transaction state\n seenTxids.setState((currentTxids) => {\n const clonedSeen = new Set<Txid>(currentTxids)\n if (newTxids.size > 0) {\n debug(`new txids synced from pg %O`, Array.from(newTxids))\n }\n newTxids.forEach((txid) => clonedSeen.add(txid))\n newTxids.clear()\n return clonedSeen\n })\n }\n })\n\n // Return the unsubscribe function\n return () => {\n // Unsubscribe from the stream\n unsubscribeStream()\n // Abort the abort controller to stop the stream\n abortController.abort()\n }\n },\n // Expose the getSyncMetadata function\n getSyncMetadata,\n }\n}\n"],"names":[],"mappings":";;;;AA+BA,MAAM,QAAQ,YAAY,MAAM,gBAAgB;AA2NhD,SAAS,kBACP,SACkD;AAClD,SAAO,iBAAiB,OAAO,KAAK,QAAQ,QAAQ,YAAY;AAClE;AAEA,SAAS,qBACP,SACsE;AACtE,SAAO,iBAAiB,OAAO,KAAK,QAAQ,QAAQ,YAAY;AAClE;AAGA,SAAS,SACP,SAC8D;AAC9D,SAAO,WAAW,QAAQ,WAAW,MAAM,QAAQ,QAAQ,QAAQ,KAAK;AAC1E;AAuBO,SAAS,0BAId,QAAiE;AACjE,QAAM,YAAY,IAAI,0BAAqB,IAAI,CAAA,CAAE,CAAC;AAClD,QAAM,OAAO;AAAA,IACX,OAAO;AAAA,IACP;AAAA,MACE;AAAA,IAAA;AAAA,EACF;AASF,QAAM,YAAyB,OAC7B,MACA,UAAkB,QACG;AACrB,UAAM,iCAAiC,IAAI;AAC3C,QAAI,OAAO,SAAS,UAAU;AAC5B,YAAM,IAAI,+BAA+B,OAAO,IAAI;AAAA,IACtD;AAEA,UAAM,UAAU,UAAU,MAAM,IAAI,IAAI;AACxC,QAAI,QAAS,QAAO;AAEpB,WAAO,IAAI,QAAQ,CAAC,SAAS,WAAW;AACtC,YAAM,YAAY,WAAW,MAAM;AACjC,oBAAA;AACA,eAAO,IAAI,2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AAAA,MACF,CAAC;AAGD,aAAO,MAAM;AAEX,0BAAA;AAEA,wBAAgB,MAAA;AAAA,MAClB;AAAA,IACF;AAAA;AAAA,IAEA;AAAA,EAAA;AAEJ;"}
@@ -1,2 +1,2 @@
1
- export { electricCollectionOptions, type ElectricCollectionConfig, type ElectricCollectionUtils, type Txid, } from './electric.js';
1
+ export { electricCollectionOptions, type ElectricCollectionConfig, type ElectricCollectionUtils, type Txid, type AwaitTxIdFn, } from './electric.js';
2
2
  export * from './errors.js';
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@tanstack/electric-db-collection",
3
3
  "description": "ElectricSQL collection for TanStack DB",
4
- "version": "0.1.3",
4
+ "version": "0.1.5",
5
5
  "dependencies": {
6
6
  "@electric-sql/client": "1.0.0",
7
7
  "@standard-schema/spec": "^1.0.0",
8
8
  "@tanstack/store": "^0.7.0",
9
9
  "debug": "^4.4.1",
10
- "@tanstack/db": "0.1.3"
10
+ "@tanstack/db": "0.1.5"
11
11
  },
12
12
  "devDependencies": {
13
13
  "@types/debug": "^4.1.12",
package/src/electric.ts CHANGED
@@ -254,6 +254,12 @@ function isUpToDateMessage<T extends Row<unknown>>(
254
254
  return isControlMessage(message) && message.headers.control === `up-to-date`
255
255
  }
256
256
 
257
+ function isMustRefetchMessage<T extends Row<unknown>>(
258
+ message: Message<T>
259
+ ): message is ControlMessage & { headers: { control: `must-refetch` } } {
260
+ return isControlMessage(message) && message.headers.control === `must-refetch`
261
+ }
262
+
257
263
  // Check if a message contains txids in its headers
258
264
  function hasTxids<T extends Row<unknown>>(
259
265
  message: Message<T>
@@ -470,7 +476,7 @@ function createElectricSync<T extends Row<unknown>>(
470
476
 
471
477
  return {
472
478
  sync: (params: Parameters<SyncConfig<T>[`sync`]>[0]) => {
473
- const { begin, write, commit, markReady } = params
479
+ const { begin, write, commit, markReady, truncate } = params
474
480
  const stream = new ShapeStream({
475
481
  ...shapeOptions,
476
482
  signal: abortController.signal,
@@ -521,6 +527,22 @@ function createElectricSync<T extends Row<unknown>>(
521
527
  })
522
528
  } else if (isUpToDateMessage(message)) {
523
529
  hasUpToDate = true
530
+ } else if (isMustRefetchMessage(message)) {
531
+ debug(
532
+ `Received must-refetch message, starting transaction with truncate`
533
+ )
534
+
535
+ // Start a transaction and truncate the collection
536
+ if (!transactionStarted) {
537
+ begin()
538
+ transactionStarted = true
539
+ }
540
+
541
+ truncate()
542
+
543
+ // Commit the truncate transaction immediately
544
+ commit()
545
+ transactionStarted = false
524
546
  }
525
547
  }
526
548
 
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ export {
3
3
  type ElectricCollectionConfig,
4
4
  type ElectricCollectionUtils,
5
5
  type Txid,
6
+ type AwaitTxIdFn,
6
7
  } from "./electric"
7
8
 
8
9
  export * from "./errors"