@electric-sql/client 1.0.9 → 1.0.11

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/index.d.ts CHANGED
@@ -6,17 +6,30 @@ type Value<Extensions = never> = string | number | boolean | bigint | null | Ext
6
6
  [key: string]: Value<Extensions>;
7
7
  };
8
8
  type Row<Extensions = never> = Record<string, Value<Extensions>>;
9
- type GetExtensions<T extends Row<unknown>> = T extends Row<infer Extensions> ? Extensions : never;
10
- type Offset = `-1` | `${number}_${number}` | `${bigint}_${number}`;
9
+ type GetExtensions<T> = [T] extends [Row<never>] ? never : [T] extends [Row<infer E>] ? E : never;
10
+ type Offset = `-1` | `now` | `${number}_${number}` | `${bigint}_${number}`;
11
+ /** Information about transaction visibility for a snapshot. All fields are encoded as strings, but should be treated as uint64. */
12
+ type PostgresSnapshot = {
13
+ xmin: `${bigint}`;
14
+ xmax: `${bigint}`;
15
+ xip_list: `${bigint}`[];
16
+ };
17
+ type NormalizedPgSnapshot = {
18
+ xmin: bigint;
19
+ xmax: bigint;
20
+ xip_list: bigint[];
21
+ };
11
22
  interface Header {
12
23
  [key: Exclude<string, `operation` | `control`>]: Value;
13
24
  }
14
25
  type Operation = `insert` | `update` | `delete`;
15
26
  type ControlMessage = {
16
- headers: Header & {
27
+ headers: (Header & {
17
28
  control: `up-to-date` | `must-refetch`;
18
29
  global_last_seen_lsn?: string;
19
- };
30
+ }) | (Header & {
31
+ control: `snapshot-end`;
32
+ } & PostgresSnapshot);
20
33
  };
21
34
  type ChangeMessage<T extends Row<unknown> = Row> = {
22
35
  key: string;
@@ -24,6 +37,7 @@ type ChangeMessage<T extends Row<unknown> = Row> = {
24
37
  old_value?: Partial<T>;
25
38
  headers: Header & {
26
39
  operation: Operation;
40
+ txids?: number[];
27
41
  };
28
42
  };
29
43
  type Message<T extends Row<unknown> = Row> = ControlMessage | ChangeMessage<T>;
@@ -78,6 +92,20 @@ type TypedMessages<T extends Row<unknown> = Row> = {
78
92
  schema: ColumnInfo;
79
93
  };
80
94
  type MaybePromise<T> = T | Promise<T>;
95
+ /**
96
+ * Metadata that allows the consumer to know which changes have been incorporated into this snapshot.
97
+ *
98
+ * For any data that has a known transaction ID `xid` (and e.g. a key that's part of the snapshot):
99
+ * - if `xid` < `xmin` - included, change can be skipped
100
+ * - if `xid` < `xmax` AND `xid` not in `xip` - included, change can be skipped
101
+ * - if `xid` < `xmax` AND `xid` in `xip` - parallel, not included, change must be processed
102
+ * - if `xid` >= `xmax` - not included, change must be processed, and we can stop filtering after we see this
103
+ */
104
+ type SnapshotMetadata = {
105
+ /** Random number that's reflected in the `snapshot_mark` header on the snapshot items. */
106
+ snapshot_mark: number;
107
+ database_lsn: string;
108
+ } & PostgresSnapshot;
81
109
 
82
110
  type Token = string;
83
111
  type ParseFunction<Extensions = never> = (value: Token, additionalInfo?: Omit<ColumnInfo, `type` | `dims`>) => Value<Extensions>;
@@ -88,6 +116,7 @@ type ParseFunction<Extensions = never> = (value: Token, additionalInfo?: Omit<Co
88
116
  type Parser<Extensions = never> = {
89
117
  [key: string]: ParseFunction<Extensions>;
90
118
  };
119
+ type TransformFunction<Extensions = never> = (message: Row<Extensions>) => Row<Extensions>;
91
120
 
92
121
  declare class FetchError extends Error {
93
122
  url: string;
@@ -125,6 +154,7 @@ declare const OFFSET_QUERY_PARAM = "offset";
125
154
  declare const ELECTRIC_PROTOCOL_QUERY_PARAMS: Array<string>;
126
155
 
127
156
  type Replica = `full` | `default`;
157
+ type LogMode = `changes_only` | `full`;
128
158
  /**
129
159
  * PostgreSQL-specific shape parameters that can be provided externally
130
160
  */
@@ -174,7 +204,14 @@ type ExternalParamsRecord<T extends Row<unknown> = Row> = {
174
204
  } & Partial<PostgresParams<T>> & {
175
205
  [K in ReservedParamKeys]?: never;
176
206
  };
177
- type ReservedParamKeys = typeof LIVE_CACHE_BUSTER_QUERY_PARAM | typeof SHAPE_HANDLE_QUERY_PARAM | typeof LIVE_QUERY_PARAM | typeof OFFSET_QUERY_PARAM;
207
+ type SubsetParams = {
208
+ where?: string;
209
+ params?: Record<string, string>;
210
+ limit?: number;
211
+ offset?: number;
212
+ orderBy?: string;
213
+ };
214
+ type ReservedParamKeys = typeof LIVE_CACHE_BUSTER_QUERY_PARAM | typeof SHAPE_HANDLE_QUERY_PARAM | typeof LIVE_QUERY_PARAM | typeof OFFSET_QUERY_PARAM | `subset__${string}`;
178
215
  /**
179
216
  * External headers type - what users provide.
180
217
  * Allows string or function values for any header.
@@ -244,10 +281,15 @@ interface ShapeStreamOptions<T = never> {
244
281
  * Experimental support for Server-Sent Events (SSE) for live updates.
245
282
  */
246
283
  experimentalLiveSse?: boolean;
284
+ /**
285
+ * Initial data loading mode
286
+ */
287
+ mode?: LogMode;
247
288
  signal?: AbortSignal;
248
289
  fetchClient?: typeof fetch;
249
290
  backoffOptions?: BackoffOptions;
250
291
  parser?: Parser<T>;
292
+ transformer?: TransformFunction<T>;
251
293
  /**
252
294
  * A function for handling shapestream errors.
253
295
  * This is optional, when it is not provided any shapestream errors will be thrown.
@@ -271,7 +313,18 @@ interface ShapeStreamInterface<T extends Row<unknown> = Row> {
271
313
  lastOffset: Offset;
272
314
  shapeHandle?: string;
273
315
  error?: unknown;
316
+ mode: LogMode;
274
317
  forceDisconnectAndRefresh(): Promise<void>;
318
+ requestSnapshot(params: {
319
+ where?: string;
320
+ params?: Record<string, string>;
321
+ limit: number;
322
+ offset?: number;
323
+ orderBy: string;
324
+ }): Promise<{
325
+ metadata: SnapshotMetadata;
326
+ data: Array<Message<T>>;
327
+ }>;
275
328
  }
276
329
  /**
277
330
  * Reads updates to a shape from Electric using HTTP requests and long polling or
@@ -324,6 +377,7 @@ declare class ShapeStream<T extends Row<unknown> = Row> implements ShapeStreamIn
324
377
  get error(): unknown;
325
378
  get isUpToDate(): boolean;
326
379
  get lastOffset(): Offset;
380
+ get mode(): LogMode;
327
381
  subscribe(callback: (messages: Message<T>[]) => MaybePromise<void>, onError?: (error: Error) => void): () => void;
328
382
  unsubscribeAll(): void;
329
383
  /** Unix time at which we last synced. Undefined when `isLoading` is true. */
@@ -343,6 +397,24 @@ declare class ShapeStream<T extends Row<unknown> = Row> implements ShapeStreamIn
343
397
  * latest LSN from Postgres at that point in time.
344
398
  */
345
399
  forceDisconnectAndRefresh(): Promise<void>;
400
+ /**
401
+ * Request a snapshot for subset of data.
402
+ *
403
+ * Only available when mode is `changes_only`.
404
+ * Returns the insertion point & the data, but more importantly injects the data
405
+ * into the subscribed data stream. Returned value is unlikely to be useful for the caller,
406
+ * unless the caller has complicated additional logic.
407
+ *
408
+ * Data will be injected in a way that's also tracking further incoming changes, and it'll
409
+ * skip the ones that are already in the snapshot.
410
+ *
411
+ * @param opts - The options for the snapshot request.
412
+ * @returns The metadata and the data for the snapshot.
413
+ */
414
+ requestSnapshot(opts: SubsetParams): Promise<{
415
+ metadata: SnapshotMetadata;
416
+ data: Array<ChangeMessage<T>>;
417
+ }>;
346
418
  }
347
419
 
348
420
  type ShapeData<T extends Row<unknown> = Row> = Map<string, T>;
@@ -406,6 +478,13 @@ declare class Shape<T extends Row<unknown> = Row> {
406
478
  isLoading(): boolean;
407
479
  /** Indicates if we are connected to the Electric sync service. */
408
480
  isConnected(): boolean;
481
+ /** Current log mode of the underlying stream */
482
+ get mode(): LogMode;
483
+ /**
484
+ * Request a snapshot for subset of data. Only available when mode is changes_only.
485
+ * Returns void; data will be emitted via the stream and processed by this Shape.
486
+ */
487
+ requestSnapshot(params: Parameters<ShapeStreamInterface<T>[`requestSnapshot`]>[0]): Promise<void>;
409
488
  subscribe(callback: ShapeChangedCallback<T>): () => void;
410
489
  unsubscribeAll(): void;
411
490
  get numSubscribers(): number;
@@ -447,5 +526,13 @@ declare function isChangeMessage<T extends Row<unknown> = Row>(message: Message<
447
526
  * ```
448
527
  */
449
528
  declare function isControlMessage<T extends Row<unknown> = Row>(message: Message<T>): message is ControlMessage;
529
+ /**
530
+ * Checks if a transaction is visible in a snapshot.
531
+ *
532
+ * @param txid - the transaction id to check
533
+ * @param snapshot - the information about the snapshot
534
+ * @returns true if the transaction is visible in the snapshot
535
+ */
536
+ declare function isVisibleInSnapshot(txid: number | bigint | `${bigint}`, snapshot: PostgresSnapshot | NormalizedPgSnapshot): boolean;
450
537
 
451
- export { BackoffDefaults, type BackoffOptions, type BitColumn, type BpcharColumn, type ChangeMessage, type ColumnInfo, type CommonColumnProps, type ControlMessage, ELECTRIC_PROTOCOL_QUERY_PARAMS, type ExternalHeadersRecord, type ExternalParamsRecord, FetchError, type GetExtensions, type IntervalColumn, type IntervalColumnWithPrecision, type MaybePromise, type Message, type NumericColumn, type Offset, type Operation, type PostgresParams, type RegularColumn, type Row, type Schema, Shape, type ShapeChangedCallback, type ShapeData, ShapeStream, type ShapeStreamInterface, type ShapeStreamOptions, type TimeColumn, type TypedMessages, type Value, type VarcharColumn, isChangeMessage, isControlMessage, resolveValue };
538
+ export { BackoffDefaults, type BackoffOptions, type BitColumn, type BpcharColumn, type ChangeMessage, type ColumnInfo, type CommonColumnProps, type ControlMessage, ELECTRIC_PROTOCOL_QUERY_PARAMS, type ExternalHeadersRecord, type ExternalParamsRecord, FetchError, type GetExtensions, type IntervalColumn, type IntervalColumnWithPrecision, type LogMode, type MaybePromise, type Message, type NormalizedPgSnapshot, type NumericColumn, type Offset, type Operation, type PostgresParams, type PostgresSnapshot, type RegularColumn, type Row, type Schema, Shape, type ShapeChangedCallback, type ShapeData, ShapeStream, type ShapeStreamInterface, type ShapeStreamOptions, type SnapshotMetadata, type SubsetParams, type TimeColumn, type TypedMessages, type Value, type VarcharColumn, isChangeMessage, isControlMessage, isVisibleInSnapshot, resolveValue };