Skip to content

Commit a302f66

Browse files
authored
feat(client): add types for move messages (#3497)
1 parent 93bdb37 commit a302f66

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

.changeset/old-moles-thank.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@electric-sql/client': patch
3+
'@core/sync-service': patch
4+
---
5+
6+
feat: add support for subqueries without invalidation

packages/typescript-client/src/types.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,30 @@ export type NormalizedPgSnapshot = {
4343
}
4444

4545
interface Header {
46-
[key: Exclude<string, `operation` | `control`>]: Value
46+
[key: Exclude<string, `operation` | `control` | `event`>]: Value
4747
}
4848

4949
export type Operation = `insert` | `update` | `delete`
50+
/**
51+
* A tag is a string identifying a reason for this row to be part of the shape.
52+
*
53+
* Tags can be composite, but they are always sent as a single string. Compound tags
54+
* are separated by `|`. It's up to the client to split the tag into its components
55+
* in order to react to move-outs correctly. Tag parts are guaranteed to not contain an
56+
* unescaped `|` character (escaped as `\\|`) or be a literal `*`.
57+
*
58+
* Composite tag width is guaranteed to be fixed for a given shape.
59+
*/
60+
export type MoveTag = string
61+
62+
/**
63+
* A move-out pattern is a position and a value. The position is the index of the column
64+
* that is being moved out. The value is the value of the column that is being moved out.
65+
*
66+
* Tag width and value order is fixed for a given shape, so the client can determine
67+
* which tags match this pattern.
68+
*/
69+
export type MoveOutPattern = { pos: number; value: string }
5070

5171
export type ControlMessage = {
5272
headers:
@@ -57,16 +77,27 @@ export type ControlMessage = {
5777
| (Header & { control: `snapshot-end` } & PostgresSnapshot)
5878
}
5979

80+
export type EventMessage = {
81+
headers: Header & { event: `move-out`; patterns: MoveOutPattern[] }
82+
}
83+
6084
export type ChangeMessage<T extends Row<unknown> = Row> = {
6185
key: string
6286
value: T
6387
old_value?: Partial<T> // Only provided for updates if `replica` is `full`
64-
headers: Header & { operation: Operation; txids?: number[] }
88+
headers: Header & {
89+
operation: Operation
90+
txids?: number[]
91+
/** Tags will always be present for changes if the shape has a subquery in its where clause, and are omitted otherwise.*/
92+
tags?: MoveTag[]
93+
removed_tags?: MoveTag[]
94+
}
6595
}
6696

6797
// Define the type for a record
6898
export type Message<T extends Row<unknown> = Row> =
6999
| ControlMessage
100+
| EventMessage
70101
| ChangeMessage<T>
71102

72103
/**

0 commit comments

Comments
 (0)