@sqlite-sync/core 0.0.1 → 0.0.3
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/chunk-627DSM2Q.js +1410 -0
- package/dist/chunk-627DSM2Q.js.map +1 -0
- package/dist/chunk-UGF5IU53.js +132 -0
- package/dist/chunk-UGF5IU53.js.map +1 -0
- package/dist/crdt-schema-DQ1cYsFE.d.ts +63 -0
- package/dist/crdt-sync-remote-source-idoIjMcs.d.ts +479 -0
- package/dist/index.d.ts +117 -207
- package/dist/index.js +614 -357
- package/dist/index.js.map +1 -1
- package/dist/server.d.ts +50 -26
- package/dist/server.js +28 -36
- package/dist/server.js.map +1 -1
- package/dist/worker.d.ts +13 -11
- package/dist/worker.js +275 -140
- package/dist/worker.js.map +1 -1
- package/package.json +6 -6
- package/dist/chunk-LK5FJCUD.js +0 -522
- package/dist/chunk-LK5FJCUD.js.map +0 -1
- package/dist/chunk-YLXMST5Z.js +0 -490
- package/dist/chunk-YLXMST5Z.js.map +0 -1
- package/dist/crdt-sync-producer-0toEpGf0.d.ts +0 -15
- package/dist/crdt-sync-remote-source-rrqinqLn.d.ts +0 -271
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
import { CompiledQuery, Compilable, Kysely, CreateTableBuilder, CreateIndexBuilder, ColumnDataType, Expression, ColumnDefinitionBuilderCallback } from 'kysely';
|
|
2
|
+
import { Sqlite3Static, Database, SqlValue, FunctionOptions } from '@sqlite.org/sqlite-wasm';
|
|
3
|
+
|
|
4
|
+
interface HLC {
|
|
5
|
+
timestamp: number;
|
|
6
|
+
counter: number;
|
|
7
|
+
nodeId: string;
|
|
8
|
+
}
|
|
9
|
+
declare class HLCCounter {
|
|
10
|
+
private timestamp;
|
|
11
|
+
private counter;
|
|
12
|
+
private nodeId;
|
|
13
|
+
private readonly getTimestamp;
|
|
14
|
+
private readonly maxDrift;
|
|
15
|
+
constructor(nodeId: string, getTimestamp: () => number, maxDrift?: number);
|
|
16
|
+
getCurrentHLC(): HLC;
|
|
17
|
+
getNextHLC(): HLC;
|
|
18
|
+
mergeHLC(hlc: HLC): void;
|
|
19
|
+
}
|
|
20
|
+
declare function serializeHLC(hlc: HLC): string;
|
|
21
|
+
declare function deserializeHLC(serialized: string): HLC;
|
|
22
|
+
declare function compareHLC(one: HLC, two: HLC): number;
|
|
23
|
+
|
|
24
|
+
type LogLevel = "info" | "warning" | "error" | "trace" | "system";
|
|
25
|
+
type Logger = (type: string, message: string, level?: LogLevel) => void;
|
|
26
|
+
declare const startPerformanceLogger: (logger: Logger) => {
|
|
27
|
+
restart: () => void;
|
|
28
|
+
logEnd: (type: string, message: string, level?: LogLevel) => void;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
type ExecuteParams = {
|
|
32
|
+
sql: string;
|
|
33
|
+
parameters: readonly unknown[];
|
|
34
|
+
};
|
|
35
|
+
type ExecuteResult<T> = {
|
|
36
|
+
rows: T[];
|
|
37
|
+
};
|
|
38
|
+
type PreparedStatement<TParams extends SqlValue[], TResult> = {
|
|
39
|
+
execute: (parameters: TParams) => TResult[];
|
|
40
|
+
finalize: () => void;
|
|
41
|
+
isFinalized: boolean;
|
|
42
|
+
};
|
|
43
|
+
type ScalarFunctionOptions<TArgs extends readonly SqlValue[], TResult extends SqlValue | undefined> = {
|
|
44
|
+
name: string;
|
|
45
|
+
callback: (...args: TArgs) => TResult;
|
|
46
|
+
} & Pick<FunctionOptions, "deterministic" | "directOnly" | "innocuous">;
|
|
47
|
+
type SqliteWrapperOptions = {
|
|
48
|
+
logger?: Logger;
|
|
49
|
+
loggerPrefix?: string;
|
|
50
|
+
sqlite3: Sqlite3Static;
|
|
51
|
+
db: () => Database;
|
|
52
|
+
};
|
|
53
|
+
type SQLiteTransactionWrapper<TDatabase = unknown> = Pick<SQLiteDbWrapper<TDatabase>, "execute" | "sql" | "executeKysely" | "prepare" | "executePrepared" | "prepareKysely">;
|
|
54
|
+
type QueryMetaOpts = {
|
|
55
|
+
loggerLevel?: "info" | "system";
|
|
56
|
+
};
|
|
57
|
+
declare class SQLiteDbWrapper<TDatabase = unknown> {
|
|
58
|
+
private db;
|
|
59
|
+
private sqlite3;
|
|
60
|
+
private logger?;
|
|
61
|
+
private loggerPrefix?;
|
|
62
|
+
private loadedDbSchema;
|
|
63
|
+
private readonly dataPointers;
|
|
64
|
+
private preparedStatements;
|
|
65
|
+
private preparedStatementsMap;
|
|
66
|
+
private preparedRawStatementsMap;
|
|
67
|
+
constructor(opts: SqliteWrapperOptions);
|
|
68
|
+
get ensureDb(): Database;
|
|
69
|
+
get dbSchema(): DatabaseIntrospection;
|
|
70
|
+
execute<T = unknown>(opts: ExecuteParams | string | CompiledQuery<T>, meta?: QueryMetaOpts): ExecuteResult<T>;
|
|
71
|
+
executeTransaction<T>(callback: (db: SQLiteTransactionWrapper<TDatabase>) => T): T;
|
|
72
|
+
isInTransaction(): boolean;
|
|
73
|
+
beginTransaction(): {
|
|
74
|
+
commit: () => void;
|
|
75
|
+
rollback: () => void;
|
|
76
|
+
};
|
|
77
|
+
prepare<TParams extends SqlValue[], TResult>(sql: string, opts?: QueryMetaOpts): PreparedStatement<TParams, TResult>;
|
|
78
|
+
prepareKysely<TParams extends Record<string, unknown>>(opts?: QueryMetaOpts): <TQuery extends Compilable<TResult>, TResult = QueryBuilderOutput<TQuery>>(factory: KyselyStatementFactory<TParams, TDatabase, TQuery, TResult>) => TypedStatement<TParams, TResult>;
|
|
79
|
+
executeKysely<TQuery extends Compilable<TResult>, TResult = QueryBuilderOutput<TQuery>>(factory: KyselyQueryFactory<TDatabase, TQuery, TResult>, meta?: QueryMetaOpts): ExecuteResult<TResult>;
|
|
80
|
+
executePrepared<TParams extends Record<string, unknown>, TQuery extends Compilable<TResult>, TResult = QueryBuilderOutput<TQuery>>(key: string, params: TParams, factory: KyselyStatementFactory<TParams, TDatabase, TQuery, TResult>, meta?: QueryMetaOpts): TResult[];
|
|
81
|
+
executePreparedRaw<TParams extends SqlValue[], TResult>({ key, sql, params, meta, }: {
|
|
82
|
+
key: string;
|
|
83
|
+
sql: string;
|
|
84
|
+
params?: TParams;
|
|
85
|
+
meta?: QueryMetaOpts;
|
|
86
|
+
}): TResult[];
|
|
87
|
+
sql<T = unknown>(templateOrString: TemplateStringsArray | string, ...parameters: unknown[]): ExecuteResult<T>;
|
|
88
|
+
createScalarFunction<TArgs extends SqlValue[], TResult extends SqlValue | undefined>({ name, callback, deterministic, directOnly, innocuous, }: ScalarFunctionOptions<TArgs, TResult>): Database;
|
|
89
|
+
useSnapshot(snapshot: Uint8Array<ArrayBufferLike>): void;
|
|
90
|
+
createSnapshot(): Uint8Array<ArrayBuffer>;
|
|
91
|
+
invalidateDbSchema(): void;
|
|
92
|
+
cleanup(): void;
|
|
93
|
+
close(): void;
|
|
94
|
+
}
|
|
95
|
+
type QueryBuilderOutput<QB> = QB extends Compilable<infer O> ? O : never;
|
|
96
|
+
type ParamsGetter<TParams> = <TKey extends keyof TParams>(key: TKey) => TParams[TKey];
|
|
97
|
+
type TypedStatement<TParams extends Record<string, unknown>, TResult> = {
|
|
98
|
+
execute: (parameters: TParams) => TResult[];
|
|
99
|
+
};
|
|
100
|
+
type KyselyStatementFactory<TParams extends Record<string, unknown>, TDatabase, TQuery extends Compilable<TResult>, TResult = QueryBuilderOutput<TQuery>> = (kysely: Kysely<TDatabase>, params: ParamsGetter<TParams>) => TQuery;
|
|
101
|
+
type KyselyQueryFactory<TDatabase, TQuery extends Compilable<TResult>, TResult = QueryBuilderOutput<TQuery>> = (kysely: Kysely<TDatabase>) => TQuery;
|
|
102
|
+
|
|
103
|
+
type TableMetadata = {
|
|
104
|
+
name: string;
|
|
105
|
+
isView: boolean;
|
|
106
|
+
columns: ColumnMetadata[];
|
|
107
|
+
};
|
|
108
|
+
type DatabaseIntrospection = Record<string, TableMetadata>;
|
|
109
|
+
type ColumnMetadata = {
|
|
110
|
+
name: string;
|
|
111
|
+
dataType: string;
|
|
112
|
+
isNullable: boolean;
|
|
113
|
+
isAutoIncrementing: boolean;
|
|
114
|
+
hasDefaultValue: boolean;
|
|
115
|
+
comment: undefined;
|
|
116
|
+
};
|
|
117
|
+
declare function introspectDb<BaseDatabase>(_db: SQLiteDbWrapper<BaseDatabase>): DatabaseIntrospection;
|
|
118
|
+
|
|
119
|
+
type DeferredPromise<T> = {
|
|
120
|
+
promise: Promise<T>;
|
|
121
|
+
resolve: (value: T) => void;
|
|
122
|
+
reject: (error: Error) => void;
|
|
123
|
+
};
|
|
124
|
+
declare function createDeferredPromise<T>(opts?: {
|
|
125
|
+
timeout?: number;
|
|
126
|
+
onTimeout?: () => void;
|
|
127
|
+
}): DeferredPromise<T>;
|
|
128
|
+
declare const generateId: () => `${string}-${string}-${string}-${string}-${string}`;
|
|
129
|
+
type DistributiveOmit<T, K extends keyof T> = T extends any ? Omit<T, K> : never;
|
|
130
|
+
declare class TypedBroadcastChannel<TMessage> {
|
|
131
|
+
private readonly channel;
|
|
132
|
+
constructor(name: string);
|
|
133
|
+
postMessage(message: TMessage): void;
|
|
134
|
+
set onmessage(callback: ((event: MessageEvent<TMessage>) => void) | null);
|
|
135
|
+
close(): void;
|
|
136
|
+
}
|
|
137
|
+
declare class TypedEvent<T = unknown> extends Event {
|
|
138
|
+
readonly payload: T;
|
|
139
|
+
constructor(type: string, payload: T);
|
|
140
|
+
}
|
|
141
|
+
type TypedEventTarget<T extends Record<string, unknown>> = {
|
|
142
|
+
addEventListener: <K extends keyof T & string>(type: K, listener: (event: TypedEvent<T[K]>) => void) => void;
|
|
143
|
+
removeEventListener: <K extends keyof T & string>(type: K, listener: (event: TypedEvent<T[K]>) => void) => void;
|
|
144
|
+
dispatchEvent: <K extends keyof T & string>(type: K, payload: T[K]) => void;
|
|
145
|
+
};
|
|
146
|
+
declare const createTypedEventTarget: <T extends Record<string, unknown>>() => TypedEventTarget<T>;
|
|
147
|
+
type TryCatchResult<T> = {
|
|
148
|
+
success: true;
|
|
149
|
+
data: T;
|
|
150
|
+
} | {
|
|
151
|
+
success: false;
|
|
152
|
+
error: unknown;
|
|
153
|
+
};
|
|
154
|
+
declare function tryCatch<T>(fn: () => T): TryCatchResult<T>;
|
|
155
|
+
declare function tryCatchAsync<T>(fn: () => Promise<T>): Promise<TryCatchResult<T>>;
|
|
156
|
+
declare function jsonSafeParse<T>(json: string): TryCatchResult<T>;
|
|
157
|
+
/** Quote a SQLite identifier (table/column name), handling dot-separated schema qualifiers. */
|
|
158
|
+
declare function quoteId(name: string): string;
|
|
159
|
+
|
|
160
|
+
type CrdtEventType = "item-created" | "item-updated";
|
|
161
|
+
type CrdtEventStatus = "pending" | "applied" | "failed" | "skipped";
|
|
162
|
+
type CrdtEventOrigin = "remote" | "own" | "local";
|
|
163
|
+
type PersistedCrdtEvent = {
|
|
164
|
+
schema_version: number;
|
|
165
|
+
sync_id: number;
|
|
166
|
+
status: CrdtEventStatus;
|
|
167
|
+
type: CrdtEventType;
|
|
168
|
+
timestamp: string;
|
|
169
|
+
origin: CrdtEventOrigin;
|
|
170
|
+
source_node_id: string;
|
|
171
|
+
dataset: string;
|
|
172
|
+
item_id: string;
|
|
173
|
+
payload: string;
|
|
174
|
+
};
|
|
175
|
+
type CrdtUpdateLogItem = {
|
|
176
|
+
dataset: string;
|
|
177
|
+
item_id: string;
|
|
178
|
+
payload: string;
|
|
179
|
+
};
|
|
180
|
+
type CrdtUpdateLogPayload = Record<string, string>;
|
|
181
|
+
|
|
182
|
+
type StoredValue<T> = {
|
|
183
|
+
get current(): T;
|
|
184
|
+
set current(newValue: T);
|
|
185
|
+
};
|
|
186
|
+
declare function createStoredValue<T>({ initialValue, saveToStorage, }: {
|
|
187
|
+
initialValue: T;
|
|
188
|
+
saveToStorage?: (value: T) => void;
|
|
189
|
+
}): StoredValue<T>;
|
|
190
|
+
|
|
191
|
+
type CrdtEvent = {
|
|
192
|
+
type: CrdtEventType;
|
|
193
|
+
dataset: string;
|
|
194
|
+
item_id: string;
|
|
195
|
+
payload: Record<string, unknown>;
|
|
196
|
+
};
|
|
197
|
+
type MigratableEvent = {
|
|
198
|
+
schema_version: number;
|
|
199
|
+
type: CrdtEventType;
|
|
200
|
+
dataset: string;
|
|
201
|
+
item_id: string;
|
|
202
|
+
payload: string;
|
|
203
|
+
};
|
|
204
|
+
type CrdtEventTransformer = (event: CrdtEvent) => CrdtEvent | null;
|
|
205
|
+
type MigrationStepSql = {
|
|
206
|
+
sql: string;
|
|
207
|
+
parameters?: readonly unknown[];
|
|
208
|
+
} | Compilable | ((db: Kysely<unknown>) => Compilable);
|
|
209
|
+
type TableRename = {
|
|
210
|
+
oldTable: string;
|
|
211
|
+
newTable: string;
|
|
212
|
+
};
|
|
213
|
+
type MigrationStep = {
|
|
214
|
+
sql: MigrationStepSql | MigrationStepSql[];
|
|
215
|
+
eventTransformer?: MigrationEventTransformers;
|
|
216
|
+
tableRenames?: TableRename[];
|
|
217
|
+
tableDrops?: string[];
|
|
218
|
+
};
|
|
219
|
+
type RawMigrationStep = {
|
|
220
|
+
sql: MigrationSql[];
|
|
221
|
+
eventTransformer?: CompiledMigrationEventTransformer;
|
|
222
|
+
tableRenames?: TableRename[];
|
|
223
|
+
tableDrops?: string[];
|
|
224
|
+
};
|
|
225
|
+
type MigrationSql = {
|
|
226
|
+
sql: string;
|
|
227
|
+
parameters: readonly unknown[];
|
|
228
|
+
};
|
|
229
|
+
type DataTypeExpression = ColumnDataType | Expression<any>;
|
|
230
|
+
declare const migrationSteps: {
|
|
231
|
+
createTable: (table: string, build: (table: CreateTableBuilder<string, never>) => Compilable) => MigrationStep;
|
|
232
|
+
dropTable: (table: string) => MigrationStep;
|
|
233
|
+
createIndex: (indexName: string, build: (index: CreateIndexBuilder) => Compilable) => MigrationStep;
|
|
234
|
+
dropIndex: (indexName: string) => MigrationStep;
|
|
235
|
+
renameTable: ({ oldTable, newTable }: {
|
|
236
|
+
oldTable: string;
|
|
237
|
+
newTable: string;
|
|
238
|
+
}) => MigrationStep;
|
|
239
|
+
renameColumn: ({ table, oldColumn, newColumn, }: {
|
|
240
|
+
table: string;
|
|
241
|
+
oldColumn: string;
|
|
242
|
+
newColumn: string;
|
|
243
|
+
}) => MigrationStep;
|
|
244
|
+
addColumn: ({ table, column, type, defaultValue, build, }: {
|
|
245
|
+
table: string;
|
|
246
|
+
column: string;
|
|
247
|
+
type: DataTypeExpression;
|
|
248
|
+
defaultValue: number | boolean | string | null;
|
|
249
|
+
build?: ColumnDefinitionBuilderCallback;
|
|
250
|
+
}) => MigrationStep;
|
|
251
|
+
dropColumn: ({ table, column }: {
|
|
252
|
+
table: string;
|
|
253
|
+
column: string;
|
|
254
|
+
}) => MigrationStep;
|
|
255
|
+
};
|
|
256
|
+
type MigrationEventTransformers = Record<string, CrdtEventTransformer>;
|
|
257
|
+
type CompiledMigrationEventTransformer = (event: CrdtEvent) => CrdtEvent | null;
|
|
258
|
+
declare function createMigrations(buildMigrations: (builder: typeof migrationSteps) => Record<number, MigrationStep[]>): Record<number, RawMigrationStep>;
|
|
259
|
+
type Migrations = ReturnType<typeof createMigrations>;
|
|
260
|
+
type MigrationsDb = {
|
|
261
|
+
startTransaction: (callback: (tx: MigrationsTransaction) => void) => void;
|
|
262
|
+
};
|
|
263
|
+
type MigrationsTransaction = {
|
|
264
|
+
execute: (sql: string, parameters: readonly unknown[]) => void;
|
|
265
|
+
};
|
|
266
|
+
declare function createMigrator({ migrations, schemaVersion, updateLogTableName, }: {
|
|
267
|
+
migrations: Migrations;
|
|
268
|
+
schemaVersion: StoredValue<number>;
|
|
269
|
+
updateLogTableName?: string;
|
|
270
|
+
}): {
|
|
271
|
+
latestSchemaVersion: number;
|
|
272
|
+
readonly currentSchemaVersion: number;
|
|
273
|
+
migrateDbToLatest: (db: MigrationsDb) => void;
|
|
274
|
+
migrateEvent: <Event extends MigratableEvent>(event: Event, targetVersion?: number) => Event | null;
|
|
275
|
+
migrateEvents: <Event extends MigratableEvent>(events: Event[], targetVersion?: number) => Event[];
|
|
276
|
+
};
|
|
277
|
+
type SyncDbMigrator = ReturnType<typeof createMigrator>;
|
|
278
|
+
|
|
279
|
+
type PendingCrdtEvent = {
|
|
280
|
+
type: CrdtEventType;
|
|
281
|
+
dataset: string;
|
|
282
|
+
item_id: string;
|
|
283
|
+
timestamp: string;
|
|
284
|
+
payload: string;
|
|
285
|
+
};
|
|
286
|
+
declare const createSQLiteCrdtApplyFunction: ({ db, updateLogTableName, }: {
|
|
287
|
+
db: SQLiteTransactionWrapper<any>;
|
|
288
|
+
updateLogTableName: string;
|
|
289
|
+
}) => (event: PendingCrdtEvent) => void;
|
|
290
|
+
type CreateCrdtApplyOpts = {
|
|
291
|
+
getCrdtUpdateLog: (opts: {
|
|
292
|
+
itemId: string;
|
|
293
|
+
dataset: string;
|
|
294
|
+
}) => CrdtUpdateLogPayload | null;
|
|
295
|
+
insertItem: (opts: {
|
|
296
|
+
dataset: string;
|
|
297
|
+
payload: Record<string, unknown>;
|
|
298
|
+
}) => void;
|
|
299
|
+
insertCrdtUpdateLog: (opts: {
|
|
300
|
+
dataset: string;
|
|
301
|
+
itemId: string;
|
|
302
|
+
payload: string;
|
|
303
|
+
}) => void;
|
|
304
|
+
updateItem: (opts: {
|
|
305
|
+
dataset: string;
|
|
306
|
+
itemId: string;
|
|
307
|
+
payload: Record<string, unknown>;
|
|
308
|
+
}) => void;
|
|
309
|
+
updateCrdtUpdateLog: (opts: {
|
|
310
|
+
dataset: string;
|
|
311
|
+
itemId: string;
|
|
312
|
+
payload: string;
|
|
313
|
+
}) => void;
|
|
314
|
+
};
|
|
315
|
+
declare function createCrdtApplyFunction({ getCrdtUpdateLog, insertItem, insertCrdtUpdateLog, updateItem, updateCrdtUpdateLog, }: CreateCrdtApplyOpts): (event: PendingCrdtEvent) => void;
|
|
316
|
+
|
|
317
|
+
type LocalCrdtEvent = {
|
|
318
|
+
type: CrdtEventType;
|
|
319
|
+
dataset: string;
|
|
320
|
+
item_id: string;
|
|
321
|
+
payload: string;
|
|
322
|
+
timestamp: string;
|
|
323
|
+
schema_version: number;
|
|
324
|
+
};
|
|
325
|
+
type OwnCrdtEvent = {
|
|
326
|
+
type: CrdtEventType;
|
|
327
|
+
dataset: string;
|
|
328
|
+
item_id: string;
|
|
329
|
+
payload: string;
|
|
330
|
+
timestamp?: undefined;
|
|
331
|
+
schema_version?: undefined;
|
|
332
|
+
};
|
|
333
|
+
type RemoteCrdtEvent = {
|
|
334
|
+
type: CrdtEventType;
|
|
335
|
+
dataset: string;
|
|
336
|
+
item_id: string;
|
|
337
|
+
payload: string;
|
|
338
|
+
timestamp: string;
|
|
339
|
+
schema_version: number;
|
|
340
|
+
};
|
|
341
|
+
type GetEventsOptions = {
|
|
342
|
+
afterSyncId?: number;
|
|
343
|
+
status?: CrdtEventStatus;
|
|
344
|
+
excludeOrigin?: string;
|
|
345
|
+
excludeNodeId?: string;
|
|
346
|
+
limit?: number;
|
|
347
|
+
};
|
|
348
|
+
type GetEventsBatch = {
|
|
349
|
+
events: PersistedCrdtEvent[];
|
|
350
|
+
hasMore: boolean;
|
|
351
|
+
nextSyncId: number;
|
|
352
|
+
};
|
|
353
|
+
type EventUpdate = {
|
|
354
|
+
status: CrdtEventStatus;
|
|
355
|
+
schema_version: number;
|
|
356
|
+
type: CrdtEventType;
|
|
357
|
+
dataset: string;
|
|
358
|
+
item_id: string;
|
|
359
|
+
payload: string;
|
|
360
|
+
};
|
|
361
|
+
type StorageHLC = Pick<HLCCounter, "getNextHLC" | "mergeHLC">;
|
|
362
|
+
type DbSyncerStorage = {
|
|
363
|
+
nodeId: string;
|
|
364
|
+
syncId: StoredValue<number>;
|
|
365
|
+
migrator: SyncDbMigrator;
|
|
366
|
+
persistEvent: (events: PersistedCrdtEvent) => void;
|
|
367
|
+
getEventsBatch: (options: GetEventsOptions) => PersistedCrdtEvent[];
|
|
368
|
+
updateEvent: (syncId: number, update: EventUpdate) => void;
|
|
369
|
+
handleCrdtEventApply: (event: PersistedCrdtEvent) => void;
|
|
370
|
+
hlc: StorageHLC;
|
|
371
|
+
transaction?: (callback: () => void) => void;
|
|
372
|
+
};
|
|
373
|
+
type CrdtStorage = ReturnType<typeof createCrdtStorage>;
|
|
374
|
+
type EventsAppliedPayload = {
|
|
375
|
+
syncId: number;
|
|
376
|
+
};
|
|
377
|
+
declare function createCrdtStorage(storage: DbSyncerStorage): {
|
|
378
|
+
getEventsBatch: (options: GetEventsOptions) => GetEventsBatch;
|
|
379
|
+
enqueueLocalEvents: (events: LocalCrdtEvent[], sourceNodeId: string) => void;
|
|
380
|
+
enqueueOwnEvents: (events: OwnCrdtEvent[]) => void;
|
|
381
|
+
enqueueRemoteEvents: (events: RemoteCrdtEvent[]) => void;
|
|
382
|
+
applyOwnEvent: (event: OwnCrdtEvent, { wrapInTransaction }?: {
|
|
383
|
+
wrapInTransaction?: boolean;
|
|
384
|
+
}) => void;
|
|
385
|
+
dispatchEventsApplied: () => void;
|
|
386
|
+
addEventListener: <K extends "events-applied">(type: K, listener: (event: TypedEvent<{
|
|
387
|
+
"events-applied": EventsAppliedPayload;
|
|
388
|
+
}[K]>) => void) => void;
|
|
389
|
+
removeEventListener: <K extends "events-applied">(type: K, listener: (event: TypedEvent<{
|
|
390
|
+
"events-applied": EventsAppliedPayload;
|
|
391
|
+
}[K]>) => void) => void;
|
|
392
|
+
};
|
|
393
|
+
|
|
394
|
+
type WorkerState = {
|
|
395
|
+
remoteState: "online" | "offline" | "pending";
|
|
396
|
+
};
|
|
397
|
+
type EventsPullResponse = {
|
|
398
|
+
events: {
|
|
399
|
+
schema_version: number;
|
|
400
|
+
type: CrdtEventType;
|
|
401
|
+
timestamp: string;
|
|
402
|
+
dataset: string;
|
|
403
|
+
item_id: string;
|
|
404
|
+
payload: string;
|
|
405
|
+
}[];
|
|
406
|
+
hasMore: boolean;
|
|
407
|
+
nextSyncId: number;
|
|
408
|
+
};
|
|
409
|
+
type WorkerConfig<Props = any> = {
|
|
410
|
+
dbId: string;
|
|
411
|
+
clientId: string;
|
|
412
|
+
clearOnInit?: boolean;
|
|
413
|
+
props: Props;
|
|
414
|
+
};
|
|
415
|
+
|
|
416
|
+
type CrdtSyncRemoteSourceConfig = {
|
|
417
|
+
bufferSize: number;
|
|
418
|
+
storage: CrdtStorage;
|
|
419
|
+
migrator: SyncDbMigrator;
|
|
420
|
+
pullSyncId: StoredValue<number>;
|
|
421
|
+
pushSyncId: StoredValue<number>;
|
|
422
|
+
nodeId: string;
|
|
423
|
+
remoteFactory?: CreateRemoteSourceFactory;
|
|
424
|
+
};
|
|
425
|
+
type EventsPullRequest = {
|
|
426
|
+
afterSyncId: number;
|
|
427
|
+
excludeNodeId?: string;
|
|
428
|
+
};
|
|
429
|
+
type EventsPushRequest = {
|
|
430
|
+
nodeId: string;
|
|
431
|
+
events: (PendingCrdtEvent & {
|
|
432
|
+
schema_version: number;
|
|
433
|
+
})[];
|
|
434
|
+
};
|
|
435
|
+
type EventsPushResponse = {
|
|
436
|
+
ok: boolean;
|
|
437
|
+
};
|
|
438
|
+
type CrdtSyncRemoteSource = ReturnType<typeof createCrdtSyncRemoteSource>;
|
|
439
|
+
type CreateRemoteSourceFactory = (opts: {
|
|
440
|
+
onEventsAvailable: (newSyncId: number) => void;
|
|
441
|
+
}) => RemoteSource | Promise<RemoteSource>;
|
|
442
|
+
type RemoteSource = {
|
|
443
|
+
pullEvents: (request: EventsPullRequest) => Promise<EventsPullResponse>;
|
|
444
|
+
pushEvents: (request: EventsPushRequest) => Promise<EventsPushResponse>;
|
|
445
|
+
disconnect?: () => void | Promise<void>;
|
|
446
|
+
};
|
|
447
|
+
type RemoteSourceState = {
|
|
448
|
+
type: "pending";
|
|
449
|
+
} | {
|
|
450
|
+
type: "offline";
|
|
451
|
+
reason: OfflineReason;
|
|
452
|
+
} | {
|
|
453
|
+
type: "online";
|
|
454
|
+
source: RemoteSource;
|
|
455
|
+
};
|
|
456
|
+
type OfflineReason = "NOT_INITIALIZED" | "INITIALIZATION_FAILED" | "REMOTE_PUSH_ERROR" | "REMOTE_PULL_ERROR" | "DISCONNECTED";
|
|
457
|
+
declare const createCrdtSyncRemoteSource: ({ bufferSize, storage, migrator, pullSyncId, pushSyncId, nodeId, remoteFactory, }: CrdtSyncRemoteSourceConfig) => {
|
|
458
|
+
goOnline: () => Promise<void>;
|
|
459
|
+
goOffline: {
|
|
460
|
+
(reason: OfflineReason): Promise<void>;
|
|
461
|
+
promise(): Promise<void> | null;
|
|
462
|
+
isExecuting(): boolean;
|
|
463
|
+
};
|
|
464
|
+
syncWithRemote: {
|
|
465
|
+
(): Promise<void>;
|
|
466
|
+
promise(): Promise<void> | null;
|
|
467
|
+
isExecuting(): boolean;
|
|
468
|
+
};
|
|
469
|
+
getState: () => "pending" | "offline" | "online";
|
|
470
|
+
dispose: () => Promise<void>;
|
|
471
|
+
addEventListener: <K extends "state-changed">(type: K, listener: (event: TypedEvent<{
|
|
472
|
+
"state-changed": RemoteSourceState["type"];
|
|
473
|
+
}[K]>) => void) => void;
|
|
474
|
+
removeEventListener: <K extends "state-changed">(type: K, listener: (event: TypedEvent<{
|
|
475
|
+
"state-changed": RemoteSourceState["type"];
|
|
476
|
+
}[K]>) => void) => void;
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
export { type TypedEventTarget as $, type CrdtEventStatus as A, type CrdtEventType as B, type CrdtStorage as C, type DatabaseIntrospection as D, type ExecuteParams as E, type CrdtUpdateLogItem as F, type GetEventsOptions as G, type HLC as H, type CrdtUpdateLogPayload as I, createStoredValue as J, type KyselyQueryFactory as K, type Logger as L, type MigratableEvent as M, type PreparedStatement as N, createDeferredPromise as O, type PersistedCrdtEvent as P, type QueryBuilderOutput as Q, createTypedEventTarget as R, SQLiteDbWrapper as S, TypedEvent as T, type DeferredPromise as U, type DistributiveOmit as V, type WorkerState as W, generateId as X, jsonSafeParse as Y, quoteId as Z, TypedBroadcastChannel as _, type SQLiteTransactionWrapper as a, tryCatch as a0, tryCatchAsync as a1, type WorkerConfig as a2, type CreateRemoteSourceFactory as a3, type GetEventsBatch as a4, type EventsPullResponse as a5, type StoredValue as b, type ExecuteResult as c, compareHLC as d, deserializeHLC as e, HLCCounter as f, type TableMetadata as g, type LogLevel as h, introspectDb as i, startPerformanceLogger as j, createMigrations as k, createMigrator as l, type Migrations as m, type MigrationsDb as n, type SyncDbMigrator as o, createCrdtApplyFunction as p, createSQLiteCrdtApplyFunction as q, type PendingCrdtEvent as r, serializeHLC as s, createCrdtStorage as t, type CrdtSyncRemoteSource as u, createCrdtSyncRemoteSource as v, type EventsPullRequest as w, type EventsPushRequest as x, type EventsPushResponse as y, type CrdtEventOrigin as z };
|