@tanstack/electric-db-collection 0.0.2 → 0.0.3

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Kyle Mathews
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,193 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const client = require("@electric-sql/client");
4
+ const store = require("@tanstack/store");
5
+ const DebugModule = require("debug");
6
+ const debug = DebugModule.debug(`ts/db:electric`);
7
+ function isUpToDateMessage(message) {
8
+ return client.isControlMessage(message) && message.headers.control === `up-to-date`;
9
+ }
10
+ function hasTxids(message) {
11
+ return `txids` in message.headers && Array.isArray(message.headers.txids);
12
+ }
13
+ function electricCollectionOptions(config) {
14
+ const seenTxids = new store.Store(/* @__PURE__ */ new Set([]));
15
+ const sync = createElectricSync(
16
+ config.shapeOptions,
17
+ {
18
+ seenTxids
19
+ }
20
+ );
21
+ const awaitTxId = async (txId, timeout = 3e4) => {
22
+ debug(`awaitTxId called with txid %d`, txId);
23
+ if (typeof txId !== `number`) {
24
+ throw new TypeError(
25
+ `Expected number in awaitTxId, received ${typeof txId}`
26
+ );
27
+ }
28
+ const hasTxid = seenTxids.state.has(txId);
29
+ if (hasTxid) return true;
30
+ return new Promise((resolve, reject) => {
31
+ const timeoutId = setTimeout(() => {
32
+ unsubscribe();
33
+ reject(new Error(`Timeout waiting for txId: ${txId}`));
34
+ }, timeout);
35
+ const unsubscribe = seenTxids.subscribe(() => {
36
+ if (seenTxids.state.has(txId)) {
37
+ debug(`awaitTxId found match for txid %o`, txId);
38
+ clearTimeout(timeoutId);
39
+ unsubscribe();
40
+ resolve(true);
41
+ }
42
+ });
43
+ });
44
+ };
45
+ const wrappedOnInsert = config.onInsert ? async (params) => {
46
+ const handlerResult = await config.onInsert(params) ?? {};
47
+ const txid = handlerResult.txid;
48
+ if (!txid) {
49
+ throw new Error(
50
+ `Electric collection onInsert handler must return a txid or array of txids`
51
+ );
52
+ }
53
+ if (Array.isArray(txid)) {
54
+ await Promise.all(txid.map((id) => awaitTxId(id)));
55
+ } else {
56
+ await awaitTxId(txid);
57
+ }
58
+ return handlerResult;
59
+ } : void 0;
60
+ const wrappedOnUpdate = config.onUpdate ? async (params) => {
61
+ const handlerResult = await config.onUpdate(params) ?? {};
62
+ const txid = handlerResult.txid;
63
+ if (!txid) {
64
+ throw new Error(
65
+ `Electric collection onUpdate handler must return a txid or array of txids`
66
+ );
67
+ }
68
+ if (Array.isArray(txid)) {
69
+ await Promise.all(txid.map((id) => awaitTxId(id)));
70
+ } else {
71
+ await awaitTxId(txid);
72
+ }
73
+ return handlerResult;
74
+ } : void 0;
75
+ const wrappedOnDelete = config.onDelete ? async (params) => {
76
+ const handlerResult = await config.onDelete(params);
77
+ if (!handlerResult.txid) {
78
+ throw new Error(
79
+ `Electric collection onDelete handler must return a txid or array of txids`
80
+ );
81
+ }
82
+ if (Array.isArray(handlerResult.txid)) {
83
+ await Promise.all(handlerResult.txid.map((id) => awaitTxId(id)));
84
+ } else {
85
+ await awaitTxId(handlerResult.txid);
86
+ }
87
+ return handlerResult;
88
+ } : void 0;
89
+ const {
90
+ shapeOptions: _shapeOptions,
91
+ onInsert: _onInsert,
92
+ onUpdate: _onUpdate,
93
+ onDelete: _onDelete,
94
+ ...restConfig
95
+ } = config;
96
+ return {
97
+ ...restConfig,
98
+ sync,
99
+ onInsert: wrappedOnInsert,
100
+ onUpdate: wrappedOnUpdate,
101
+ onDelete: wrappedOnDelete,
102
+ utils: {
103
+ awaitTxId
104
+ }
105
+ };
106
+ }
107
+ function createElectricSync(shapeOptions, options) {
108
+ const { seenTxids } = options;
109
+ const relationSchema = new store.Store(void 0);
110
+ const getSyncMetadata = () => {
111
+ var _a;
112
+ const schema = relationSchema.state || `public`;
113
+ return {
114
+ relation: ((_a = shapeOptions.params) == null ? void 0 : _a.table) ? [schema, shapeOptions.params.table] : void 0
115
+ };
116
+ };
117
+ const abortController = new AbortController();
118
+ if (shapeOptions.signal) {
119
+ shapeOptions.signal.addEventListener(`abort`, () => {
120
+ abortController.abort();
121
+ });
122
+ if (shapeOptions.signal.aborted) {
123
+ abortController.abort();
124
+ }
125
+ }
126
+ let unsubscribeStream;
127
+ return {
128
+ sync: (params) => {
129
+ const { begin, write, commit } = params;
130
+ const stream = new client.ShapeStream({
131
+ ...shapeOptions,
132
+ signal: abortController.signal
133
+ });
134
+ let transactionStarted = false;
135
+ const newTxids = /* @__PURE__ */ new Set();
136
+ unsubscribeStream = stream.subscribe((messages) => {
137
+ var _a;
138
+ let hasUpToDate = false;
139
+ for (const message of messages) {
140
+ if (hasTxids(message)) {
141
+ (_a = message.headers.txids) == null ? void 0 : _a.forEach((txid) => newTxids.add(txid));
142
+ }
143
+ if (client.isChangeMessage(message)) {
144
+ const schema = message.headers.schema;
145
+ if (schema && typeof schema === `string`) {
146
+ relationSchema.setState(() => schema);
147
+ }
148
+ if (!transactionStarted) {
149
+ begin();
150
+ transactionStarted = true;
151
+ }
152
+ write({
153
+ type: message.headers.operation,
154
+ value: message.value,
155
+ // Include the primary key and relation info in the metadata
156
+ metadata: {
157
+ ...message.headers
158
+ }
159
+ });
160
+ } else if (isUpToDateMessage(message)) {
161
+ hasUpToDate = true;
162
+ }
163
+ }
164
+ if (hasUpToDate) {
165
+ if (transactionStarted) {
166
+ commit();
167
+ transactionStarted = false;
168
+ } else {
169
+ begin();
170
+ commit();
171
+ }
172
+ seenTxids.setState((currentTxids) => {
173
+ const clonedSeen = new Set(currentTxids);
174
+ if (newTxids.size > 0) {
175
+ debug(`new txids synced from pg %O`, Array.from(newTxids));
176
+ }
177
+ newTxids.forEach((txid) => clonedSeen.add(txid));
178
+ newTxids.clear();
179
+ return clonedSeen;
180
+ });
181
+ }
182
+ });
183
+ return () => {
184
+ unsubscribeStream();
185
+ abortController.abort();
186
+ };
187
+ },
188
+ // Expose the getSyncMetadata function
189
+ getSyncMetadata
190
+ };
191
+ }
192
+ exports.electricCollectionOptions = electricCollectionOptions;
193
+ //# sourceMappingURL=electric.cjs.map
@@ -0,0 +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 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 Electric SQL\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 TypeError(\n `Expected number in awaitTxId, received ${typeof txId}`\n )\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 Error(`Timeout waiting for txId: ${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 Error(\n `Electric collection onInsert handler must return a txid or array of txids`\n )\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 Error(\n `Electric collection onUpdate handler must return a txid or array of txids`\n )\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 Error(\n `Electric collection onDelete handler must return a txid or array of txids`\n )\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 } = params\n const stream = new ShapeStream({\n ...shapeOptions,\n signal: abortController.signal,\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 } else {\n // If the shape is empty, do an empty commit to move the collection status\n // to ready.\n begin()\n commit()\n }\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","ShapeStream","isChangeMessage"],"mappings":";;;;;AAwBA,MAAM,QAAQ,YAAY,MAAM,gBAAgB;AA2NhD,SAAS,kBACP,SACkD;AAClD,SAAOA,OAAAA,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,IAAIC,MAAAA,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;AAAA,QACR,0CAA0C,OAAO,IAAI;AAAA,MAAA;AAAA,IAEzD;AAEA,UAAM,UAAU,UAAU,MAA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AgB,MAAA;AAAA,MAClB;AAAA,IACF;AAAA;AAAA,IAEA;AAAA,EAAA;AAEJ;;"}
@@ -0,0 +1,231 @@
1
+ import { CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams, UtilsRecord } from '@tanstack/db';
2
+ import { StandardSchemaV1 } from '@standard-schema/spec';
3
+ import { GetExtensions, Row, ShapeStreamOptions } from '@electric-sql/client';
4
+ /**
5
+ * Type representing a transaction ID in Electric SQL
6
+ */
7
+ export type Txid = number;
8
+ type InferSchemaOutput<T> = T extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<T> extends Row<unknown> ? StandardSchemaV1.InferOutput<T> : Record<string, unknown> : Record<string, unknown>;
9
+ type ResolveType<TExplicit extends Row<unknown> = Row<unknown>, TSchema extends StandardSchemaV1 = never, TFallback extends object = Record<string, unknown>> = unknown extends GetExtensions<TExplicit> ? [TSchema] extends [never] ? TFallback : InferSchemaOutput<TSchema> : TExplicit;
10
+ /**
11
+ * Configuration interface for Electric collection options
12
+ * @template TExplicit - The explicit type of items in the collection (highest priority)
13
+ * @template TSchema - The schema type for validation and type inference (second priority)
14
+ * @template TFallback - The fallback type if no explicit or schema type is provided
15
+ *
16
+ * @remarks
17
+ * Type resolution follows a priority order:
18
+ * 1. If you provide an explicit type via generic parameter, it will be used
19
+ * 2. If no explicit type is provided but a schema is, the schema's output type will be inferred
20
+ * 3. If neither explicit type nor schema is provided, the fallback type will be used
21
+ *
22
+ * You should provide EITHER an explicit type OR a schema, but not both, as they would conflict.
23
+ */
24
+ export interface ElectricCollectionConfig<TExplicit extends Row<unknown> = Row<unknown>, TSchema extends StandardSchemaV1 = never, TFallback extends Row<unknown> = Row<unknown>> {
25
+ /**
26
+ * Configuration options for the ElectricSQL ShapeStream
27
+ */
28
+ shapeOptions: ShapeStreamOptions<GetExtensions<ResolveType<TExplicit, TSchema, TFallback>>>;
29
+ /**
30
+ * All standard Collection configuration properties
31
+ */
32
+ id?: string;
33
+ schema?: TSchema;
34
+ getKey: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`getKey`];
35
+ sync?: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`sync`];
36
+ /**
37
+ * Optional asynchronous handler function called before an insert operation
38
+ * Must return an object containing a txid number or array of txids
39
+ * @param params Object containing transaction and collection information
40
+ * @returns Promise resolving to an object with txid or txids
41
+ * @example
42
+ * // Basic Electric insert handler - MUST return { txid: number }
43
+ * onInsert: async ({ transaction }) => {
44
+ * const newItem = transaction.mutations[0].modified
45
+ * const result = await api.todos.create({
46
+ * data: newItem
47
+ * })
48
+ * return { txid: result.txid } // Required for Electric sync matching
49
+ * }
50
+ *
51
+ * @example
52
+ * // Insert handler with multiple items - return array of txids
53
+ * onInsert: async ({ transaction }) => {
54
+ * const items = transaction.mutations.map(m => m.modified)
55
+ * const results = await Promise.all(
56
+ * items.map(item => api.todos.create({ data: item }))
57
+ * )
58
+ * return { txid: results.map(r => r.txid) } // Array of txids
59
+ * }
60
+ *
61
+ * @example
62
+ * // Insert handler with error handling
63
+ * onInsert: async ({ transaction }) => {
64
+ * try {
65
+ * const newItem = transaction.mutations[0].modified
66
+ * const result = await api.createTodo(newItem)
67
+ * return { txid: result.txid }
68
+ * } catch (error) {
69
+ * console.error('Insert failed:', error)
70
+ * throw error // This will cause the transaction to fail
71
+ * }
72
+ * }
73
+ *
74
+ * @example
75
+ * // Insert handler with batch operation - single txid
76
+ * onInsert: async ({ transaction }) => {
77
+ * const items = transaction.mutations.map(m => m.modified)
78
+ * const result = await api.todos.createMany({
79
+ * data: items
80
+ * })
81
+ * return { txid: result.txid } // Single txid for batch operation
82
+ * }
83
+ */
84
+ onInsert?: (params: InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
85
+ txid: Txid | Array<Txid>;
86
+ }>;
87
+ /**
88
+ * Optional asynchronous handler function called before an update operation
89
+ * Must return an object containing a txid number or array of txids
90
+ * @param params Object containing transaction and collection information
91
+ * @returns Promise resolving to an object with txid or txids
92
+ * @example
93
+ * // Basic Electric update handler - MUST return { txid: number }
94
+ * onUpdate: async ({ transaction }) => {
95
+ * const { original, changes } = transaction.mutations[0]
96
+ * const result = await api.todos.update({
97
+ * where: { id: original.id },
98
+ * data: changes // Only the changed fields
99
+ * })
100
+ * return { txid: result.txid } // Required for Electric sync matching
101
+ * }
102
+ *
103
+ * @example
104
+ * // Update handler with multiple items - return array of txids
105
+ * onUpdate: async ({ transaction }) => {
106
+ * const updates = await Promise.all(
107
+ * transaction.mutations.map(m =>
108
+ * api.todos.update({
109
+ * where: { id: m.original.id },
110
+ * data: m.changes
111
+ * })
112
+ * )
113
+ * )
114
+ * return { txid: updates.map(u => u.txid) } // Array of txids
115
+ * }
116
+ *
117
+ * @example
118
+ * // Update handler with optimistic rollback
119
+ * onUpdate: async ({ transaction }) => {
120
+ * const mutation = transaction.mutations[0]
121
+ * try {
122
+ * const result = await api.updateTodo(mutation.original.id, mutation.changes)
123
+ * return { txid: result.txid }
124
+ * } catch (error) {
125
+ * // Transaction will automatically rollback optimistic changes
126
+ * console.error('Update failed, rolling back:', error)
127
+ * throw error
128
+ * }
129
+ * }
130
+ */
131
+ onUpdate?: (params: UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
132
+ txid: Txid | Array<Txid>;
133
+ }>;
134
+ /**
135
+ * Optional asynchronous handler function called before a delete operation
136
+ * Must return an object containing a txid number or array of txids
137
+ * @param params Object containing transaction and collection information
138
+ * @returns Promise resolving to an object with txid or txids
139
+ * @example
140
+ * // Basic Electric delete handler - MUST return { txid: number }
141
+ * onDelete: async ({ transaction }) => {
142
+ * const mutation = transaction.mutations[0]
143
+ * const result = await api.todos.delete({
144
+ * id: mutation.original.id
145
+ * })
146
+ * return { txid: result.txid } // Required for Electric sync matching
147
+ * }
148
+ *
149
+ * @example
150
+ * // Delete handler with multiple items - return array of txids
151
+ * onDelete: async ({ transaction }) => {
152
+ * const deletes = await Promise.all(
153
+ * transaction.mutations.map(m =>
154
+ * api.todos.delete({
155
+ * where: { id: m.key }
156
+ * })
157
+ * )
158
+ * )
159
+ * return { txid: deletes.map(d => d.txid) } // Array of txids
160
+ * }
161
+ *
162
+ * @example
163
+ * // Delete handler with batch operation - single txid
164
+ * onDelete: async ({ transaction }) => {
165
+ * const idsToDelete = transaction.mutations.map(m => m.original.id)
166
+ * const result = await api.todos.deleteMany({
167
+ * ids: idsToDelete
168
+ * })
169
+ * return { txid: result.txid } // Single txid for batch operation
170
+ * }
171
+ *
172
+ * @example
173
+ * // Delete handler with optimistic rollback
174
+ * onDelete: async ({ transaction }) => {
175
+ * const mutation = transaction.mutations[0]
176
+ * try {
177
+ * const result = await api.deleteTodo(mutation.original.id)
178
+ * return { txid: result.txid }
179
+ * } catch (error) {
180
+ * // Transaction will automatically rollback optimistic changes
181
+ * console.error('Delete failed, rolling back:', error)
182
+ * throw error
183
+ * }
184
+ * }
185
+ *
186
+ */
187
+ onDelete?: (params: DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
188
+ txid: Txid | Array<Txid>;
189
+ }>;
190
+ }
191
+ /**
192
+ * Type for the awaitTxId utility function
193
+ */
194
+ export type AwaitTxIdFn = (txId: Txid, timeout?: number) => Promise<boolean>;
195
+ /**
196
+ * Electric collection utilities type
197
+ */
198
+ export interface ElectricCollectionUtils extends UtilsRecord {
199
+ awaitTxId: AwaitTxIdFn;
200
+ }
201
+ /**
202
+ * Creates Electric collection options for use with a standard Collection
203
+ *
204
+ * @template TExplicit - The explicit type of items in the collection (highest priority)
205
+ * @template TSchema - The schema type for validation and type inference (second priority)
206
+ * @template TFallback - The fallback type if no explicit or schema type is provided
207
+ * @param config - Configuration options for the Electric collection
208
+ * @returns Collection options with utilities
209
+ */
210
+ export declare function electricCollectionOptions<TExplicit extends Row<unknown> = Row<unknown>, TSchema extends StandardSchemaV1 = never, TFallback extends Row<unknown> = Row<unknown>>(config: ElectricCollectionConfig<TExplicit, TSchema, TFallback>): {
211
+ sync: SyncConfig<ResolveType<TExplicit, TSchema, TFallback>, string | number>;
212
+ onInsert: ((params: InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
213
+ txid: Txid | Array<Txid>;
214
+ }>) | undefined;
215
+ onUpdate: ((params: UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
216
+ txid: Txid | Array<Txid>;
217
+ }>) | undefined;
218
+ onDelete: ((params: DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
219
+ txid: Txid | Array<Txid>;
220
+ }>) | undefined;
221
+ utils: {
222
+ awaitTxId: AwaitTxIdFn;
223
+ };
224
+ /**
225
+ * All standard Collection configuration properties
226
+ */
227
+ id?: string;
228
+ schema?: TSchema | undefined;
229
+ getKey: (item: ResolveType<TExplicit, TSchema, TFallback>) => string | number;
230
+ };
231
+ export {};
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const electric = require("./electric.cjs");
4
+ exports.electricCollectionOptions = electric.electricCollectionOptions;
5
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;"}
@@ -0,0 +1 @@
1
+ export { electricCollectionOptions, type ElectricCollectionConfig, type ElectricCollectionUtils, type Txid, } from './electric.cjs';
@@ -0,0 +1,231 @@
1
+ import { CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams, UtilsRecord } from '@tanstack/db';
2
+ import { StandardSchemaV1 } from '@standard-schema/spec';
3
+ import { GetExtensions, Row, ShapeStreamOptions } from '@electric-sql/client';
4
+ /**
5
+ * Type representing a transaction ID in Electric SQL
6
+ */
7
+ export type Txid = number;
8
+ type InferSchemaOutput<T> = T extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<T> extends Row<unknown> ? StandardSchemaV1.InferOutput<T> : Record<string, unknown> : Record<string, unknown>;
9
+ type ResolveType<TExplicit extends Row<unknown> = Row<unknown>, TSchema extends StandardSchemaV1 = never, TFallback extends object = Record<string, unknown>> = unknown extends GetExtensions<TExplicit> ? [TSchema] extends [never] ? TFallback : InferSchemaOutput<TSchema> : TExplicit;
10
+ /**
11
+ * Configuration interface for Electric collection options
12
+ * @template TExplicit - The explicit type of items in the collection (highest priority)
13
+ * @template TSchema - The schema type for validation and type inference (second priority)
14
+ * @template TFallback - The fallback type if no explicit or schema type is provided
15
+ *
16
+ * @remarks
17
+ * Type resolution follows a priority order:
18
+ * 1. If you provide an explicit type via generic parameter, it will be used
19
+ * 2. If no explicit type is provided but a schema is, the schema's output type will be inferred
20
+ * 3. If neither explicit type nor schema is provided, the fallback type will be used
21
+ *
22
+ * You should provide EITHER an explicit type OR a schema, but not both, as they would conflict.
23
+ */
24
+ export interface ElectricCollectionConfig<TExplicit extends Row<unknown> = Row<unknown>, TSchema extends StandardSchemaV1 = never, TFallback extends Row<unknown> = Row<unknown>> {
25
+ /**
26
+ * Configuration options for the ElectricSQL ShapeStream
27
+ */
28
+ shapeOptions: ShapeStreamOptions<GetExtensions<ResolveType<TExplicit, TSchema, TFallback>>>;
29
+ /**
30
+ * All standard Collection configuration properties
31
+ */
32
+ id?: string;
33
+ schema?: TSchema;
34
+ getKey: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`getKey`];
35
+ sync?: CollectionConfig<ResolveType<TExplicit, TSchema, TFallback>>[`sync`];
36
+ /**
37
+ * Optional asynchronous handler function called before an insert operation
38
+ * Must return an object containing a txid number or array of txids
39
+ * @param params Object containing transaction and collection information
40
+ * @returns Promise resolving to an object with txid or txids
41
+ * @example
42
+ * // Basic Electric insert handler - MUST return { txid: number }
43
+ * onInsert: async ({ transaction }) => {
44
+ * const newItem = transaction.mutations[0].modified
45
+ * const result = await api.todos.create({
46
+ * data: newItem
47
+ * })
48
+ * return { txid: result.txid } // Required for Electric sync matching
49
+ * }
50
+ *
51
+ * @example
52
+ * // Insert handler with multiple items - return array of txids
53
+ * onInsert: async ({ transaction }) => {
54
+ * const items = transaction.mutations.map(m => m.modified)
55
+ * const results = await Promise.all(
56
+ * items.map(item => api.todos.create({ data: item }))
57
+ * )
58
+ * return { txid: results.map(r => r.txid) } // Array of txids
59
+ * }
60
+ *
61
+ * @example
62
+ * // Insert handler with error handling
63
+ * onInsert: async ({ transaction }) => {
64
+ * try {
65
+ * const newItem = transaction.mutations[0].modified
66
+ * const result = await api.createTodo(newItem)
67
+ * return { txid: result.txid }
68
+ * } catch (error) {
69
+ * console.error('Insert failed:', error)
70
+ * throw error // This will cause the transaction to fail
71
+ * }
72
+ * }
73
+ *
74
+ * @example
75
+ * // Insert handler with batch operation - single txid
76
+ * onInsert: async ({ transaction }) => {
77
+ * const items = transaction.mutations.map(m => m.modified)
78
+ * const result = await api.todos.createMany({
79
+ * data: items
80
+ * })
81
+ * return { txid: result.txid } // Single txid for batch operation
82
+ * }
83
+ */
84
+ onInsert?: (params: InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
85
+ txid: Txid | Array<Txid>;
86
+ }>;
87
+ /**
88
+ * Optional asynchronous handler function called before an update operation
89
+ * Must return an object containing a txid number or array of txids
90
+ * @param params Object containing transaction and collection information
91
+ * @returns Promise resolving to an object with txid or txids
92
+ * @example
93
+ * // Basic Electric update handler - MUST return { txid: number }
94
+ * onUpdate: async ({ transaction }) => {
95
+ * const { original, changes } = transaction.mutations[0]
96
+ * const result = await api.todos.update({
97
+ * where: { id: original.id },
98
+ * data: changes // Only the changed fields
99
+ * })
100
+ * return { txid: result.txid } // Required for Electric sync matching
101
+ * }
102
+ *
103
+ * @example
104
+ * // Update handler with multiple items - return array of txids
105
+ * onUpdate: async ({ transaction }) => {
106
+ * const updates = await Promise.all(
107
+ * transaction.mutations.map(m =>
108
+ * api.todos.update({
109
+ * where: { id: m.original.id },
110
+ * data: m.changes
111
+ * })
112
+ * )
113
+ * )
114
+ * return { txid: updates.map(u => u.txid) } // Array of txids
115
+ * }
116
+ *
117
+ * @example
118
+ * // Update handler with optimistic rollback
119
+ * onUpdate: async ({ transaction }) => {
120
+ * const mutation = transaction.mutations[0]
121
+ * try {
122
+ * const result = await api.updateTodo(mutation.original.id, mutation.changes)
123
+ * return { txid: result.txid }
124
+ * } catch (error) {
125
+ * // Transaction will automatically rollback optimistic changes
126
+ * console.error('Update failed, rolling back:', error)
127
+ * throw error
128
+ * }
129
+ * }
130
+ */
131
+ onUpdate?: (params: UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
132
+ txid: Txid | Array<Txid>;
133
+ }>;
134
+ /**
135
+ * Optional asynchronous handler function called before a delete operation
136
+ * Must return an object containing a txid number or array of txids
137
+ * @param params Object containing transaction and collection information
138
+ * @returns Promise resolving to an object with txid or txids
139
+ * @example
140
+ * // Basic Electric delete handler - MUST return { txid: number }
141
+ * onDelete: async ({ transaction }) => {
142
+ * const mutation = transaction.mutations[0]
143
+ * const result = await api.todos.delete({
144
+ * id: mutation.original.id
145
+ * })
146
+ * return { txid: result.txid } // Required for Electric sync matching
147
+ * }
148
+ *
149
+ * @example
150
+ * // Delete handler with multiple items - return array of txids
151
+ * onDelete: async ({ transaction }) => {
152
+ * const deletes = await Promise.all(
153
+ * transaction.mutations.map(m =>
154
+ * api.todos.delete({
155
+ * where: { id: m.key }
156
+ * })
157
+ * )
158
+ * )
159
+ * return { txid: deletes.map(d => d.txid) } // Array of txids
160
+ * }
161
+ *
162
+ * @example
163
+ * // Delete handler with batch operation - single txid
164
+ * onDelete: async ({ transaction }) => {
165
+ * const idsToDelete = transaction.mutations.map(m => m.original.id)
166
+ * const result = await api.todos.deleteMany({
167
+ * ids: idsToDelete
168
+ * })
169
+ * return { txid: result.txid } // Single txid for batch operation
170
+ * }
171
+ *
172
+ * @example
173
+ * // Delete handler with optimistic rollback
174
+ * onDelete: async ({ transaction }) => {
175
+ * const mutation = transaction.mutations[0]
176
+ * try {
177
+ * const result = await api.deleteTodo(mutation.original.id)
178
+ * return { txid: result.txid }
179
+ * } catch (error) {
180
+ * // Transaction will automatically rollback optimistic changes
181
+ * console.error('Delete failed, rolling back:', error)
182
+ * throw error
183
+ * }
184
+ * }
185
+ *
186
+ */
187
+ onDelete?: (params: DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
188
+ txid: Txid | Array<Txid>;
189
+ }>;
190
+ }
191
+ /**
192
+ * Type for the awaitTxId utility function
193
+ */
194
+ export type AwaitTxIdFn = (txId: Txid, timeout?: number) => Promise<boolean>;
195
+ /**
196
+ * Electric collection utilities type
197
+ */
198
+ export interface ElectricCollectionUtils extends UtilsRecord {
199
+ awaitTxId: AwaitTxIdFn;
200
+ }
201
+ /**
202
+ * Creates Electric collection options for use with a standard Collection
203
+ *
204
+ * @template TExplicit - The explicit type of items in the collection (highest priority)
205
+ * @template TSchema - The schema type for validation and type inference (second priority)
206
+ * @template TFallback - The fallback type if no explicit or schema type is provided
207
+ * @param config - Configuration options for the Electric collection
208
+ * @returns Collection options with utilities
209
+ */
210
+ export declare function electricCollectionOptions<TExplicit extends Row<unknown> = Row<unknown>, TSchema extends StandardSchemaV1 = never, TFallback extends Row<unknown> = Row<unknown>>(config: ElectricCollectionConfig<TExplicit, TSchema, TFallback>): {
211
+ sync: SyncConfig<ResolveType<TExplicit, TSchema, TFallback>, string | number>;
212
+ onInsert: ((params: InsertMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
213
+ txid: Txid | Array<Txid>;
214
+ }>) | undefined;
215
+ onUpdate: ((params: UpdateMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
216
+ txid: Txid | Array<Txid>;
217
+ }>) | undefined;
218
+ onDelete: ((params: DeleteMutationFnParams<ResolveType<TExplicit, TSchema, TFallback>>) => Promise<{
219
+ txid: Txid | Array<Txid>;
220
+ }>) | undefined;
221
+ utils: {
222
+ awaitTxId: AwaitTxIdFn;
223
+ };
224
+ /**
225
+ * All standard Collection configuration properties
226
+ */
227
+ id?: string;
228
+ schema?: TSchema | undefined;
229
+ getKey: (item: ResolveType<TExplicit, TSchema, TFallback>) => string | number;
230
+ };
231
+ export {};
@@ -0,0 +1,193 @@
1
+ import { ShapeStream, isChangeMessage, isControlMessage } from "@electric-sql/client";
2
+ import { Store } from "@tanstack/store";
3
+ import DebugModule from "debug";
4
+ const debug = DebugModule.debug(`ts/db:electric`);
5
+ function isUpToDateMessage(message) {
6
+ return isControlMessage(message) && message.headers.control === `up-to-date`;
7
+ }
8
+ function hasTxids(message) {
9
+ return `txids` in message.headers && Array.isArray(message.headers.txids);
10
+ }
11
+ function electricCollectionOptions(config) {
12
+ const seenTxids = new Store(/* @__PURE__ */ new Set([]));
13
+ const sync = createElectricSync(
14
+ config.shapeOptions,
15
+ {
16
+ seenTxids
17
+ }
18
+ );
19
+ const awaitTxId = async (txId, timeout = 3e4) => {
20
+ debug(`awaitTxId called with txid %d`, txId);
21
+ if (typeof txId !== `number`) {
22
+ throw new TypeError(
23
+ `Expected number in awaitTxId, received ${typeof txId}`
24
+ );
25
+ }
26
+ const hasTxid = seenTxids.state.has(txId);
27
+ if (hasTxid) return true;
28
+ return new Promise((resolve, reject) => {
29
+ const timeoutId = setTimeout(() => {
30
+ unsubscribe();
31
+ reject(new Error(`Timeout waiting for txId: ${txId}`));
32
+ }, timeout);
33
+ const unsubscribe = seenTxids.subscribe(() => {
34
+ if (seenTxids.state.has(txId)) {
35
+ debug(`awaitTxId found match for txid %o`, txId);
36
+ clearTimeout(timeoutId);
37
+ unsubscribe();
38
+ resolve(true);
39
+ }
40
+ });
41
+ });
42
+ };
43
+ const wrappedOnInsert = config.onInsert ? async (params) => {
44
+ const handlerResult = await config.onInsert(params) ?? {};
45
+ const txid = handlerResult.txid;
46
+ if (!txid) {
47
+ throw new Error(
48
+ `Electric collection onInsert handler must return a txid or array of txids`
49
+ );
50
+ }
51
+ if (Array.isArray(txid)) {
52
+ await Promise.all(txid.map((id) => awaitTxId(id)));
53
+ } else {
54
+ await awaitTxId(txid);
55
+ }
56
+ return handlerResult;
57
+ } : void 0;
58
+ const wrappedOnUpdate = config.onUpdate ? async (params) => {
59
+ const handlerResult = await config.onUpdate(params) ?? {};
60
+ const txid = handlerResult.txid;
61
+ if (!txid) {
62
+ throw new Error(
63
+ `Electric collection onUpdate handler must return a txid or array of txids`
64
+ );
65
+ }
66
+ if (Array.isArray(txid)) {
67
+ await Promise.all(txid.map((id) => awaitTxId(id)));
68
+ } else {
69
+ await awaitTxId(txid);
70
+ }
71
+ return handlerResult;
72
+ } : void 0;
73
+ const wrappedOnDelete = config.onDelete ? async (params) => {
74
+ const handlerResult = await config.onDelete(params);
75
+ if (!handlerResult.txid) {
76
+ throw new Error(
77
+ `Electric collection onDelete handler must return a txid or array of txids`
78
+ );
79
+ }
80
+ if (Array.isArray(handlerResult.txid)) {
81
+ await Promise.all(handlerResult.txid.map((id) => awaitTxId(id)));
82
+ } else {
83
+ await awaitTxId(handlerResult.txid);
84
+ }
85
+ return handlerResult;
86
+ } : void 0;
87
+ const {
88
+ shapeOptions: _shapeOptions,
89
+ onInsert: _onInsert,
90
+ onUpdate: _onUpdate,
91
+ onDelete: _onDelete,
92
+ ...restConfig
93
+ } = config;
94
+ return {
95
+ ...restConfig,
96
+ sync,
97
+ onInsert: wrappedOnInsert,
98
+ onUpdate: wrappedOnUpdate,
99
+ onDelete: wrappedOnDelete,
100
+ utils: {
101
+ awaitTxId
102
+ }
103
+ };
104
+ }
105
+ function createElectricSync(shapeOptions, options) {
106
+ const { seenTxids } = options;
107
+ const relationSchema = new Store(void 0);
108
+ const getSyncMetadata = () => {
109
+ var _a;
110
+ const schema = relationSchema.state || `public`;
111
+ return {
112
+ relation: ((_a = shapeOptions.params) == null ? void 0 : _a.table) ? [schema, shapeOptions.params.table] : void 0
113
+ };
114
+ };
115
+ const abortController = new AbortController();
116
+ if (shapeOptions.signal) {
117
+ shapeOptions.signal.addEventListener(`abort`, () => {
118
+ abortController.abort();
119
+ });
120
+ if (shapeOptions.signal.aborted) {
121
+ abortController.abort();
122
+ }
123
+ }
124
+ let unsubscribeStream;
125
+ return {
126
+ sync: (params) => {
127
+ const { begin, write, commit } = params;
128
+ const stream = new ShapeStream({
129
+ ...shapeOptions,
130
+ signal: abortController.signal
131
+ });
132
+ let transactionStarted = false;
133
+ const newTxids = /* @__PURE__ */ new Set();
134
+ unsubscribeStream = stream.subscribe((messages) => {
135
+ var _a;
136
+ let hasUpToDate = false;
137
+ for (const message of messages) {
138
+ if (hasTxids(message)) {
139
+ (_a = message.headers.txids) == null ? void 0 : _a.forEach((txid) => newTxids.add(txid));
140
+ }
141
+ if (isChangeMessage(message)) {
142
+ const schema = message.headers.schema;
143
+ if (schema && typeof schema === `string`) {
144
+ relationSchema.setState(() => schema);
145
+ }
146
+ if (!transactionStarted) {
147
+ begin();
148
+ transactionStarted = true;
149
+ }
150
+ write({
151
+ type: message.headers.operation,
152
+ value: message.value,
153
+ // Include the primary key and relation info in the metadata
154
+ metadata: {
155
+ ...message.headers
156
+ }
157
+ });
158
+ } else if (isUpToDateMessage(message)) {
159
+ hasUpToDate = true;
160
+ }
161
+ }
162
+ if (hasUpToDate) {
163
+ if (transactionStarted) {
164
+ commit();
165
+ transactionStarted = false;
166
+ } else {
167
+ begin();
168
+ commit();
169
+ }
170
+ seenTxids.setState((currentTxids) => {
171
+ const clonedSeen = new Set(currentTxids);
172
+ if (newTxids.size > 0) {
173
+ debug(`new txids synced from pg %O`, Array.from(newTxids));
174
+ }
175
+ newTxids.forEach((txid) => clonedSeen.add(txid));
176
+ newTxids.clear();
177
+ return clonedSeen;
178
+ });
179
+ }
180
+ });
181
+ return () => {
182
+ unsubscribeStream();
183
+ abortController.abort();
184
+ };
185
+ },
186
+ // Expose the getSyncMetadata function
187
+ getSyncMetadata
188
+ };
189
+ }
190
+ export {
191
+ electricCollectionOptions
192
+ };
193
+ //# sourceMappingURL=electric.js.map
@@ -0,0 +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 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 Electric SQL\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 TypeError(\n `Expected number in awaitTxId, received ${typeof txId}`\n )\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 Error(`Timeout waiting for txId: ${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 Error(\n `Electric collection onInsert handler must return a txid or array of txids`\n )\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 Error(\n `Electric collection onUpdate handler must return a txid or array of txids`\n )\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 Error(\n `Electric collection onDelete handler must return a txid or array of txids`\n )\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 } = params\n const stream = new ShapeStream({\n ...shapeOptions,\n signal: abortController.signal,\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 } else {\n // If the shape is empty, do an empty commit to move the collection status\n // to ready.\n begin()\n commit()\n }\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":";;;AAwBA,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,Q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iCAAqB;AAAA,UACvB,OAAO;AAGL,kBAAA;AACA,mBAAA;AAAA,UACF;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;"}
@@ -0,0 +1 @@
1
+ export { electricCollectionOptions, type ElectricCollectionConfig, type ElectricCollectionUtils, type Txid, } from './electric.js';
@@ -0,0 +1,5 @@
1
+ import { electricCollectionOptions } from "./electric.js";
2
+ export {
3
+ electricCollectionOptions
4
+ };
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@tanstack/electric-db-collection",
3
3
  "description": "Electric SQL collection for TanStack DB",
4
- "version": "0.0.2",
4
+ "version": "0.0.3",
5
5
  "dependencies": {
6
6
  "@electric-sql/client": "1.0.0",
7
7
  "@standard-schema/spec": "^1.0.0",
8
- "@tanstack/db": "workspace:*",
9
8
  "@tanstack/store": "^0.7.0",
10
- "debug": "^4.4.1"
9
+ "debug": "^4.4.1",
10
+ "@tanstack/db": "0.0.21"
11
11
  },
12
12
  "devDependencies": {
13
13
  "@types/debug": "^4.1.12",
@@ -32,7 +32,6 @@
32
32
  ],
33
33
  "main": "dist/cjs/index.cjs",
34
34
  "module": "dist/esm/index.js",
35
- "packageManager": "pnpm@10.6.3",
36
35
  "peerDependencies": {
37
36
  "@electric-sql/client": ">=1.0.0",
38
37
  "typescript": ">=4.7"
@@ -51,13 +50,13 @@
51
50
  "optimistic",
52
51
  "typescript"
53
52
  ],
53
+ "sideEffects": false,
54
+ "type": "module",
55
+ "types": "dist/esm/index.d.ts",
54
56
  "scripts": {
55
57
  "build": "vite build",
56
58
  "dev": "vite build --watch",
57
59
  "lint": "eslint . --fix",
58
60
  "test": "npx vitest --run"
59
- },
60
- "sideEffects": false,
61
- "type": "module",
62
- "types": "dist/esm/index.d.ts"
63
- }
61
+ }
62
+ }