Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

protocol.ts

209 lines5.7 KB

1import type { LoadSubsetOptions } from '@tanstack/db'
2import type {
3 CommittedTxAnchor,
4 PersistedCacheGenerationClaim,
5 PersistedCollectionMode,
6 PersistedIndexSpec,
7 PersistedKeySetEvidence,
8 PersistedStorageTarget,
9 PersistedTx,
10 ReconciledCommittedTx,
11 SQLitePullSinceResult,
12} from '@tanstack/db-sqlite-persistence-core'
13​
14export const ELECTRON_PERSISTENCE_PROTOCOL_VERSION = 6 as const
15export const DEFAULT_ELECTRON_PERSISTENCE_CHANNEL = `tanstack-db:sqlite-persistence`
16​
17export type ElectronPersistedRow = Record<string, unknown>
18export type ElectronPersistedKey = string | number
19​
20export type ElectronPersistenceResolution = {
21 mode: PersistedCollectionMode
22 schemaVersion?: number
23 logicalCollectionId?: string
25​
26export type ElectronPersistenceMethod =
27 | `loadSubset`
28 | `loadResumeSnapshot`
29 | `loadCollectionMetadata`
30 | `scanRows`
31 | `applyCommittedTx`
32 | `reconcileCommittedTx`
33 | `ensureIndex`
34 | `markIndexRemoved`
35 | `pullSince`
36 | `getStreamPosition`
37 | `claimCacheGeneration`
38 | `rotateCacheGeneration`
39 | `renewCacheGenerationClaim`
40 | `releaseCacheGenerationClaim`
41 | `reserveLeadershipTerm`
42​
43export type ElectronPersistencePayloadMap = {
44 loadSubset: {
45 options: LoadSubsetOptions
46 ctx?: {
47 requiredIndexSignatures?: ReadonlyArray<string>
48 cacheGenerationClaimId?: string
49 }
50 }
51 loadResumeSnapshot: {
52 ctx?: {
53 requiredIndexSignatures?: ReadonlyArray<string>
54 includeRows?: boolean
55 cacheGenerationClaimId?: string
56 }
57 }
58 loadCollectionMetadata: { ctx?: { cacheGenerationClaimId?: string } }
59 scanRows: {
60 options?: {
61 metadataOnly?: boolean
62 }
63 ctx?: { cacheGenerationClaimId?: string }
64 }
65 applyCommittedTx: {
66 tx: PersistedTx<ElectronPersistedRow, ElectronPersistedKey>
67 }
68 reconcileCommittedTx: {
69 tx: PersistedTx<ElectronPersistedRow, ElectronPersistedKey>
70 anchor: CommittedTxAnchor
71 }
72 ensureIndex: {
73 signature: string
74 spec: PersistedIndexSpec
75 ctx?: { cacheGenerationClaimId?: string }
76 }
77 markIndexRemoved: {
78 signature: string
79 ctx?: { cacheGenerationClaimId?: string }
80 }
81 pullSince: {
82 fromRowVersion: number
83 ctx?: { cacheGenerationClaimId?: string }
84 }
85 getStreamPosition: { ctx?: { cacheGenerationClaimId?: string } }
86 claimCacheGeneration: {}
87 rotateCacheGeneration: {
88 claimId: string
89 resetMetadata?: { key: string; value: unknown }
90 expectedStorageCollectionId?: string
91 }
92 renewCacheGenerationClaim: {
93 storageCollectionId: string
94 claimId: string
95 }
96 releaseCacheGenerationClaim: { claimId: string }
97 reserveLeadershipTerm: {
98 observedTerm: number
99 ctx?: { cacheGenerationClaimId?: string }
100 }
102​
103export type ElectronPersistenceResultMap = {
104 loadSubset: Array<{ key: ElectronPersistedKey; value: ElectronPersistedRow }>
105 loadResumeSnapshot: {
106 rows: Array<{
107 key: ElectronPersistedKey
108 value: ElectronPersistedRow
109 metadata?: unknown
110 }>
111 keySet?: PersistedKeySetEvidence
112 collectionMetadata: Array<{ key: string; value: unknown }>
113 latestTerm: number
114 latestSeq: number
115 latestRowVersion: number
116 resetEpoch: number
117 }
118 loadCollectionMetadata: Array<{ key: string; value: unknown }>
119 scanRows: Array<{
120 key: ElectronPersistedKey
121 value: ElectronPersistedRow
122 metadata?: unknown
123 }>
124 applyCommittedTx: null
125 reconcileCommittedTx: ReconciledCommittedTx
126 ensureIndex: null
127 markIndexRemoved: null
128 pullSince: SQLitePullSinceResult<ElectronPersistedKey>
129 getStreamPosition: {
130 latestTerm: number
131 latestSeq: number
132 latestRowVersion: number
133 }
134 claimCacheGeneration: PersistedCacheGenerationClaim
135 rotateCacheGeneration: PersistedCacheGenerationClaim
136 renewCacheGenerationClaim: number | undefined
137 releaseCacheGenerationClaim: null
138 reserveLeadershipTerm: {
139 latestTerm: number
140 latestSeq: number
141 latestRowVersion: number
142 }
144​
145export type ElectronSerializedError = {
146 name: string
147 message: string
148 stack?: string
149 code?: string | number
150 path?: string | ReadonlyArray<string | number>
152​
153export type ElectronPersistenceRequestByMethod = {
154 [Method in ElectronPersistenceMethod]: {
155 v: number
156 requestId: string
157 collectionId: string
158 /** Required for data operations; preserves eager or managed identity across IPC. */
159 storageTarget?: PersistedStorageTarget
160 resolution?: ElectronPersistenceResolution
161 method: Method
162 payload: ElectronPersistencePayloadMap[Method]
163 }
165​
166export type ElectronPersistenceRequest<
167 TMethod extends ElectronPersistenceMethod = ElectronPersistenceMethod,
168> = ElectronPersistenceRequestByMethod[TMethod]
169​
170export type ElectronPersistenceRequestEnvelope =
171 ElectronPersistenceRequestByMethod[ElectronPersistenceMethod]
172​
173type ElectronPersistenceSuccessResponseByMethod = {
174 [Method in ElectronPersistenceMethod]: {
175 v: number
176 requestId: string
177 method: Method
178 ok: true
179 result: ElectronPersistenceResultMap[Method]
180 }
182​
183type ElectronPersistenceErrorResponseByMethod = {
184 [Method in ElectronPersistenceMethod]: {
185 v: number
186 requestId: string
187 method: Method
188 ok: false
189 error: ElectronSerializedError
190 }
192​
193export type ElectronPersistenceResponse<
194 TMethod extends ElectronPersistenceMethod = ElectronPersistenceMethod,
195> =
196 | ElectronPersistenceSuccessResponseByMethod[TMethod]
197 | ElectronPersistenceErrorResponseByMethod[TMethod]
198​
199export type ElectronPersistenceResponseEnvelope =
200 ElectronPersistenceResponse<ElectronPersistenceMethod>
201​
202export type ElectronPersistenceRequestHandler = (
203 request: ElectronPersistenceRequestEnvelope,
204) => Promise<ElectronPersistenceResponseEnvelope>
205​
206export type ElectronPersistenceInvoke = (
207 channel: string,
208 request: ElectronPersistenceRequestEnvelope,
209) => Promise<ElectronPersistenceResponseEnvelope>