Open History to see the last commit
1import {
2 Shape,
3 ShapeStream,
4 FetchError,
5 FetchBackoffAbortError,
6 isChangeMessage,
7 isControlMessage,
8 createColumnMapper,
9 snakeCamelMapper,
10 compileExpression,
11 compileOrderBy,
12 ELECTRIC_PROTOCOL_QUERY_PARAMS,
13 type Row,
14 type Message,
15 type ShapeData,
16 type ShapeStreamInterface,
17 type ShapeStreamOptions,
18 type ExternalParamsRecord,
19 type SnapshotMetadata,
20 type Offset,
21} from '@electric-sql/client'
22
23type Equal<A, B> =
24 (<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2
25 ? true
26 : false
27type Assert<T extends true> = T
28type Task = { id: number; title: string; createdAt: Date }
29
30const options: ShapeStreamOptions<Date> = {
31 url: 'https://example.test/api/shape?tenant=one',
32 params: { table: 'tasks', columns: ['id', 'title', 'created_at'] },
33 parser: { timestamptz: (value) => new Date(value) },
34 columnMapper: snakeCamelMapper(),
35 fetchClient: fetch,
36 subscribe: false,
37 onError: async () => ({ headers: { authorization: 'refreshed' } }),
38}
39const stream = new ShapeStream<Task>(options)
40const shape = new Shape(stream)
41const structural: ShapeStreamInterface<Task> = stream
42const originalOptions: ShapeStreamOptions<Date> = stream.options
43const subscribers: number = shape.numSubscribers
44const paused: boolean = stream.isPaused()
45const offset: Offset = stream.lastOffset
46
47type RowsAreInferred = Assert<Equal<typeof shape.rows, Promise<Task[]>>>
48type ValuesAreInferred = Assert<
49 Equal<typeof shape.value, Promise<ShapeData<Task>>>
50>
51const defaultStream = new ShapeStream({ url: options.url })
52type DefaultRowsArePreserved = Assert<
53 Equal<typeof defaultStream, ShapeStream<Row>>
54>
55
56stream.subscribe(async (messages) => {
57 type CallbackRowsAreInferred = Assert<Equal<typeof messages, Message<Task>[]>>
58 for (const message of messages) {
59 if (isChangeMessage(message)) {
60 const date: Date = message.value.createdAt
61 // @ts-expect-error a custom parsed date must not become any or string
62 const invalid: string = message.value.createdAt
63 void [date, invalid]
64 } else if (isControlMessage(message)) {
65 const control: string = message.headers.control
66 void control
67 }
68 }
69})
70shape.subscribe(({ rows, value }) => {
71 const taskRows: Task[] = rows
72 const taskMap: Map<string, Task> = value
73 void [taskRows, taskMap]
74})
75
76const dynamicParams: ExternalParamsRecord = {
77 table: 'tasks',
78 tenant: async () => 'one',
79}
80const reserved: ExternalParamsRecord = {
81 // @ts-expect-error transport parameters are reserved
82 handle: 'not-an-application-param',
83}
84// @ts-expect-error offsets retain their numeric-pair / sentinel contract
85const invalidOffset: Offset = 'arbitrary-opaque-text'
86
87async function subsets() {
88 const { metadata, data } = await stream.fetchSnapshot({
89 whereExpr: {
90 type: 'func',
91 name: 'eq',
92 args: [
93 { type: 'ref', column: 'id' },
94 { type: 'val', paramIndex: 1 },
95 ],
96 },
97 params: { '1': 1 },
98 limit: 10,
99 method: 'POST',
100 })
101 const snapshot: SnapshotMetadata = metadata
102 const changes: Message<Task>[] = data
103 await stream.requestSnapshot({ where: 'id = $1', params: { '1': 1 } })
104 return [snapshot, changes]
105}
106
107void [
108 structural,
109 originalOptions,
110 subscribers,
111 paused,
112 offset,
113 dynamicParams,
114 reserved,
115 invalidOffset,
116 subsets,
117 FetchError,
118 FetchBackoffAbortError,
119 createColumnMapper,
120 compileExpression,
121 compileOrderBy,
122 ELECTRIC_PROTOCOL_QUERY_PARAMS,
123]