@tanstack/electric-db-collection 0.1.30 → 0.1.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/electric.cjs +169 -31
- package/dist/cjs/electric.cjs.map +1 -1
- package/dist/cjs/electric.d.cts +116 -7
- package/dist/cjs/errors.cjs +8 -24
- package/dist/cjs/errors.cjs.map +1 -1
- package/dist/cjs/errors.d.cts +2 -5
- package/dist/cjs/index.cjs +2 -3
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/esm/electric.d.ts +116 -7
- package/dist/esm/electric.js +166 -33
- package/dist/esm/electric.js.map +1 -1
- package/dist/esm/errors.d.ts +2 -5
- package/dist/esm/errors.js +8 -24
- package/dist/esm/errors.js.map +1 -1
- package/dist/esm/index.js +3 -4
- package/package.json +2 -2
- package/src/electric.ts +369 -65
- package/src/errors.ts +6 -22
package/dist/cjs/index.cjs
CHANGED
|
@@ -4,9 +4,8 @@ const electric = require("./electric.cjs");
|
|
|
4
4
|
const errors = require("./errors.cjs");
|
|
5
5
|
exports.electricCollectionOptions = electric.electricCollectionOptions;
|
|
6
6
|
exports.ElectricDBCollectionError = errors.ElectricDBCollectionError;
|
|
7
|
-
exports.ElectricDeleteHandlerMustReturnTxIdError = errors.ElectricDeleteHandlerMustReturnTxIdError;
|
|
8
|
-
exports.ElectricInsertHandlerMustReturnTxIdError = errors.ElectricInsertHandlerMustReturnTxIdError;
|
|
9
|
-
exports.ElectricUpdateHandlerMustReturnTxIdError = errors.ElectricUpdateHandlerMustReturnTxIdError;
|
|
10
7
|
exports.ExpectedNumberInAwaitTxIdError = errors.ExpectedNumberInAwaitTxIdError;
|
|
8
|
+
exports.StreamAbortedError = errors.StreamAbortedError;
|
|
9
|
+
exports.TimeoutWaitingForMatchError = errors.TimeoutWaitingForMatchError;
|
|
11
10
|
exports.TimeoutWaitingForTxIdError = errors.TimeoutWaitingForTxIdError;
|
|
12
11
|
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
|
package/dist/esm/electric.d.ts
CHANGED
|
@@ -1,33 +1,143 @@
|
|
|
1
|
-
import { BaseCollectionConfig, CollectionConfig,
|
|
1
|
+
import { BaseCollectionConfig, CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, UpdateMutationFnParams, UtilsRecord } from '@tanstack/db';
|
|
2
2
|
import { StandardSchemaV1 } from '@standard-schema/spec';
|
|
3
|
-
import { GetExtensions, Row, ShapeStreamOptions } from '@electric-sql/client';
|
|
3
|
+
import { GetExtensions, Message, Row, ShapeStreamOptions } from '@electric-sql/client';
|
|
4
|
+
export { isChangeMessage, isControlMessage } from '@electric-sql/client';
|
|
4
5
|
/**
|
|
5
6
|
* Type representing a transaction ID in ElectricSQL
|
|
6
7
|
*/
|
|
7
8
|
export type Txid = number;
|
|
9
|
+
/**
|
|
10
|
+
* Custom match function type - receives stream messages and returns boolean
|
|
11
|
+
* indicating if the mutation has been synchronized
|
|
12
|
+
*/
|
|
13
|
+
export type MatchFunction<T extends Row<unknown>> = (message: Message<T>) => boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Matching strategies for Electric synchronization
|
|
16
|
+
* Handlers can return:
|
|
17
|
+
* - Txid strategy: { txid: number | number[] } (recommended)
|
|
18
|
+
* - Void (no return value) - mutation completes without waiting
|
|
19
|
+
*/
|
|
20
|
+
export type MatchingStrategy = {
|
|
21
|
+
txid: Txid | Array<Txid>;
|
|
22
|
+
} | void;
|
|
8
23
|
type InferSchemaOutput<T> = T extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<T> extends Row<unknown> ? StandardSchemaV1.InferOutput<T> : Record<string, unknown> : Record<string, unknown>;
|
|
9
24
|
/**
|
|
10
25
|
* Configuration interface for Electric collection options
|
|
11
26
|
* @template T - The type of items in the collection
|
|
12
27
|
* @template TSchema - The schema type for validation
|
|
13
28
|
*/
|
|
14
|
-
export interface ElectricCollectionConfig<T extends Row<unknown> = Row<unknown>, TSchema extends StandardSchemaV1 = never> extends BaseCollectionConfig<T, string | number, TSchema,
|
|
15
|
-
txid: Txid | Array<Txid>;
|
|
16
|
-
}> {
|
|
29
|
+
export interface ElectricCollectionConfig<T extends Row<unknown> = Row<unknown>, TSchema extends StandardSchemaV1 = never> extends Omit<BaseCollectionConfig<T, string | number, TSchema, UtilsRecord, any>, `onInsert` | `onUpdate` | `onDelete`> {
|
|
17
30
|
/**
|
|
18
31
|
* Configuration options for the ElectricSQL ShapeStream
|
|
19
32
|
*/
|
|
20
33
|
shapeOptions: ShapeStreamOptions<GetExtensions<T>>;
|
|
34
|
+
/**
|
|
35
|
+
* Optional asynchronous handler function called before an insert operation
|
|
36
|
+
* @param params Object containing transaction and collection information
|
|
37
|
+
* @returns Promise resolving to { txid } or void
|
|
38
|
+
* @example
|
|
39
|
+
* // Basic Electric insert handler with txid (recommended)
|
|
40
|
+
* onInsert: async ({ transaction }) => {
|
|
41
|
+
* const newItem = transaction.mutations[0].modified
|
|
42
|
+
* const result = await api.todos.create({
|
|
43
|
+
* data: newItem
|
|
44
|
+
* })
|
|
45
|
+
* return { txid: result.txid }
|
|
46
|
+
* }
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* // Insert handler with multiple items - return array of txids
|
|
50
|
+
* onInsert: async ({ transaction }) => {
|
|
51
|
+
* const items = transaction.mutations.map(m => m.modified)
|
|
52
|
+
* const results = await Promise.all(
|
|
53
|
+
* items.map(item => api.todos.create({ data: item }))
|
|
54
|
+
* )
|
|
55
|
+
* return { txid: results.map(r => r.txid) }
|
|
56
|
+
* }
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* // Use awaitMatch utility for custom matching
|
|
60
|
+
* onInsert: async ({ transaction, collection }) => {
|
|
61
|
+
* const newItem = transaction.mutations[0].modified
|
|
62
|
+
* await api.todos.create({ data: newItem })
|
|
63
|
+
* await collection.utils.awaitMatch(
|
|
64
|
+
* (message) => isChangeMessage(message) &&
|
|
65
|
+
* message.headers.operation === 'insert' &&
|
|
66
|
+
* message.value.name === newItem.name
|
|
67
|
+
* )
|
|
68
|
+
* }
|
|
69
|
+
*/
|
|
70
|
+
onInsert?: (params: InsertMutationFnParams<T>) => Promise<MatchingStrategy>;
|
|
71
|
+
/**
|
|
72
|
+
* Optional asynchronous handler function called before an update operation
|
|
73
|
+
* @param params Object containing transaction and collection information
|
|
74
|
+
* @returns Promise resolving to { txid } or void
|
|
75
|
+
* @example
|
|
76
|
+
* // Basic Electric update handler with txid (recommended)
|
|
77
|
+
* onUpdate: async ({ transaction }) => {
|
|
78
|
+
* const { original, changes } = transaction.mutations[0]
|
|
79
|
+
* const result = await api.todos.update({
|
|
80
|
+
* where: { id: original.id },
|
|
81
|
+
* data: changes
|
|
82
|
+
* })
|
|
83
|
+
* return { txid: result.txid }
|
|
84
|
+
* }
|
|
85
|
+
*
|
|
86
|
+
* @example
|
|
87
|
+
* // Use awaitMatch utility for custom matching
|
|
88
|
+
* onUpdate: async ({ transaction, collection }) => {
|
|
89
|
+
* const { original, changes } = transaction.mutations[0]
|
|
90
|
+
* await api.todos.update({ where: { id: original.id }, data: changes })
|
|
91
|
+
* await collection.utils.awaitMatch(
|
|
92
|
+
* (message) => isChangeMessage(message) &&
|
|
93
|
+
* message.headers.operation === 'update' &&
|
|
94
|
+
* message.value.id === original.id
|
|
95
|
+
* )
|
|
96
|
+
* }
|
|
97
|
+
*/
|
|
98
|
+
onUpdate?: (params: UpdateMutationFnParams<T>) => Promise<MatchingStrategy>;
|
|
99
|
+
/**
|
|
100
|
+
* Optional asynchronous handler function called before a delete operation
|
|
101
|
+
* @param params Object containing transaction and collection information
|
|
102
|
+
* @returns Promise resolving to { txid } or void
|
|
103
|
+
* @example
|
|
104
|
+
* // Basic Electric delete handler with txid (recommended)
|
|
105
|
+
* onDelete: async ({ transaction }) => {
|
|
106
|
+
* const mutation = transaction.mutations[0]
|
|
107
|
+
* const result = await api.todos.delete({
|
|
108
|
+
* id: mutation.original.id
|
|
109
|
+
* })
|
|
110
|
+
* return { txid: result.txid }
|
|
111
|
+
* }
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* // Use awaitMatch utility for custom matching
|
|
115
|
+
* onDelete: async ({ transaction, collection }) => {
|
|
116
|
+
* const mutation = transaction.mutations[0]
|
|
117
|
+
* await api.todos.delete({ id: mutation.original.id })
|
|
118
|
+
* await collection.utils.awaitMatch(
|
|
119
|
+
* (message) => isChangeMessage(message) &&
|
|
120
|
+
* message.headers.operation === 'delete' &&
|
|
121
|
+
* message.value.id === mutation.original.id
|
|
122
|
+
* )
|
|
123
|
+
* }
|
|
124
|
+
*/
|
|
125
|
+
onDelete?: (params: DeleteMutationFnParams<T>) => Promise<MatchingStrategy>;
|
|
21
126
|
}
|
|
22
127
|
/**
|
|
23
128
|
* Type for the awaitTxId utility function
|
|
24
129
|
*/
|
|
25
130
|
export type AwaitTxIdFn = (txId: Txid, timeout?: number) => Promise<boolean>;
|
|
131
|
+
/**
|
|
132
|
+
* Type for the awaitMatch utility function
|
|
133
|
+
*/
|
|
134
|
+
export type AwaitMatchFn<T extends Row<unknown>> = (matchFn: MatchFunction<T>, timeout?: number) => Promise<boolean>;
|
|
26
135
|
/**
|
|
27
136
|
* Electric collection utilities type
|
|
28
137
|
*/
|
|
29
|
-
export interface ElectricCollectionUtils extends UtilsRecord {
|
|
138
|
+
export interface ElectricCollectionUtils<T extends Row<unknown> = Row<unknown>> extends UtilsRecord {
|
|
30
139
|
awaitTxId: AwaitTxIdFn;
|
|
140
|
+
awaitMatch: AwaitMatchFn<T>;
|
|
31
141
|
}
|
|
32
142
|
/**
|
|
33
143
|
* Creates Electric collection options for use with a standard Collection
|
|
@@ -52,4 +162,3 @@ export declare function electricCollectionOptions<T extends Row<unknown>>(config
|
|
|
52
162
|
utils: ElectricCollectionUtils;
|
|
53
163
|
schema?: never;
|
|
54
164
|
};
|
|
55
|
-
export {};
|
package/dist/esm/electric.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { ShapeStream, isChangeMessage, isVisibleInSnapshot, isControlMessage } from "@electric-sql/client";
|
|
2
|
+
import { isChangeMessage as isChangeMessage2, isControlMessage as isControlMessage2 } from "@electric-sql/client";
|
|
2
3
|
import { Store } from "@tanstack/store";
|
|
3
4
|
import DebugModule from "debug";
|
|
4
|
-
import {
|
|
5
|
+
import { StreamAbortedError, ExpectedNumberInAwaitTxIdError, TimeoutWaitingForTxIdError, TimeoutWaitingForMatchError } from "./errors.js";
|
|
5
6
|
const debug = DebugModule.debug(`ts/db:electric`);
|
|
6
7
|
function isUpToDateMessage(message) {
|
|
7
8
|
return isControlMessage(message) && message.headers.control === `up-to-date`;
|
|
@@ -25,12 +26,42 @@ function hasTxids(message) {
|
|
|
25
26
|
function electricCollectionOptions(config) {
|
|
26
27
|
const seenTxids = new Store(/* @__PURE__ */ new Set([]));
|
|
27
28
|
const seenSnapshots = new Store([]);
|
|
29
|
+
const pendingMatches = new Store(/* @__PURE__ */ new Map());
|
|
30
|
+
const currentBatchMessages = new Store([]);
|
|
31
|
+
const removePendingMatches = (matchIds) => {
|
|
32
|
+
if (matchIds.length > 0) {
|
|
33
|
+
pendingMatches.setState((current) => {
|
|
34
|
+
const newMatches = new Map(current);
|
|
35
|
+
matchIds.forEach((id) => newMatches.delete(id));
|
|
36
|
+
return newMatches;
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
const resolveMatchedPendingMatches = () => {
|
|
41
|
+
const matchesToResolve = [];
|
|
42
|
+
pendingMatches.state.forEach((match, matchId) => {
|
|
43
|
+
if (match.matched) {
|
|
44
|
+
clearTimeout(match.timeoutId);
|
|
45
|
+
match.resolve(true);
|
|
46
|
+
matchesToResolve.push(matchId);
|
|
47
|
+
debug(
|
|
48
|
+
`${config.id ? `[${config.id}] ` : ``}awaitMatch resolved on up-to-date for match %s`,
|
|
49
|
+
matchId
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
removePendingMatches(matchesToResolve);
|
|
54
|
+
};
|
|
28
55
|
const sync = createElectricSync(config.shapeOptions, {
|
|
29
56
|
seenTxids,
|
|
30
57
|
seenSnapshots,
|
|
58
|
+
pendingMatches,
|
|
59
|
+
currentBatchMessages,
|
|
60
|
+
removePendingMatches,
|
|
61
|
+
resolveMatchedPendingMatches,
|
|
31
62
|
collectionId: config.id
|
|
32
63
|
});
|
|
33
|
-
const awaitTxId = async (txId, timeout =
|
|
64
|
+
const awaitTxId = async (txId, timeout = 5e3) => {
|
|
34
65
|
debug(
|
|
35
66
|
`${config.id ? `[${config.id}] ` : ``}awaitTxId called with txid %d`,
|
|
36
67
|
txId
|
|
@@ -80,42 +111,96 @@ function electricCollectionOptions(config) {
|
|
|
80
111
|
});
|
|
81
112
|
});
|
|
82
113
|
};
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
114
|
+
const awaitMatch = async (matchFn, timeout = 3e3) => {
|
|
115
|
+
debug(
|
|
116
|
+
`${config.id ? `[${config.id}] ` : ``}awaitMatch called with custom function`
|
|
117
|
+
);
|
|
118
|
+
return new Promise((resolve, reject) => {
|
|
119
|
+
const matchId = Math.random().toString(36);
|
|
120
|
+
const cleanupMatch = () => {
|
|
121
|
+
pendingMatches.setState((current) => {
|
|
122
|
+
const newMatches = new Map(current);
|
|
123
|
+
newMatches.delete(matchId);
|
|
124
|
+
return newMatches;
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
const onTimeout = () => {
|
|
128
|
+
cleanupMatch();
|
|
129
|
+
reject(new TimeoutWaitingForMatchError(config.id));
|
|
130
|
+
};
|
|
131
|
+
const timeoutId = setTimeout(onTimeout, timeout);
|
|
132
|
+
const checkMatch = (message) => {
|
|
133
|
+
if (matchFn(message)) {
|
|
134
|
+
debug(
|
|
135
|
+
`${config.id ? `[${config.id}] ` : ``}awaitMatch found matching message, waiting for up-to-date`
|
|
136
|
+
);
|
|
137
|
+
pendingMatches.setState((current) => {
|
|
138
|
+
const newMatches = new Map(current);
|
|
139
|
+
const existing = newMatches.get(matchId);
|
|
140
|
+
if (existing) {
|
|
141
|
+
newMatches.set(matchId, { ...existing, matched: true });
|
|
142
|
+
}
|
|
143
|
+
return newMatches;
|
|
144
|
+
});
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
};
|
|
149
|
+
for (const message of currentBatchMessages.state) {
|
|
150
|
+
if (matchFn(message)) {
|
|
151
|
+
debug(
|
|
152
|
+
`${config.id ? `[${config.id}] ` : ``}awaitMatch found immediate match in current batch, waiting for up-to-date`
|
|
153
|
+
);
|
|
154
|
+
pendingMatches.setState((current) => {
|
|
155
|
+
const newMatches = new Map(current);
|
|
156
|
+
newMatches.set(matchId, {
|
|
157
|
+
matchFn: checkMatch,
|
|
158
|
+
resolve,
|
|
159
|
+
reject,
|
|
160
|
+
timeoutId,
|
|
161
|
+
matched: true
|
|
162
|
+
// Already matched
|
|
163
|
+
});
|
|
164
|
+
return newMatches;
|
|
165
|
+
});
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
pendingMatches.setState((current) => {
|
|
170
|
+
const newMatches = new Map(current);
|
|
171
|
+
newMatches.set(matchId, {
|
|
172
|
+
matchFn: checkMatch,
|
|
173
|
+
resolve,
|
|
174
|
+
reject,
|
|
175
|
+
timeoutId,
|
|
176
|
+
matched: false
|
|
177
|
+
});
|
|
178
|
+
return newMatches;
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
const processMatchingStrategy = async (result) => {
|
|
183
|
+
if (result && `txid` in result) {
|
|
184
|
+
if (Array.isArray(result.txid)) {
|
|
185
|
+
await Promise.all(result.txid.map(awaitTxId));
|
|
186
|
+
} else {
|
|
187
|
+
await awaitTxId(result.txid);
|
|
188
|
+
}
|
|
93
189
|
}
|
|
190
|
+
};
|
|
191
|
+
const wrappedOnInsert = config.onInsert ? async (params) => {
|
|
192
|
+
const handlerResult = await config.onInsert(params);
|
|
193
|
+
await processMatchingStrategy(handlerResult);
|
|
94
194
|
return handlerResult;
|
|
95
195
|
} : void 0;
|
|
96
196
|
const wrappedOnUpdate = config.onUpdate ? async (params) => {
|
|
97
|
-
const handlerResult = await config.onUpdate(params)
|
|
98
|
-
|
|
99
|
-
if (!txid) {
|
|
100
|
-
throw new ElectricUpdateHandlerMustReturnTxIdError(config.id);
|
|
101
|
-
}
|
|
102
|
-
if (Array.isArray(txid)) {
|
|
103
|
-
await Promise.all(txid.map((id) => awaitTxId(id)));
|
|
104
|
-
} else {
|
|
105
|
-
await awaitTxId(txid);
|
|
106
|
-
}
|
|
197
|
+
const handlerResult = await config.onUpdate(params);
|
|
198
|
+
await processMatchingStrategy(handlerResult);
|
|
107
199
|
return handlerResult;
|
|
108
200
|
} : void 0;
|
|
109
201
|
const wrappedOnDelete = config.onDelete ? async (params) => {
|
|
110
202
|
const handlerResult = await config.onDelete(params);
|
|
111
|
-
|
|
112
|
-
throw new ElectricDeleteHandlerMustReturnTxIdError(config.id);
|
|
113
|
-
}
|
|
114
|
-
if (Array.isArray(handlerResult.txid)) {
|
|
115
|
-
await Promise.all(handlerResult.txid.map((id) => awaitTxId(id)));
|
|
116
|
-
} else {
|
|
117
|
-
await awaitTxId(handlerResult.txid);
|
|
118
|
-
}
|
|
203
|
+
await processMatchingStrategy(handlerResult);
|
|
119
204
|
return handlerResult;
|
|
120
205
|
} : void 0;
|
|
121
206
|
const {
|
|
@@ -132,12 +217,22 @@ function electricCollectionOptions(config) {
|
|
|
132
217
|
onUpdate: wrappedOnUpdate,
|
|
133
218
|
onDelete: wrappedOnDelete,
|
|
134
219
|
utils: {
|
|
135
|
-
awaitTxId
|
|
220
|
+
awaitTxId,
|
|
221
|
+
awaitMatch
|
|
136
222
|
}
|
|
137
223
|
};
|
|
138
224
|
}
|
|
139
225
|
function createElectricSync(shapeOptions, options) {
|
|
140
|
-
const {
|
|
226
|
+
const {
|
|
227
|
+
seenTxids,
|
|
228
|
+
seenSnapshots,
|
|
229
|
+
pendingMatches,
|
|
230
|
+
currentBatchMessages,
|
|
231
|
+
removePendingMatches,
|
|
232
|
+
resolveMatchedPendingMatches,
|
|
233
|
+
collectionId
|
|
234
|
+
} = options;
|
|
235
|
+
const MAX_BATCH_MESSAGES = 1e3;
|
|
141
236
|
const relationSchema = new Store(void 0);
|
|
142
237
|
const getSyncMetadata = () => {
|
|
143
238
|
const schema = relationSchema.state || `public`;
|
|
@@ -164,6 +259,15 @@ function createElectricSync(shapeOptions, options) {
|
|
|
164
259
|
abortController.abort();
|
|
165
260
|
}
|
|
166
261
|
}
|
|
262
|
+
abortController.signal.addEventListener(`abort`, () => {
|
|
263
|
+
pendingMatches.setState((current) => {
|
|
264
|
+
current.forEach((match) => {
|
|
265
|
+
clearTimeout(match.timeoutId);
|
|
266
|
+
match.reject(new StreamAbortedError());
|
|
267
|
+
});
|
|
268
|
+
return /* @__PURE__ */ new Map();
|
|
269
|
+
});
|
|
270
|
+
});
|
|
167
271
|
const stream = new ShapeStream({
|
|
168
272
|
...shapeOptions,
|
|
169
273
|
signal: abortController.signal,
|
|
@@ -188,9 +292,34 @@ You can provide an 'onError' handler on the shapeOptions to handle this error, a
|
|
|
188
292
|
unsubscribeStream = stream.subscribe((messages) => {
|
|
189
293
|
let hasUpToDate = false;
|
|
190
294
|
for (const message of messages) {
|
|
295
|
+
if (isChangeMessage(message)) {
|
|
296
|
+
currentBatchMessages.setState((currentBuffer) => {
|
|
297
|
+
const newBuffer = [...currentBuffer, message];
|
|
298
|
+
if (newBuffer.length > MAX_BATCH_MESSAGES) {
|
|
299
|
+
newBuffer.splice(0, newBuffer.length - MAX_BATCH_MESSAGES);
|
|
300
|
+
}
|
|
301
|
+
return newBuffer;
|
|
302
|
+
});
|
|
303
|
+
}
|
|
191
304
|
if (hasTxids(message)) {
|
|
192
305
|
message.headers.txids?.forEach((txid) => newTxids.add(txid));
|
|
193
306
|
}
|
|
307
|
+
const matchesToRemove = [];
|
|
308
|
+
pendingMatches.state.forEach((match, matchId) => {
|
|
309
|
+
if (!match.matched) {
|
|
310
|
+
try {
|
|
311
|
+
match.matchFn(message);
|
|
312
|
+
} catch (err) {
|
|
313
|
+
clearTimeout(match.timeoutId);
|
|
314
|
+
match.reject(
|
|
315
|
+
err instanceof Error ? err : new Error(String(err))
|
|
316
|
+
);
|
|
317
|
+
matchesToRemove.push(matchId);
|
|
318
|
+
debug(`matchFn error: %o`, err);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
});
|
|
322
|
+
removePendingMatches(matchesToRemove);
|
|
194
323
|
if (isChangeMessage(message)) {
|
|
195
324
|
const schema = message.headers.schema;
|
|
196
325
|
if (schema && typeof schema === `string`) {
|
|
@@ -225,6 +354,7 @@ You can provide an 'onError' handler on the shapeOptions to handle this error, a
|
|
|
225
354
|
}
|
|
226
355
|
}
|
|
227
356
|
if (hasUpToDate) {
|
|
357
|
+
currentBatchMessages.setState(() => []);
|
|
228
358
|
if (transactionStarted) {
|
|
229
359
|
commit();
|
|
230
360
|
transactionStarted = false;
|
|
@@ -253,6 +383,7 @@ You can provide an 'onError' handler on the shapeOptions to handle this error, a
|
|
|
253
383
|
newSnapshots.length = 0;
|
|
254
384
|
return seen;
|
|
255
385
|
});
|
|
386
|
+
resolveMatchedPendingMatches();
|
|
256
387
|
}
|
|
257
388
|
});
|
|
258
389
|
return () => {
|
|
@@ -265,6 +396,8 @@ You can provide an 'onError' handler on the shapeOptions to handle this error, a
|
|
|
265
396
|
};
|
|
266
397
|
}
|
|
267
398
|
export {
|
|
268
|
-
electricCollectionOptions
|
|
399
|
+
electricCollectionOptions,
|
|
400
|
+
isChangeMessage2 as isChangeMessage,
|
|
401
|
+
isControlMessage2 as isControlMessage
|
|
269
402
|
};
|
|
270
403
|
//# sourceMappingURL=electric.js.map
|
package/dist/esm/electric.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"electric.js","sources":["../../src/electric.ts"],"sourcesContent":["import {\n ShapeStream,\n isChangeMessage,\n isControlMessage,\n isVisibleInSnapshot,\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 BaseCollectionConfig,\n CollectionConfig,\n DeleteMutationFnParams,\n Fn,\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 PostgresSnapshot,\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/**\n * Type representing the result of an insert, update, or delete handler\n */\ntype MaybeTxId =\n | {\n txid?: Txid | Array<Txid>\n }\n | undefined\n | null\n\n/**\n * Type representing a snapshot end message\n */\ntype SnapshotEndMessage = ControlMessage & {\n headers: { control: `snapshot-end` }\n}\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\n/**\n * Configuration interface for Electric collection options\n * @template T - The type of items in the collection\n * @template TSchema - The schema type for validation\n */\nexport interface ElectricCollectionConfig<\n T extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n> extends BaseCollectionConfig<\n T,\n string | number,\n TSchema,\n Record<string, Fn>,\n { txid: Txid | Array<Txid> }\n > {\n /**\n * Configuration options for the ElectricSQL ShapeStream\n */\n shapeOptions: ShapeStreamOptions<GetExtensions<T>>\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\nfunction isSnapshotEndMessage<T extends Row<unknown>>(\n message: Message<T>\n): message is SnapshotEndMessage {\n return isControlMessage(message) && message.headers.control === `snapshot-end`\n}\n\nfunction parseSnapshotMessage(message: SnapshotEndMessage): PostgresSnapshot {\n return {\n xmin: message.headers.xmin,\n xmax: message.headers.xmax,\n xip_list: message.headers.xip_list,\n }\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 T - 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 */\n\n// Overload for when schema is provided\nexport function electricCollectionOptions<T extends StandardSchemaV1>(\n config: ElectricCollectionConfig<InferSchemaOutput<T>, T> & {\n schema: T\n }\n): CollectionConfig<InferSchemaOutput<T>, string | number, T> & {\n id?: string\n utils: ElectricCollectionUtils\n schema: T\n}\n\n// Overload for when no schema is provided\nexport function electricCollectionOptions<T extends Row<unknown>>(\n config: ElectricCollectionConfig<T> & {\n schema?: never // prohibit schema\n }\n): CollectionConfig<T, string | number> & {\n id?: string\n utils: ElectricCollectionUtils\n schema?: never // no schema in the result\n}\n\nexport function electricCollectionOptions(\n config: ElectricCollectionConfig<any, any>\n): CollectionConfig<any, string | number, any> & {\n id?: string\n utils: ElectricCollectionUtils\n schema?: any\n} {\n const seenTxids = new Store<Set<Txid>>(new Set([]))\n const seenSnapshots = new Store<Array<PostgresSnapshot>>([])\n const sync = createElectricSync<any>(config.shapeOptions, {\n seenTxids,\n seenSnapshots,\n collectionId: config.id,\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(\n `${config.id ? `[${config.id}] ` : ``}awaitTxId called with txid %d`,\n txId\n )\n if (typeof txId !== `number`) {\n throw new ExpectedNumberInAwaitTxIdError(typeof txId, config.id)\n }\n\n // First check if the txid is in the seenTxids store\n const hasTxid = seenTxids.state.has(txId)\n if (hasTxid) return true\n\n // Then check if the txid is in any of the seen snapshots\n const hasSnapshot = seenSnapshots.state.some((snapshot) =>\n isVisibleInSnapshot(txId, snapshot)\n )\n if (hasSnapshot) return true\n\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n unsubscribeSeenTxids()\n unsubscribeSeenSnapshots()\n reject(new TimeoutWaitingForTxIdError(txId, config.id))\n }, timeout)\n\n const unsubscribeSeenTxids = seenTxids.subscribe(() => {\n if (seenTxids.state.has(txId)) {\n debug(\n `${config.id ? `[${config.id}] ` : ``}awaitTxId found match for txid %o`,\n txId\n )\n clearTimeout(timeoutId)\n unsubscribeSeenTxids()\n unsubscribeSeenSnapshots()\n resolve(true)\n }\n })\n\n const unsubscribeSeenSnapshots = seenSnapshots.subscribe(() => {\n const visibleSnapshot = seenSnapshots.state.find((snapshot) =>\n isVisibleInSnapshot(txId, snapshot)\n )\n if (visibleSnapshot) {\n debug(\n `${config.id ? `[${config.id}] ` : ``}awaitTxId found match for txid %o in snapshot %o`,\n txId,\n visibleSnapshot\n )\n clearTimeout(timeoutId)\n unsubscribeSeenSnapshots()\n unsubscribeSeenTxids()\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 (params: InsertMutationFnParams<any>) => {\n // Runtime check (that doesn't follow type)\n\n const handlerResult =\n ((await config.onInsert!(params)) as MaybeTxId) ?? {}\n const txid = handlerResult.txid\n\n if (!txid) {\n throw new ElectricInsertHandlerMustReturnTxIdError(config.id)\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 (params: UpdateMutationFnParams<any>) => {\n // Runtime check (that doesn't follow type)\n\n const handlerResult =\n ((await config.onUpdate!(params)) as MaybeTxId) ?? {}\n const txid = handlerResult.txid\n\n if (!txid) {\n throw new ElectricUpdateHandlerMustReturnTxIdError(config.id)\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 (params: DeleteMutationFnParams<any>) => {\n const handlerResult = await config.onDelete!(params)\n if (!handlerResult.txid) {\n throw new ElectricDeleteHandlerMustReturnTxIdError(config.id)\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 seenSnapshots: Store<Array<PostgresSnapshot>>\n collectionId?: string\n }\n): SyncConfig<T> {\n const { seenTxids, seenSnapshots, collectionId } = 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 let unsubscribeStream: () => void\n\n return {\n sync: (params: Parameters<SyncConfig<T>[`sync`]>[0]) => {\n const { begin, write, commit, markReady, truncate, collection } = params\n\n // Abort controller for the stream - wraps the signal if provided\n const abortController = new AbortController()\n\n if (shapeOptions.signal) {\n shapeOptions.signal.addEventListener(\n `abort`,\n () => {\n abortController.abort()\n },\n {\n once: true,\n }\n )\n if (shapeOptions.signal.aborted) {\n abortController.abort()\n }\n }\n\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 // Note that Electric sends a 409 error on a `must-refetch` message, but the\n // ShapeStream handled this and it will not reach this handler, therefor\n // this markReady will not be triggers by a `must-refetch`.\n markReady()\n\n if (shapeOptions.onError) {\n return shapeOptions.onError(errorParams)\n } else {\n console.error(\n `An error occurred while syncing collection: ${collection.id}, \\n` +\n `it has been marked as ready to avoid blocking apps waiting for '.preload()' to finish. \\n` +\n `You can provide an 'onError' handler on the shapeOptions to handle this error, and this message will not be logged.`,\n errorParams\n )\n }\n\n return\n },\n })\n let transactionStarted = false\n const newTxids = new Set<Txid>()\n const newSnapshots: Array<PostgresSnapshot> = []\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 (isSnapshotEndMessage(message)) {\n newSnapshots.push(parseSnapshotMessage(message))\n } else if (isUpToDateMessage(message)) {\n hasUpToDate = true\n } else if (isMustRefetchMessage(message)) {\n debug(\n `${collectionId ? `[${collectionId}] ` : ``}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 // Reset hasUpToDate so we continue accumulating changes until next up-to-date\n hasUpToDate = 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(\n `${collectionId ? `[${collectionId}] ` : ``}new txids synced from pg %O`,\n Array.from(newTxids)\n )\n }\n newTxids.forEach((txid) => clonedSeen.add(txid))\n newTxids.clear()\n return clonedSeen\n })\n\n // Always commit snapshots when we receive up-to-date, regardless of transaction state\n seenSnapshots.setState((currentSnapshots) => {\n const seen = [...currentSnapshots, ...newSnapshots]\n newSnapshots.forEach((snapshot) =>\n debug(\n `${collectionId ? `[${collectionId}] ` : ``}new snapshot synced from pg %o`,\n snapshot\n )\n )\n newSnapshots.length = 0\n return seen\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":";;;;AAmCA,MAAM,QAAQ,YAAY,MAAM,gBAAgB;AAsDhD,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;AAEA,SAAS,qBACP,SAC+B;AAC/B,SAAO,iBAAiB,OAAO,KAAK,QAAQ,QAAQ,YAAY;AAClE;AAEA,SAAS,qBAAqB,SAA+C;AAC3E,SAAO;AAAA,IACL,MAAM,QAAQ,QAAQ;AAAA,IACtB,MAAM,QAAQ,QAAQ;AAAA,IACtB,UAAU,QAAQ,QAAQ;AAAA,EAAA;AAE9B;AAGA,SAAS,SACP,SAC8D;AAC9D,SAAO,WAAW,QAAQ,WAAW,MAAM,QAAQ,QAAQ,QAAQ,KAAK;AAC1E;AA8CO,SAAS,0BACd,QAKA;AACA,QAAM,YAAY,IAAI,0BAAqB,IAAI,CAAA,CAAE,C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AAC7C,gBAAI,SAAS,OAAO,GAAG;AACrB;AAAA,gBACE,GAAG,eAAe,IAAI,YAAY,OAAO,EAAE;AAAA,gBAC3C,MAAM,KAAK,QAAQ;AAAA,cAAA;AAAA,YAEvB;AACA,qBAAS,QAAQ,CAAC,SAAS,WAAW,IAAI,IAAI,CAAC;AAC/C,qBAAS,MAAA;AACT,mBAAO;AAAA,UACT,CAAC;AAGD,wBAAc,SAAS,CAAC,qBAAqB;AAC3C,kBAAM,OAAO,CAAC,GAAG,kBAAkB,GAAG,YAAY;AAClD,yBAAa;AAAA,cAAQ,CAAC,aACpB;AAAA,gBACE,GAAG,eAAe,IAAI,YAAY,OAAO,EAAE;AAAA,gBAC3C;AAAA,cAAA;AAAA,YACF;AAEF,yBAAa,SAAS;AACtB,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.js","sources":["../../src/electric.ts"],"sourcesContent":["import {\n ShapeStream,\n isChangeMessage,\n isControlMessage,\n isVisibleInSnapshot,\n} from \"@electric-sql/client\"\nimport { Store } from \"@tanstack/store\"\nimport DebugModule from \"debug\"\nimport {\n ExpectedNumberInAwaitTxIdError,\n StreamAbortedError,\n TimeoutWaitingForMatchError,\n TimeoutWaitingForTxIdError,\n} from \"./errors\"\nimport type {\n BaseCollectionConfig,\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 PostgresSnapshot,\n Row,\n ShapeStreamOptions,\n} from \"@electric-sql/client\"\n\n// Re-export for user convenience in custom match functions\nexport { isChangeMessage, isControlMessage } 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/**\n * Custom match function type - receives stream messages and returns boolean\n * indicating if the mutation has been synchronized\n */\nexport type MatchFunction<T extends Row<unknown>> = (\n message: Message<T>\n) => boolean\n\n/**\n * Matching strategies for Electric synchronization\n * Handlers can return:\n * - Txid strategy: { txid: number | number[] } (recommended)\n * - Void (no return value) - mutation completes without waiting\n */\nexport type MatchingStrategy = { txid: Txid | Array<Txid> } | void\n\n/**\n * Type representing a snapshot end message\n */\ntype SnapshotEndMessage = ControlMessage & {\n headers: { control: `snapshot-end` }\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\n/**\n * Configuration interface for Electric collection options\n * @template T - The type of items in the collection\n * @template TSchema - The schema type for validation\n */\nexport interface ElectricCollectionConfig<\n T extends Row<unknown> = Row<unknown>,\n TSchema extends StandardSchemaV1 = never,\n> extends Omit<\n BaseCollectionConfig<T, string | number, TSchema, UtilsRecord, any>,\n `onInsert` | `onUpdate` | `onDelete`\n > {\n /**\n * Configuration options for the ElectricSQL ShapeStream\n */\n shapeOptions: ShapeStreamOptions<GetExtensions<T>>\n\n /**\n * Optional asynchronous handler function called before an insert operation\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to { txid } or void\n * @example\n * // Basic Electric insert handler with txid (recommended)\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 }\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) }\n * }\n *\n * @example\n * // Use awaitMatch utility for custom matching\n * onInsert: async ({ transaction, collection }) => {\n * const newItem = transaction.mutations[0].modified\n * await api.todos.create({ data: newItem })\n * await collection.utils.awaitMatch(\n * (message) => isChangeMessage(message) &&\n * message.headers.operation === 'insert' &&\n * message.value.name === newItem.name\n * )\n * }\n */\n onInsert?: (params: InsertMutationFnParams<T>) => Promise<MatchingStrategy>\n\n /**\n * Optional asynchronous handler function called before an update operation\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to { txid } or void\n * @example\n * // Basic Electric update handler with txid (recommended)\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\n * })\n * return { txid: result.txid }\n * }\n *\n * @example\n * // Use awaitMatch utility for custom matching\n * onUpdate: async ({ transaction, collection }) => {\n * const { original, changes } = transaction.mutations[0]\n * await api.todos.update({ where: { id: original.id }, data: changes })\n * await collection.utils.awaitMatch(\n * (message) => isChangeMessage(message) &&\n * message.headers.operation === 'update' &&\n * message.value.id === original.id\n * )\n * }\n */\n onUpdate?: (params: UpdateMutationFnParams<T>) => Promise<MatchingStrategy>\n\n /**\n * Optional asynchronous handler function called before a delete operation\n * @param params Object containing transaction and collection information\n * @returns Promise resolving to { txid } or void\n * @example\n * // Basic Electric delete handler with txid (recommended)\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 }\n * }\n *\n * @example\n * // Use awaitMatch utility for custom matching\n * onDelete: async ({ transaction, collection }) => {\n * const mutation = transaction.mutations[0]\n * await api.todos.delete({ id: mutation.original.id })\n * await collection.utils.awaitMatch(\n * (message) => isChangeMessage(message) &&\n * message.headers.operation === 'delete' &&\n * message.value.id === mutation.original.id\n * )\n * }\n */\n onDelete?: (params: DeleteMutationFnParams<T>) => Promise<MatchingStrategy>\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\nfunction isSnapshotEndMessage<T extends Row<unknown>>(\n message: Message<T>\n): message is SnapshotEndMessage {\n return isControlMessage(message) && message.headers.control === `snapshot-end`\n}\n\nfunction parseSnapshotMessage(message: SnapshotEndMessage): PostgresSnapshot {\n return {\n xmin: message.headers.xmin,\n xmax: message.headers.xmax,\n xip_list: message.headers.xip_list,\n }\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 * Type for the awaitMatch utility function\n */\nexport type AwaitMatchFn<T extends Row<unknown>> = (\n matchFn: MatchFunction<T>,\n timeout?: number\n) => Promise<boolean>\n\n/**\n * Electric collection utilities type\n */\nexport interface ElectricCollectionUtils<T extends Row<unknown> = Row<unknown>>\n extends UtilsRecord {\n awaitTxId: AwaitTxIdFn\n awaitMatch: AwaitMatchFn<T>\n}\n\n/**\n * Creates Electric collection options for use with a standard Collection\n *\n * @template T - 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 */\n\n// Overload for when schema is provided\nexport function electricCollectionOptions<T extends StandardSchemaV1>(\n config: ElectricCollectionConfig<InferSchemaOutput<T>, T> & {\n schema: T\n }\n): CollectionConfig<InferSchemaOutput<T>, string | number, T> & {\n id?: string\n utils: ElectricCollectionUtils\n schema: T\n}\n\n// Overload for when no schema is provided\nexport function electricCollectionOptions<T extends Row<unknown>>(\n config: ElectricCollectionConfig<T> & {\n schema?: never // prohibit schema\n }\n): CollectionConfig<T, string | number> & {\n id?: string\n utils: ElectricCollectionUtils\n schema?: never // no schema in the result\n}\n\nexport function electricCollectionOptions(\n config: ElectricCollectionConfig<any, any>\n): CollectionConfig<any, string | number, any> & {\n id?: string\n utils: ElectricCollectionUtils\n schema?: any\n} {\n const seenTxids = new Store<Set<Txid>>(new Set([]))\n const seenSnapshots = new Store<Array<PostgresSnapshot>>([])\n const pendingMatches = new Store<\n Map<\n string,\n {\n matchFn: (message: Message<any>) => boolean\n resolve: (value: boolean) => void\n reject: (error: Error) => void\n timeoutId: ReturnType<typeof setTimeout>\n matched: boolean\n }\n >\n >(new Map())\n\n // Buffer messages since last up-to-date to handle race conditions\n const currentBatchMessages = new Store<Array<Message<any>>>([])\n\n /**\n * Helper function to remove multiple matches from the pendingMatches store\n */\n const removePendingMatches = (matchIds: Array<string>) => {\n if (matchIds.length > 0) {\n pendingMatches.setState((current) => {\n const newMatches = new Map(current)\n matchIds.forEach((id) => newMatches.delete(id))\n return newMatches\n })\n }\n }\n\n /**\n * Helper function to resolve and cleanup matched pending matches\n */\n const resolveMatchedPendingMatches = () => {\n const matchesToResolve: Array<string> = []\n pendingMatches.state.forEach((match, matchId) => {\n if (match.matched) {\n clearTimeout(match.timeoutId)\n match.resolve(true)\n matchesToResolve.push(matchId)\n debug(\n `${config.id ? `[${config.id}] ` : ``}awaitMatch resolved on up-to-date for match %s`,\n matchId\n )\n }\n })\n removePendingMatches(matchesToResolve)\n }\n const sync = createElectricSync<any>(config.shapeOptions, {\n seenTxids,\n seenSnapshots,\n pendingMatches,\n currentBatchMessages,\n removePendingMatches,\n resolveMatchedPendingMatches,\n collectionId: config.id,\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 5000ms)\n * @returns Promise that resolves when the txId is synced\n */\n const awaitTxId: AwaitTxIdFn = async (\n txId: Txid,\n timeout: number = 5000\n ): Promise<boolean> => {\n debug(\n `${config.id ? `[${config.id}] ` : ``}awaitTxId called with txid %d`,\n txId\n )\n if (typeof txId !== `number`) {\n throw new ExpectedNumberInAwaitTxIdError(typeof txId, config.id)\n }\n\n // First check if the txid is in the seenTxids store\n const hasTxid = seenTxids.state.has(txId)\n if (hasTxid) return true\n\n // Then check if the txid is in any of the seen snapshots\n const hasSnapshot = seenSnapshots.state.some((snapshot) =>\n isVisibleInSnapshot(txId, snapshot)\n )\n if (hasSnapshot) return true\n\n return new Promise((resolve, reject) => {\n const timeoutId = setTimeout(() => {\n unsubscribeSeenTxids()\n unsubscribeSeenSnapshots()\n reject(new TimeoutWaitingForTxIdError(txId, config.id))\n }, timeout)\n\n const unsubscribeSeenTxids = seenTxids.subscribe(() => {\n if (seenTxids.state.has(txId)) {\n debug(\n `${config.id ? `[${config.id}] ` : ``}awaitTxId found match for txid %o`,\n txId\n )\n clearTimeout(timeoutId)\n unsubscribeSeenTxids()\n unsubscribeSeenSnapshots()\n resolve(true)\n }\n })\n\n const unsubscribeSeenSnapshots = seenSnapshots.subscribe(() => {\n const visibleSnapshot = seenSnapshots.state.find((snapshot) =>\n isVisibleInSnapshot(txId, snapshot)\n )\n if (visibleSnapshot) {\n debug(\n `${config.id ? `[${config.id}] ` : ``}awaitTxId found match for txid %o in snapshot %o`,\n txId,\n visibleSnapshot\n )\n clearTimeout(timeoutId)\n unsubscribeSeenSnapshots()\n unsubscribeSeenTxids()\n resolve(true)\n }\n })\n })\n }\n\n /**\n * Wait for a custom match function to find a matching message\n * @param matchFn Function that returns true when a message matches\n * @param timeout Optional timeout in milliseconds (defaults to 5000ms)\n * @returns Promise that resolves when a matching message is found\n */\n const awaitMatch: AwaitMatchFn<any> = async (\n matchFn: MatchFunction<any>,\n timeout: number = 3000\n ): Promise<boolean> => {\n debug(\n `${config.id ? `[${config.id}] ` : ``}awaitMatch called with custom function`\n )\n\n return new Promise((resolve, reject) => {\n const matchId = Math.random().toString(36)\n\n const cleanupMatch = () => {\n pendingMatches.setState((current) => {\n const newMatches = new Map(current)\n newMatches.delete(matchId)\n return newMatches\n })\n }\n\n const onTimeout = () => {\n cleanupMatch()\n reject(new TimeoutWaitingForMatchError(config.id))\n }\n\n const timeoutId = setTimeout(onTimeout, timeout)\n\n // We need access to the stream messages to check against the match function\n // This will be handled by the sync configuration\n const checkMatch = (message: Message<any>) => {\n if (matchFn(message)) {\n debug(\n `${config.id ? `[${config.id}] ` : ``}awaitMatch found matching message, waiting for up-to-date`\n )\n // Mark as matched but don't resolve yet - wait for up-to-date\n pendingMatches.setState((current) => {\n const newMatches = new Map(current)\n const existing = newMatches.get(matchId)\n if (existing) {\n newMatches.set(matchId, { ...existing, matched: true })\n }\n return newMatches\n })\n return true\n }\n return false\n }\n\n // Check against current batch messages first to handle race conditions\n for (const message of currentBatchMessages.state) {\n if (matchFn(message)) {\n debug(\n `${config.id ? `[${config.id}] ` : ``}awaitMatch found immediate match in current batch, waiting for up-to-date`\n )\n // Register match as already matched\n pendingMatches.setState((current) => {\n const newMatches = new Map(current)\n newMatches.set(matchId, {\n matchFn: checkMatch,\n resolve,\n reject,\n timeoutId,\n matched: true, // Already matched\n })\n return newMatches\n })\n return\n }\n }\n\n // Store the match function for the sync process to use\n // We'll add this to a pending matches store\n pendingMatches.setState((current) => {\n const newMatches = new Map(current)\n newMatches.set(matchId, {\n matchFn: checkMatch,\n resolve,\n reject,\n timeoutId,\n matched: false,\n })\n return newMatches\n })\n })\n }\n\n /**\n * Process matching strategy and wait for synchronization\n */\n const processMatchingStrategy = async (\n result: MatchingStrategy\n ): Promise<void> => {\n // Only wait if result contains txid\n if (result && `txid` in result) {\n // Handle both single txid and array of txids\n if (Array.isArray(result.txid)) {\n await Promise.all(result.txid.map(awaitTxId))\n } else {\n await awaitTxId(result.txid)\n }\n }\n // If result is void/undefined, don't wait - mutation completes immediately\n }\n\n // Create wrapper handlers for direct persistence operations that handle different matching strategies\n const wrappedOnInsert = config.onInsert\n ? async (params: InsertMutationFnParams<any>) => {\n const handlerResult = await config.onInsert!(params)\n await processMatchingStrategy(handlerResult)\n return handlerResult\n }\n : undefined\n\n const wrappedOnUpdate = config.onUpdate\n ? async (params: UpdateMutationFnParams<any>) => {\n const handlerResult = await config.onUpdate!(params)\n await processMatchingStrategy(handlerResult)\n return handlerResult\n }\n : undefined\n\n const wrappedOnDelete = config.onDelete\n ? async (params: DeleteMutationFnParams<any>) => {\n const handlerResult = await config.onDelete!(params)\n await processMatchingStrategy(handlerResult)\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 awaitMatch,\n } as ElectricCollectionUtils<any>,\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 seenSnapshots: Store<Array<PostgresSnapshot>>\n pendingMatches: Store<\n Map<\n string,\n {\n matchFn: (message: Message<T>) => boolean\n resolve: (value: boolean) => void\n reject: (error: Error) => void\n timeoutId: ReturnType<typeof setTimeout>\n matched: boolean\n }\n >\n >\n currentBatchMessages: Store<Array<Message<T>>>\n removePendingMatches: (matchIds: Array<string>) => void\n resolveMatchedPendingMatches: () => void\n collectionId?: string\n }\n): SyncConfig<T> {\n const {\n seenTxids,\n seenSnapshots,\n pendingMatches,\n currentBatchMessages,\n removePendingMatches,\n resolveMatchedPendingMatches,\n collectionId,\n } = options\n const MAX_BATCH_MESSAGES = 1000 // Safety limit for message buffer\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 let unsubscribeStream: () => void\n\n return {\n sync: (params: Parameters<SyncConfig<T>[`sync`]>[0]) => {\n const { begin, write, commit, markReady, truncate, collection } = params\n\n // Abort controller for the stream - wraps the signal if provided\n const abortController = new AbortController()\n\n if (shapeOptions.signal) {\n shapeOptions.signal.addEventListener(\n `abort`,\n () => {\n abortController.abort()\n },\n {\n once: true,\n }\n )\n if (shapeOptions.signal.aborted) {\n abortController.abort()\n }\n }\n\n // Cleanup pending matches on abort\n abortController.signal.addEventListener(`abort`, () => {\n pendingMatches.setState((current) => {\n current.forEach((match) => {\n clearTimeout(match.timeoutId)\n match.reject(new StreamAbortedError())\n })\n return new Map() // Clear all pending matches\n })\n })\n\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 // Note that Electric sends a 409 error on a `must-refetch` message, but the\n // ShapeStream handled this and it will not reach this handler, therefor\n // this markReady will not be triggers by a `must-refetch`.\n markReady()\n\n if (shapeOptions.onError) {\n return shapeOptions.onError(errorParams)\n } else {\n console.error(\n `An error occurred while syncing collection: ${collection.id}, \\n` +\n `it has been marked as ready to avoid blocking apps waiting for '.preload()' to finish. \\n` +\n `You can provide an 'onError' handler on the shapeOptions to handle this error, and this message will not be logged.`,\n errorParams\n )\n }\n\n return\n },\n })\n let transactionStarted = false\n const newTxids = new Set<Txid>()\n const newSnapshots: Array<PostgresSnapshot> = []\n\n unsubscribeStream = stream.subscribe((messages: Array<Message<T>>) => {\n let hasUpToDate = false\n\n for (const message of messages) {\n // Add message to current batch buffer (for race condition handling)\n if (isChangeMessage(message)) {\n currentBatchMessages.setState((currentBuffer) => {\n const newBuffer = [...currentBuffer, message]\n // Limit buffer size for safety\n if (newBuffer.length > MAX_BATCH_MESSAGES) {\n newBuffer.splice(0, newBuffer.length - MAX_BATCH_MESSAGES)\n }\n return newBuffer\n })\n }\n\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 // Check pending matches against this message\n // Note: matchFn will mark matches internally, we don't resolve here\n const matchesToRemove: Array<string> = []\n pendingMatches.state.forEach((match, matchId) => {\n if (!match.matched) {\n try {\n match.matchFn(message)\n } catch (err) {\n // If matchFn throws, clean up and reject the promise\n clearTimeout(match.timeoutId)\n match.reject(\n err instanceof Error ? err : new Error(String(err))\n )\n matchesToRemove.push(matchId)\n debug(`matchFn error: %o`, err)\n }\n }\n })\n\n // Remove matches that errored\n removePendingMatches(matchesToRemove)\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 (isSnapshotEndMessage(message)) {\n newSnapshots.push(parseSnapshotMessage(message))\n } else if (isUpToDateMessage(message)) {\n hasUpToDate = true\n } else if (isMustRefetchMessage(message)) {\n debug(\n `${collectionId ? `[${collectionId}] ` : ``}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 // Reset hasUpToDate so we continue accumulating changes until next up-to-date\n hasUpToDate = false\n }\n }\n\n if (hasUpToDate) {\n // Clear the current batch buffer since we're now up-to-date\n currentBatchMessages.setState(() => [])\n\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(\n `${collectionId ? `[${collectionId}] ` : ``}new txids synced from pg %O`,\n Array.from(newTxids)\n )\n }\n newTxids.forEach((txid) => clonedSeen.add(txid))\n newTxids.clear()\n return clonedSeen\n })\n\n // Always commit snapshots when we receive up-to-date, regardless of transaction state\n seenSnapshots.setState((currentSnapshots) => {\n const seen = [...currentSnapshots, ...newSnapshots]\n newSnapshots.forEach((snapshot) =>\n debug(\n `${collectionId ? `[${collectionId}] ` : ``}new snapshot synced from pg %o`,\n snapshot\n )\n )\n newSnapshots.length = 0\n return seen\n })\n\n // Resolve all matched pending matches on up-to-date\n resolveMatchedPendingMatches()\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":";;;;;AAoCA,MAAM,QAAQ,YAAY,MAAM,gBAAgB;AAuJhD,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;AAEA,SAAS,qBACP,SAC+B;AAC/B,SAAO,iBAAiB,OAAO,KAAK,QAAQ,QAAQ,YAAY;AAClE;AAEA,SAAS,qBAAqB,SAA+C;AAC3E,SAAO;AAAA,IACL,MAAM,QAAQ,QAAQ;AAAA,IACtB,MAAM,QAAQ,QAAQ;AAAA,IACtB,UAAU,QAAQ,QAAQ;AAAA,EAAA;AAE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TxIdError extends ElectricDBCollection
|
|
|
8
8
|
export declare class TimeoutWaitingForTxIdError extends ElectricDBCollectionError {
|
|
9
9
|
constructor(txId: number, collectionId?: string);
|
|
10
10
|
}
|
|
11
|
-
export declare class
|
|
11
|
+
export declare class TimeoutWaitingForMatchError extends ElectricDBCollectionError {
|
|
12
12
|
constructor(collectionId?: string);
|
|
13
13
|
}
|
|
14
|
-
export declare class
|
|
15
|
-
constructor(collectionId?: string);
|
|
16
|
-
}
|
|
17
|
-
export declare class ElectricDeleteHandlerMustReturnTxIdError extends ElectricDBCollectionError {
|
|
14
|
+
export declare class StreamAbortedError extends ElectricDBCollectionError {
|
|
18
15
|
constructor(collectionId?: string);
|
|
19
16
|
}
|
package/dist/esm/errors.js
CHANGED
|
@@ -17,39 +17,23 @@ class TimeoutWaitingForTxIdError extends ElectricDBCollectionError {
|
|
|
17
17
|
this.name = `TimeoutWaitingForTxIdError`;
|
|
18
18
|
}
|
|
19
19
|
}
|
|
20
|
-
class
|
|
20
|
+
class TimeoutWaitingForMatchError extends ElectricDBCollectionError {
|
|
21
21
|
constructor(collectionId) {
|
|
22
|
-
super(
|
|
23
|
-
|
|
24
|
-
collectionId
|
|
25
|
-
);
|
|
26
|
-
this.name = `ElectricInsertHandlerMustReturnTxIdError`;
|
|
22
|
+
super(`Timeout waiting for custom match function`, collectionId);
|
|
23
|
+
this.name = `TimeoutWaitingForMatchError`;
|
|
27
24
|
}
|
|
28
25
|
}
|
|
29
|
-
class
|
|
26
|
+
class StreamAbortedError extends ElectricDBCollectionError {
|
|
30
27
|
constructor(collectionId) {
|
|
31
|
-
super(
|
|
32
|
-
|
|
33
|
-
collectionId
|
|
34
|
-
);
|
|
35
|
-
this.name = `ElectricUpdateHandlerMustReturnTxIdError`;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
class ElectricDeleteHandlerMustReturnTxIdError extends ElectricDBCollectionError {
|
|
39
|
-
constructor(collectionId) {
|
|
40
|
-
super(
|
|
41
|
-
`Electric collection onDelete handler must return a txid or array of txids`,
|
|
42
|
-
collectionId
|
|
43
|
-
);
|
|
44
|
-
this.name = `ElectricDeleteHandlerMustReturnTxIdError`;
|
|
28
|
+
super(`Stream aborted`, collectionId);
|
|
29
|
+
this.name = `StreamAbortedError`;
|
|
45
30
|
}
|
|
46
31
|
}
|
|
47
32
|
export {
|
|
48
33
|
ElectricDBCollectionError,
|
|
49
|
-
ElectricDeleteHandlerMustReturnTxIdError,
|
|
50
|
-
ElectricInsertHandlerMustReturnTxIdError,
|
|
51
|
-
ElectricUpdateHandlerMustReturnTxIdError,
|
|
52
34
|
ExpectedNumberInAwaitTxIdError,
|
|
35
|
+
StreamAbortedError,
|
|
36
|
+
TimeoutWaitingForMatchError,
|
|
53
37
|
TimeoutWaitingForTxIdError
|
|
54
38
|
};
|
|
55
39
|
//# sourceMappingURL=errors.js.map
|