@electric-sql/client 1.0.10 → 1.0.12

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
@@ -7,16 +7,29 @@ type Value<Extensions = never> = string | number | boolean | bigint | null | Ext
7
7
  };
8
8
  type Row<Extensions = never> = Record<string, Value<Extensions>>;
9
9
  type GetExtensions<T> = [T] extends [Row<never>] ? never : [T] extends [Row<infer E>] ? E : never;
10
- type Offset = `-1` | `${number}_${number}` | `${bigint}_${number}`;
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>;
@@ -126,6 +154,7 @@ declare const OFFSET_QUERY_PARAM = "offset";
126
154
  declare const ELECTRIC_PROTOCOL_QUERY_PARAMS: Array<string>;
127
155
 
128
156
  type Replica = `full` | `default`;
157
+ type LogMode = `changes_only` | `full`;
129
158
  /**
130
159
  * PostgreSQL-specific shape parameters that can be provided externally
131
160
  */
@@ -175,7 +204,14 @@ type ExternalParamsRecord<T extends Row<unknown> = Row> = {
175
204
  } & Partial<PostgresParams<T>> & {
176
205
  [K in ReservedParamKeys]?: never;
177
206
  };
178
- 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}`;
179
215
  /**
180
216
  * External headers type - what users provide.
181
217
  * Allows string or function values for any header.
@@ -245,6 +281,10 @@ interface ShapeStreamOptions<T = never> {
245
281
  * Experimental support for Server-Sent Events (SSE) for live updates.
246
282
  */
247
283
  experimentalLiveSse?: boolean;
284
+ /**
285
+ * Initial data loading mode
286
+ */
287
+ log?: LogMode;
248
288
  signal?: AbortSignal;
249
289
  fetchClient?: typeof fetch;
250
290
  backoffOptions?: BackoffOptions;
@@ -273,7 +313,18 @@ interface ShapeStreamInterface<T extends Row<unknown> = Row> {
273
313
  lastOffset: Offset;
274
314
  shapeHandle?: string;
275
315
  error?: unknown;
316
+ mode: LogMode;
276
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
+ }>;
277
328
  }
278
329
  /**
279
330
  * Reads updates to a shape from Electric using HTTP requests and long polling or
@@ -326,6 +377,7 @@ declare class ShapeStream<T extends Row<unknown> = Row> implements ShapeStreamIn
326
377
  get error(): unknown;
327
378
  get isUpToDate(): boolean;
328
379
  get lastOffset(): Offset;
380
+ get mode(): LogMode;
329
381
  subscribe(callback: (messages: Message<T>[]) => MaybePromise<void>, onError?: (error: Error) => void): () => void;
330
382
  unsubscribeAll(): void;
331
383
  /** Unix time at which we last synced. Undefined when `isLoading` is true. */
@@ -345,6 +397,24 @@ declare class ShapeStream<T extends Row<unknown> = Row> implements ShapeStreamIn
345
397
  * latest LSN from Postgres at that point in time.
346
398
  */
347
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
+ }>;
348
418
  }
349
419
 
350
420
  type ShapeData<T extends Row<unknown> = Row> = Map<string, T>;
@@ -408,6 +478,13 @@ declare class Shape<T extends Row<unknown> = Row> {
408
478
  isLoading(): boolean;
409
479
  /** Indicates if we are connected to the Electric sync service. */
410
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>;
411
488
  subscribe(callback: ShapeChangedCallback<T>): () => void;
412
489
  unsubscribeAll(): void;
413
490
  get numSubscribers(): number;
@@ -449,5 +526,13 @@ declare function isChangeMessage<T extends Row<unknown> = Row>(message: Message<
449
526
  * ```
450
527
  */
451
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;
452
537
 
453
- 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 };