@sqlite-sync/core 0.6.0 → 0.7.1
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-3L4QG3BZ.js → chunk-LBYL5L4O.js} +371 -34
- package/dist/chunk-LBYL5L4O.js.map +1 -0
- package/dist/{crdt-sync-remote-source-CJrVzW-B.d.ts → crdt-sync-remote-source-yXp-iMEd.d.ts} +304 -185
- package/dist/index.d.ts +126 -18
- package/dist/index.js +296 -74
- package/dist/index.js.map +1 -1
- package/dist/reset-state-D6UslXaj.d.ts +19 -0
- package/dist/server.d.ts +2 -2
- package/dist/worker.d.ts +10 -3
- package/dist/worker.js +12 -2
- package/dist/worker.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-3L4QG3BZ.js.map +0 -1
- package/dist/reset-state-Comk7IC0.d.ts +0 -81
package/dist/{crdt-sync-remote-source-CJrVzW-B.d.ts → crdt-sync-remote-source-yXp-iMEd.d.ts}
RENAMED
|
@@ -1,6 +1,237 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { CreateTableBuilder, Compilable, Kysely, CreateIndexBuilder, ColumnDataType, Expression, ColumnDefinitionBuilderCallback, ColumnType, CompiledQuery } from 'kysely';
|
|
2
2
|
import { Sqlite3Static, Database, SqlValue, FunctionOptions } from '@sqlite.org/sqlite-wasm';
|
|
3
3
|
|
|
4
|
+
type NewCrdtEvent = {
|
|
5
|
+
type: CrdtEventType;
|
|
6
|
+
dataset: string;
|
|
7
|
+
item_id: string;
|
|
8
|
+
payload: Record<string, unknown>;
|
|
9
|
+
};
|
|
10
|
+
type CrdtEventType = "item-created" | "item-updated" | "item-deleted";
|
|
11
|
+
type CrdtEventStatus = "pending" | "applied" | "failed" | "deduped";
|
|
12
|
+
/** Persisted on applied events that were accepted but did not mutate materialized state. */
|
|
13
|
+
declare const CRDT_EVENT_NO_OP_PAYLOAD = "no-op";
|
|
14
|
+
declare function isNoOpCrdtEventPayload(payload: string): payload is "no-op";
|
|
15
|
+
type CrdtEventOrigin = "remote" | "own" | "own-applied" | "local";
|
|
16
|
+
type PersistedCrdtEvent = {
|
|
17
|
+
schema_version: number;
|
|
18
|
+
sync_id: number;
|
|
19
|
+
status: CrdtEventStatus;
|
|
20
|
+
type: CrdtEventType;
|
|
21
|
+
timestamp: string;
|
|
22
|
+
origin: CrdtEventOrigin;
|
|
23
|
+
source_node_id: string;
|
|
24
|
+
dataset: string;
|
|
25
|
+
item_id: string;
|
|
26
|
+
payload: string;
|
|
27
|
+
};
|
|
28
|
+
type CrdtUpdateLogItem = {
|
|
29
|
+
dataset: string;
|
|
30
|
+
item_id: string;
|
|
31
|
+
payload: string;
|
|
32
|
+
};
|
|
33
|
+
type CrdtUpdateLogPayload = Record<string, string>;
|
|
34
|
+
|
|
35
|
+
type StoredValue<T> = {
|
|
36
|
+
get current(): T;
|
|
37
|
+
set current(newValue: T);
|
|
38
|
+
};
|
|
39
|
+
declare function createStoredValue<T>({ initialValue, saveToStorage, }: {
|
|
40
|
+
initialValue: T;
|
|
41
|
+
saveToStorage?: (value: T) => void;
|
|
42
|
+
}): StoredValue<T>;
|
|
43
|
+
|
|
44
|
+
type MigratableEvent = {
|
|
45
|
+
schema_version: number;
|
|
46
|
+
type: CrdtEventType;
|
|
47
|
+
dataset: string;
|
|
48
|
+
item_id: string;
|
|
49
|
+
payload: string;
|
|
50
|
+
};
|
|
51
|
+
type CrdtEventTransformer = (event: NewCrdtEvent) => NewCrdtEvent | null;
|
|
52
|
+
type MigrationStepSql = {
|
|
53
|
+
sql: string;
|
|
54
|
+
parameters?: readonly unknown[];
|
|
55
|
+
} | Compilable | ((db: Kysely<unknown>) => Compilable);
|
|
56
|
+
type TableRename = {
|
|
57
|
+
oldTable: string;
|
|
58
|
+
newTable: string;
|
|
59
|
+
};
|
|
60
|
+
type MigrationStep = {
|
|
61
|
+
sql: MigrationStepSql | MigrationStepSql[];
|
|
62
|
+
eventTransformer?: MigrationEventTransformers;
|
|
63
|
+
tableRenames?: TableRename[];
|
|
64
|
+
tableDrops?: string[];
|
|
65
|
+
};
|
|
66
|
+
type RawMigrationStep = {
|
|
67
|
+
sql: MigrationSql[];
|
|
68
|
+
eventTransformer?: CompiledMigrationEventTransformer;
|
|
69
|
+
tableRenames?: TableRename[];
|
|
70
|
+
tableDrops?: string[];
|
|
71
|
+
};
|
|
72
|
+
type MigrationSql = {
|
|
73
|
+
sql: string;
|
|
74
|
+
parameters: readonly unknown[];
|
|
75
|
+
};
|
|
76
|
+
type DataTypeExpression = ColumnDataType | Expression<any>;
|
|
77
|
+
declare const migrationSteps: {
|
|
78
|
+
createTable: (table: string, build: (table: CreateTableBuilder<string, never>) => Compilable) => MigrationStep;
|
|
79
|
+
dropTable: (table: string) => MigrationStep;
|
|
80
|
+
createIndex: (indexName: string, build: (index: CreateIndexBuilder) => Compilable) => MigrationStep;
|
|
81
|
+
dropIndex: (indexName: string) => MigrationStep;
|
|
82
|
+
renameTable: ({ oldTable, newTable }: {
|
|
83
|
+
oldTable: string;
|
|
84
|
+
newTable: string;
|
|
85
|
+
}) => MigrationStep;
|
|
86
|
+
renameColumn: ({ table, oldColumn, newColumn, }: {
|
|
87
|
+
table: string;
|
|
88
|
+
oldColumn: string;
|
|
89
|
+
newColumn: string;
|
|
90
|
+
}) => MigrationStep;
|
|
91
|
+
addColumn: ({ table, column, type, defaultValue, build, }: {
|
|
92
|
+
table: string;
|
|
93
|
+
column: string;
|
|
94
|
+
type: DataTypeExpression;
|
|
95
|
+
defaultValue: number | boolean | string | null;
|
|
96
|
+
build?: ColumnDefinitionBuilderCallback;
|
|
97
|
+
}) => MigrationStep;
|
|
98
|
+
dropColumn: ({ table, column }: {
|
|
99
|
+
table: string;
|
|
100
|
+
column: string;
|
|
101
|
+
}) => MigrationStep;
|
|
102
|
+
};
|
|
103
|
+
type MigrationEventTransformers = Record<string, CrdtEventTransformer>;
|
|
104
|
+
type CompiledMigrationEventTransformer = (event: NewCrdtEvent) => NewCrdtEvent | null;
|
|
105
|
+
declare function createMigrations(buildMigrations: (builder: typeof migrationSteps) => Record<number, MigrationStep[]>): Record<number, RawMigrationStep>;
|
|
106
|
+
type Migrations = ReturnType<typeof createMigrations>;
|
|
107
|
+
type MigrationsDb = {
|
|
108
|
+
startTransaction: (callback: (tx: MigrationsTransaction) => void) => void;
|
|
109
|
+
};
|
|
110
|
+
type MigrationsTransaction = {
|
|
111
|
+
execute: (sql: string, parameters: readonly unknown[], meta?: {
|
|
112
|
+
loggerLevel?: "system";
|
|
113
|
+
}) => void;
|
|
114
|
+
};
|
|
115
|
+
declare function createMigrator({ migrations, schemaVersion, updateLogTableName, }: {
|
|
116
|
+
migrations: Migrations;
|
|
117
|
+
schemaVersion: StoredValue<number>;
|
|
118
|
+
updateLogTableName?: string;
|
|
119
|
+
}): {
|
|
120
|
+
latestSchemaVersion: number;
|
|
121
|
+
readonly currentSchemaVersion: number;
|
|
122
|
+
migrateDbToLatest: (db: MigrationsDb) => void;
|
|
123
|
+
migrateEvent: <Event extends MigratableEvent>(event: Event, targetVersion?: number) => Event | null;
|
|
124
|
+
migrateEvents: <Event extends MigratableEvent>(events: Event[], targetVersion?: number) => Event[];
|
|
125
|
+
};
|
|
126
|
+
type SyncDbMigrator = ReturnType<typeof createMigrator>;
|
|
127
|
+
|
|
128
|
+
type ColumnKind = "text" | "integer" | "real" | "boolean" | "enum";
|
|
129
|
+
type SqliteStorageType = "text" | "integer" | "real";
|
|
130
|
+
type ColumnMeta = {
|
|
131
|
+
kind: ColumnKind;
|
|
132
|
+
/** Storage type used in generated DDL. Booleans store as integer 0/1. */
|
|
133
|
+
sqlType: SqliteStorageType;
|
|
134
|
+
nullable: boolean;
|
|
135
|
+
hasDefault: boolean;
|
|
136
|
+
/** Declared default in JS form (booleans as booleans). */
|
|
137
|
+
defaultValue?: unknown;
|
|
138
|
+
/** Allowed values for `enum` columns. */
|
|
139
|
+
enumValues?: readonly string[];
|
|
140
|
+
description?: string;
|
|
141
|
+
};
|
|
142
|
+
declare class ColumnBuilder<Value, HasDefault extends boolean = false> {
|
|
143
|
+
readonly meta: ColumnMeta;
|
|
144
|
+
readonly "~types": {
|
|
145
|
+
value: Value;
|
|
146
|
+
hasDefault: HasDefault;
|
|
147
|
+
};
|
|
148
|
+
constructor(meta: ColumnMeta);
|
|
149
|
+
/** Allow SQL NULL; widens the value type with `null`. */
|
|
150
|
+
nullable(): ColumnBuilder<Value | null, HasDefault>;
|
|
151
|
+
/** Declare a column default. The value is used both in DDL and when migrating older events. */
|
|
152
|
+
default(value: Value): ColumnBuilder<Value, true>;
|
|
153
|
+
/** Narrow the TypeScript value type without changing storage, e.g. `t.text().$type<"movie" | "tv">()`. */
|
|
154
|
+
$type<Narrowed extends Value>(): ColumnBuilder<Narrowed, HasDefault>;
|
|
155
|
+
/** Human/AI-readable description, surfaced in generated schema docs. */
|
|
156
|
+
describe(description: string): ColumnBuilder<Value, HasDefault>;
|
|
157
|
+
}
|
|
158
|
+
type AnyColumnBuilder = ColumnBuilder<any, boolean>;
|
|
159
|
+
type TableColumns = Record<string, AnyColumnBuilder>;
|
|
160
|
+
type AnyTableBuilder = TableBuilder<any, any>;
|
|
161
|
+
type SyncSchemaTables = Record<string, AnyTableBuilder>;
|
|
162
|
+
type TableOptions<BaseName extends string | undefined = string | undefined> = {
|
|
163
|
+
/** Override the materialized base table name (defaults to the crdt table name prefixed with "_"). */
|
|
164
|
+
baseName?: BaseName;
|
|
165
|
+
description?: string;
|
|
166
|
+
};
|
|
167
|
+
type Simplify<T> = {
|
|
168
|
+
[K in keyof T]: T[K];
|
|
169
|
+
} & {};
|
|
170
|
+
type ColumnValue<Column> = Column extends ColumnBuilder<infer Value, boolean> ? Value : never;
|
|
171
|
+
type InferRow<Cols extends TableColumns> = Simplify<{
|
|
172
|
+
id: string;
|
|
173
|
+
} & {
|
|
174
|
+
[K in keyof Cols]: ColumnValue<Cols[K]>;
|
|
175
|
+
} & {
|
|
176
|
+
tombstone?: boolean;
|
|
177
|
+
}>;
|
|
178
|
+
type PayloadValidationResult = {
|
|
179
|
+
success: true;
|
|
180
|
+
} | {
|
|
181
|
+
success: false;
|
|
182
|
+
errors: string[];
|
|
183
|
+
};
|
|
184
|
+
declare class TableBuilder<Cols extends TableColumns, BaseName extends string | undefined = undefined> {
|
|
185
|
+
readonly userColumns: Cols;
|
|
186
|
+
readonly "~baseName": BaseName;
|
|
187
|
+
/** All columns in DDL order, including the auto-injected `id` and `tombstone`. */
|
|
188
|
+
readonly columns: Record<string, ColumnMeta>;
|
|
189
|
+
readonly baseName: BaseName;
|
|
190
|
+
readonly description: string | undefined;
|
|
191
|
+
constructor(userColumns: Cols, options?: TableOptions<BaseName>);
|
|
192
|
+
describe(description: string): TableBuilder<Cols, BaseName>;
|
|
193
|
+
/** Type-only: the row shape returned by queries. Do not access at runtime. */
|
|
194
|
+
get $row(): InferRow<Cols>;
|
|
195
|
+
/**
|
|
196
|
+
* Validate a CRDT event payload against the declared columns.
|
|
197
|
+
* `item-created` payloads must contain every column without a default (nullable columns may be omitted);
|
|
198
|
+
* `item-updated` payloads are partial and must not touch `id` or set `tombstone` to a deleted value.
|
|
199
|
+
*/
|
|
200
|
+
validatePayload(payload: Record<string, unknown>, options?: {
|
|
201
|
+
event?: "item-created" | "item-updated";
|
|
202
|
+
}): PayloadValidationResult;
|
|
203
|
+
}
|
|
204
|
+
declare const t: {
|
|
205
|
+
text: () => ColumnBuilder<string, false>;
|
|
206
|
+
integer: () => ColumnBuilder<number, false>;
|
|
207
|
+
real: () => ColumnBuilder<number, false>;
|
|
208
|
+
/** Stored as INTEGER 0/1; always bind booleans as 0/1 when writing raw SQL. */
|
|
209
|
+
boolean: () => ColumnBuilder<boolean, false>;
|
|
210
|
+
/**
|
|
211
|
+
* Union of string literals stored as TEXT, e.g. `t.enum(["movie", "tv"])`. Unlike `$type`, the
|
|
212
|
+
* allowed values are kept as runtime metadata, so payloads are validated against them.
|
|
213
|
+
* For an open-ended set, use `t.text().$type<"idle" | (string & {})>()` instead.
|
|
214
|
+
*/
|
|
215
|
+
enum: <const Values extends readonly [string, ...string[]]>(values: Values) => ColumnBuilder<Values[number], false>;
|
|
216
|
+
table: <Cols extends TableColumns, const BaseName extends string | undefined = undefined>(columns: Cols, options?: TableOptions<BaseName>) => TableBuilder<Cols, BaseName>;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
type CrdtTableConfig = {
|
|
220
|
+
baseTableName: string;
|
|
221
|
+
crdtTableName: string;
|
|
222
|
+
};
|
|
223
|
+
interface SyncDbSchema<ClientDB = {}, ServerDB = {}, MutationsDB = {}> {
|
|
224
|
+
get tablesConfig(): CrdtTableConfig[];
|
|
225
|
+
get migrations(): Migrations;
|
|
226
|
+
get tables(): SyncSchemaTables;
|
|
227
|
+
"~clientSchema": ClientDB;
|
|
228
|
+
"~serverSchema": ServerDB;
|
|
229
|
+
"~mutationsSchema": MutationsDB;
|
|
230
|
+
}
|
|
231
|
+
type ReadonlyTable<Table extends Record<string, unknown>> = {
|
|
232
|
+
[K in keyof Table]: ColumnType<Table[K], never, never>;
|
|
233
|
+
};
|
|
234
|
+
|
|
4
235
|
interface HLC {
|
|
5
236
|
timestamp: number;
|
|
6
237
|
counter: number;
|
|
@@ -21,6 +252,24 @@ declare function serializeHLC(hlc: HLC): string;
|
|
|
21
252
|
declare function deserializeHLC(serialized: string): HLC;
|
|
22
253
|
declare function compareHLC(one: HLC, two: HLC): number;
|
|
23
254
|
|
|
255
|
+
type TableMetadata = {
|
|
256
|
+
name: string;
|
|
257
|
+
isView: boolean;
|
|
258
|
+
columns: ColumnMetadata[];
|
|
259
|
+
};
|
|
260
|
+
type DatabaseIntrospection = Record<string, TableMetadata>;
|
|
261
|
+
type ColumnMetadata = {
|
|
262
|
+
name: string;
|
|
263
|
+
dataType: string;
|
|
264
|
+
isNullable: boolean;
|
|
265
|
+
isAutoIncrementing: boolean;
|
|
266
|
+
hasDefaultValue: boolean;
|
|
267
|
+
defaultValueSql: string | null;
|
|
268
|
+
isPrimaryKey: boolean;
|
|
269
|
+
comment: undefined;
|
|
270
|
+
};
|
|
271
|
+
declare function introspectDb<BaseDatabase>(_db: SQLiteDbWrapper<BaseDatabase>): DatabaseIntrospection;
|
|
272
|
+
|
|
24
273
|
type LogLevel = "info" | "warning" | "error" | "trace" | "system";
|
|
25
274
|
type Logger = (type: string, message: string, level?: LogLevel) => void;
|
|
26
275
|
declare const startPerformanceLogger: (logger: Logger) => {
|
|
@@ -104,22 +353,6 @@ type TypedStatement<TParams extends Record<string, unknown>, TResult> = {
|
|
|
104
353
|
type KyselyStatementFactory<TParams extends Record<string, unknown>, TDatabase, TQuery extends Compilable<TResult>, TResult = QueryBuilderOutput<TQuery>> = (kysely: Kysely<TDatabase>, params: ParamsGetter<TParams>) => TQuery;
|
|
105
354
|
type KyselyQueryFactory<TDatabase, TQuery extends Compilable<TResult>, TResult = QueryBuilderOutput<TQuery>> = (kysely: Kysely<TDatabase>) => TQuery;
|
|
106
355
|
|
|
107
|
-
type TableMetadata = {
|
|
108
|
-
name: string;
|
|
109
|
-
isView: boolean;
|
|
110
|
-
columns: ColumnMetadata[];
|
|
111
|
-
};
|
|
112
|
-
type DatabaseIntrospection = Record<string, TableMetadata>;
|
|
113
|
-
type ColumnMetadata = {
|
|
114
|
-
name: string;
|
|
115
|
-
dataType: string;
|
|
116
|
-
isNullable: boolean;
|
|
117
|
-
isAutoIncrementing: boolean;
|
|
118
|
-
hasDefaultValue: boolean;
|
|
119
|
-
comment: undefined;
|
|
120
|
-
};
|
|
121
|
-
declare function introspectDb<BaseDatabase>(_db: SQLiteDbWrapper<BaseDatabase>): DatabaseIntrospection;
|
|
122
|
-
|
|
123
356
|
type DeferredPromise<T> = {
|
|
124
357
|
promise: Promise<T>;
|
|
125
358
|
resolve: (value: T) => void;
|
|
@@ -169,130 +402,6 @@ type ParsedTableName = {
|
|
|
169
402
|
fullIdentifier: string;
|
|
170
403
|
};
|
|
171
404
|
|
|
172
|
-
type CrdtEventType = "item-created" | "item-updated" | "item-deleted";
|
|
173
|
-
type CrdtEventStatus = "pending" | "applied" | "failed" | "deduped";
|
|
174
|
-
/** Persisted on applied events that were accepted but did not mutate materialized state. */
|
|
175
|
-
declare const CRDT_EVENT_NO_OP_PAYLOAD = "no-op";
|
|
176
|
-
declare function isNoOpCrdtEventPayload(payload: string): payload is "no-op";
|
|
177
|
-
type CrdtEventOrigin = "remote" | "own" | "local";
|
|
178
|
-
type PersistedCrdtEvent = {
|
|
179
|
-
schema_version: number;
|
|
180
|
-
sync_id: number;
|
|
181
|
-
status: CrdtEventStatus;
|
|
182
|
-
type: CrdtEventType;
|
|
183
|
-
timestamp: string;
|
|
184
|
-
origin: CrdtEventOrigin;
|
|
185
|
-
source_node_id: string;
|
|
186
|
-
dataset: string;
|
|
187
|
-
item_id: string;
|
|
188
|
-
payload: string;
|
|
189
|
-
};
|
|
190
|
-
type CrdtUpdateLogItem = {
|
|
191
|
-
dataset: string;
|
|
192
|
-
item_id: string;
|
|
193
|
-
payload: string;
|
|
194
|
-
};
|
|
195
|
-
type CrdtUpdateLogPayload = Record<string, string>;
|
|
196
|
-
|
|
197
|
-
type StoredValue<T> = {
|
|
198
|
-
get current(): T;
|
|
199
|
-
set current(newValue: T);
|
|
200
|
-
};
|
|
201
|
-
declare function createStoredValue<T>({ initialValue, saveToStorage, }: {
|
|
202
|
-
initialValue: T;
|
|
203
|
-
saveToStorage?: (value: T) => void;
|
|
204
|
-
}): StoredValue<T>;
|
|
205
|
-
|
|
206
|
-
type CrdtEvent = {
|
|
207
|
-
type: CrdtEventType;
|
|
208
|
-
dataset: string;
|
|
209
|
-
item_id: string;
|
|
210
|
-
payload: Record<string, unknown>;
|
|
211
|
-
};
|
|
212
|
-
type MigratableEvent = {
|
|
213
|
-
schema_version: number;
|
|
214
|
-
type: CrdtEventType;
|
|
215
|
-
dataset: string;
|
|
216
|
-
item_id: string;
|
|
217
|
-
payload: string;
|
|
218
|
-
};
|
|
219
|
-
type CrdtEventTransformer = (event: CrdtEvent) => CrdtEvent | null;
|
|
220
|
-
type MigrationStepSql = {
|
|
221
|
-
sql: string;
|
|
222
|
-
parameters?: readonly unknown[];
|
|
223
|
-
} | Compilable | ((db: Kysely<unknown>) => Compilable);
|
|
224
|
-
type TableRename = {
|
|
225
|
-
oldTable: string;
|
|
226
|
-
newTable: string;
|
|
227
|
-
};
|
|
228
|
-
type MigrationStep = {
|
|
229
|
-
sql: MigrationStepSql | MigrationStepSql[];
|
|
230
|
-
eventTransformer?: MigrationEventTransformers;
|
|
231
|
-
tableRenames?: TableRename[];
|
|
232
|
-
tableDrops?: string[];
|
|
233
|
-
};
|
|
234
|
-
type RawMigrationStep = {
|
|
235
|
-
sql: MigrationSql[];
|
|
236
|
-
eventTransformer?: CompiledMigrationEventTransformer;
|
|
237
|
-
tableRenames?: TableRename[];
|
|
238
|
-
tableDrops?: string[];
|
|
239
|
-
};
|
|
240
|
-
type MigrationSql = {
|
|
241
|
-
sql: string;
|
|
242
|
-
parameters: readonly unknown[];
|
|
243
|
-
};
|
|
244
|
-
type DataTypeExpression = ColumnDataType | Expression<any>;
|
|
245
|
-
declare const migrationSteps: {
|
|
246
|
-
createTable: (table: string, build: (table: CreateTableBuilder<string, never>) => Compilable) => MigrationStep;
|
|
247
|
-
dropTable: (table: string) => MigrationStep;
|
|
248
|
-
createIndex: (indexName: string, build: (index: CreateIndexBuilder) => Compilable) => MigrationStep;
|
|
249
|
-
dropIndex: (indexName: string) => MigrationStep;
|
|
250
|
-
renameTable: ({ oldTable, newTable }: {
|
|
251
|
-
oldTable: string;
|
|
252
|
-
newTable: string;
|
|
253
|
-
}) => MigrationStep;
|
|
254
|
-
renameColumn: ({ table, oldColumn, newColumn, }: {
|
|
255
|
-
table: string;
|
|
256
|
-
oldColumn: string;
|
|
257
|
-
newColumn: string;
|
|
258
|
-
}) => MigrationStep;
|
|
259
|
-
addColumn: ({ table, column, type, defaultValue, build, }: {
|
|
260
|
-
table: string;
|
|
261
|
-
column: string;
|
|
262
|
-
type: DataTypeExpression;
|
|
263
|
-
defaultValue: number | boolean | string | null;
|
|
264
|
-
build?: ColumnDefinitionBuilderCallback;
|
|
265
|
-
}) => MigrationStep;
|
|
266
|
-
dropColumn: ({ table, column }: {
|
|
267
|
-
table: string;
|
|
268
|
-
column: string;
|
|
269
|
-
}) => MigrationStep;
|
|
270
|
-
};
|
|
271
|
-
type MigrationEventTransformers = Record<string, CrdtEventTransformer>;
|
|
272
|
-
type CompiledMigrationEventTransformer = (event: CrdtEvent) => CrdtEvent | null;
|
|
273
|
-
declare function createMigrations(buildMigrations: (builder: typeof migrationSteps) => Record<number, MigrationStep[]>): Record<number, RawMigrationStep>;
|
|
274
|
-
type Migrations = ReturnType<typeof createMigrations>;
|
|
275
|
-
type MigrationsDb = {
|
|
276
|
-
startTransaction: (callback: (tx: MigrationsTransaction) => void) => void;
|
|
277
|
-
};
|
|
278
|
-
type MigrationsTransaction = {
|
|
279
|
-
execute: (sql: string, parameters: readonly unknown[], meta?: {
|
|
280
|
-
loggerLevel?: "system";
|
|
281
|
-
}) => void;
|
|
282
|
-
};
|
|
283
|
-
declare function createMigrator({ migrations, schemaVersion, updateLogTableName, }: {
|
|
284
|
-
migrations: Migrations;
|
|
285
|
-
schemaVersion: StoredValue<number>;
|
|
286
|
-
updateLogTableName?: string;
|
|
287
|
-
}): {
|
|
288
|
-
latestSchemaVersion: number;
|
|
289
|
-
readonly currentSchemaVersion: number;
|
|
290
|
-
migrateDbToLatest: (db: MigrationsDb) => void;
|
|
291
|
-
migrateEvent: <Event extends MigratableEvent>(event: Event, targetVersion?: number) => Event | null;
|
|
292
|
-
migrateEvents: <Event extends MigratableEvent>(events: Event[], targetVersion?: number) => Event[];
|
|
293
|
-
};
|
|
294
|
-
type SyncDbMigrator = ReturnType<typeof createMigrator>;
|
|
295
|
-
|
|
296
405
|
type SystemDbConfig = {
|
|
297
406
|
eventsTable: ParsedTableName;
|
|
298
407
|
updateLogTable: ParsedTableName;
|
|
@@ -327,44 +436,6 @@ declare function applyWorkerDbSchema(db: SQLiteDbWrapper<any>): {
|
|
|
327
436
|
};
|
|
328
437
|
declare function applyMemoryDbSchema(db: SQLiteDbWrapper<any>): void;
|
|
329
438
|
|
|
330
|
-
type PendingCrdtEvent = {
|
|
331
|
-
type: CrdtEventType;
|
|
332
|
-
dataset: string;
|
|
333
|
-
item_id: string;
|
|
334
|
-
timestamp: string;
|
|
335
|
-
payload: string;
|
|
336
|
-
};
|
|
337
|
-
declare const createSQLiteCrdtApplyFunction: ({ db, dbConfig, }: {
|
|
338
|
-
db: InternalSQLiteWrapper<any>;
|
|
339
|
-
dbConfig: SystemDbConfig;
|
|
340
|
-
}) => (event: PendingCrdtEvent) => void;
|
|
341
|
-
type CreateCrdtApplyOpts = {
|
|
342
|
-
getCrdtUpdateLog: (opts: {
|
|
343
|
-
itemId: string;
|
|
344
|
-
dataset: string;
|
|
345
|
-
}) => CrdtUpdateLogPayload | null;
|
|
346
|
-
insertItem: (opts: {
|
|
347
|
-
dataset: string;
|
|
348
|
-
payload: Record<string, unknown>;
|
|
349
|
-
}) => void;
|
|
350
|
-
insertCrdtUpdateLog: (opts: {
|
|
351
|
-
dataset: string;
|
|
352
|
-
itemId: string;
|
|
353
|
-
payload: string;
|
|
354
|
-
}) => void;
|
|
355
|
-
updateItem: (opts: {
|
|
356
|
-
dataset: string;
|
|
357
|
-
itemId: string;
|
|
358
|
-
payload: Record<string, unknown>;
|
|
359
|
-
}) => void;
|
|
360
|
-
updateCrdtUpdateLog: (opts: {
|
|
361
|
-
dataset: string;
|
|
362
|
-
itemId: string;
|
|
363
|
-
payload: string;
|
|
364
|
-
}) => void;
|
|
365
|
-
};
|
|
366
|
-
declare function createCrdtApplyFunction({ getCrdtUpdateLog, insertItem, insertCrdtUpdateLog, updateItem, updateCrdtUpdateLog, }: CreateCrdtApplyOpts): (event: PendingCrdtEvent) => void;
|
|
367
|
-
|
|
368
439
|
type LocalCrdtEvent = {
|
|
369
440
|
type: CrdtEventType;
|
|
370
441
|
dataset: string;
|
|
@@ -421,21 +492,23 @@ type DbSyncerStorage = {
|
|
|
421
492
|
hlc: StorageHLC;
|
|
422
493
|
eventHlcAccumulator?: StoredValue<string>;
|
|
423
494
|
onEventApplied?: (event: PersistedCrdtEvent) => void;
|
|
495
|
+
schema: Pick<SyncDbSchema, "tables" | "tablesConfig">;
|
|
424
496
|
};
|
|
425
|
-
type CrdtStorage = ReturnType<typeof createCrdtStorage>;
|
|
497
|
+
type CrdtStorage = Omit<ReturnType<typeof createCrdtStorage>, "internal">;
|
|
426
498
|
type EventsAppliedPayload = {
|
|
427
499
|
syncId: number;
|
|
428
500
|
eventHlcSum: string | null;
|
|
429
501
|
};
|
|
502
|
+
type InternalDbSchema = {
|
|
503
|
+
_crdt_events: PersistedCrdtEvent;
|
|
504
|
+
_crdt_update_log: CrdtUpdateLogItem;
|
|
505
|
+
};
|
|
430
506
|
declare function createCrdtStorage(storage: DbSyncerStorage): {
|
|
431
507
|
getEventsBatch: (options: GetEventsBatchQuery) => GetEventsBatch;
|
|
432
508
|
enqueueLocalEvents: (events: LocalCrdtEvent[], sourceNodeId: string) => EnqueueEventsResult;
|
|
433
509
|
enqueueOwnEvents: (events: OwnCrdtEvent[]) => EnqueueEventsResult;
|
|
434
510
|
enqueueRemoteEvents: (events: RemoteCrdtEvent[]) => EnqueueEventsResult;
|
|
435
|
-
|
|
436
|
-
wrapInTransaction?: boolean;
|
|
437
|
-
}) => void;
|
|
438
|
-
dispatchEventsApplied: (syncId?: number) => void;
|
|
511
|
+
applyOwnEvents: (events: OwnCrdtEvent[]) => void;
|
|
439
512
|
checkIsQuiescent: (pushedSyncId: number) => boolean;
|
|
440
513
|
getEventHlcAccumulator: () => string | null;
|
|
441
514
|
addEventListener: <K extends "events-applied" | "remote-event-apply-failed">(type: K, listener: (event: TypedEvent<{
|
|
@@ -452,8 +525,54 @@ declare function createCrdtStorage(storage: DbSyncerStorage): {
|
|
|
452
525
|
syncId: number;
|
|
453
526
|
};
|
|
454
527
|
}[K]>) => void) => void;
|
|
528
|
+
internal: {
|
|
529
|
+
applyOwnEventFromTransaction: (tx: InternalSQLiteTransactionWrapper<InternalDbSchema>, event: OwnCrdtEvent) => void;
|
|
530
|
+
processEnqueuedEvents: {
|
|
531
|
+
(): Promise<void>;
|
|
532
|
+
promise(): Promise<void> | null;
|
|
533
|
+
isExecuting(): boolean;
|
|
534
|
+
};
|
|
535
|
+
};
|
|
455
536
|
};
|
|
456
537
|
|
|
538
|
+
type PendingCrdtEvent = {
|
|
539
|
+
type: CrdtEventType;
|
|
540
|
+
dataset: string;
|
|
541
|
+
item_id: string;
|
|
542
|
+
timestamp: string;
|
|
543
|
+
payload: string;
|
|
544
|
+
};
|
|
545
|
+
declare const createSQLiteCrdtApplyFunction: ({ db, dbConfig, }: {
|
|
546
|
+
db: InternalSQLiteWrapper<any>;
|
|
547
|
+
dbConfig: SystemDbConfig;
|
|
548
|
+
}) => (event: PendingCrdtEvent) => void;
|
|
549
|
+
type CreateCrdtApplyOpts = {
|
|
550
|
+
getCrdtUpdateLog: (opts: {
|
|
551
|
+
itemId: string;
|
|
552
|
+
dataset: string;
|
|
553
|
+
}) => CrdtUpdateLogPayload | null;
|
|
554
|
+
insertItem: (opts: {
|
|
555
|
+
dataset: string;
|
|
556
|
+
payload: Record<string, unknown>;
|
|
557
|
+
}) => void;
|
|
558
|
+
insertCrdtUpdateLog: (opts: {
|
|
559
|
+
dataset: string;
|
|
560
|
+
itemId: string;
|
|
561
|
+
payload: string;
|
|
562
|
+
}) => void;
|
|
563
|
+
updateItem: (opts: {
|
|
564
|
+
dataset: string;
|
|
565
|
+
itemId: string;
|
|
566
|
+
payload: Record<string, unknown>;
|
|
567
|
+
}) => void;
|
|
568
|
+
updateCrdtUpdateLog: (opts: {
|
|
569
|
+
dataset: string;
|
|
570
|
+
itemId: string;
|
|
571
|
+
payload: string;
|
|
572
|
+
}) => void;
|
|
573
|
+
};
|
|
574
|
+
declare function createCrdtApplyFunction({ getCrdtUpdateLog, insertItem, insertCrdtUpdateLog, updateItem, updateCrdtUpdateLog, }: CreateCrdtApplyOpts): (event: PendingCrdtEvent) => void;
|
|
575
|
+
|
|
457
576
|
type WorkerNotificationMessage = {
|
|
458
577
|
notificationType: "new-event-chunk-applied";
|
|
459
578
|
newSyncId: number;
|
|
@@ -591,4 +710,4 @@ declare const createCrdtSyncRemoteSource: ({ bufferSize, storage, migrator, pull
|
|
|
591
710
|
}[K]>) => void) => void;
|
|
592
711
|
};
|
|
593
712
|
|
|
594
|
-
export { type
|
|
713
|
+
export { type TableOptions as $, type AnyTableBuilder as A, baseSystemMigrations as B, type CrdtStorage as C, type DeSyncDetectedReason as D, type ExecuteResult as E, createSystemDbConfig as F, runSystemMigrations as G, type HLC as H, type InternalSQLiteWrapper as I, type SystemDbConfig as J, type KyselyQueryFactory as K, type Logger as L, type Migrations as M, type NewCrdtEvent as N, type SystemMigration as O, type SystemMigrationContext as P, type QueryBuilderOutput as Q, type ReadonlyTable as R, type StoredValue as S, TypedEvent as T, type AnyColumnBuilder as U, ColumnBuilder as V, type WorkerState as W, type ColumnKind as X, type ColumnMeta as Y, type PayloadValidationResult as Z, type SqliteStorageType as _, type CrdtTableConfig as a, t as a0, createCrdtApplyFunction as a1, createSQLiteCrdtApplyFunction as a2, type PendingCrdtEvent as a3, createCrdtStorage as a4, type OwnCrdtEvent as a5, type CrdtSyncRemoteSource as a6, createCrdtSyncRemoteSource as a7, type EventsPullRequest as a8, type EventsPushRequest as a9, type GetEventsBatch as aA, type EventsPullResponse as aB, type EventsPushResponse as aa, CRDT_EVENT_NO_OP_PAYLOAD as ab, type CrdtEventOrigin as ac, type CrdtEventStatus as ad, type CrdtEventType as ae, type CrdtUpdateLogItem as af, type CrdtUpdateLogPayload as ag, isNoOpCrdtEventPayload as ah, type PersistedCrdtEvent as ai, createStoredValue as aj, type KyselyStatementFactory as ak, type PreparedStatement as al, createDeferredPromise as am, createTypedEventTarget as an, type DeferredPromise as ao, type DistributiveOmit as ap, generateId as aq, jsonSafeParse as ar, quoteId as as, TypedBroadcastChannel as at, type TypedEventTarget as au, tryCatch as av, tryCatchAsync as aw, type WorkerConfig as ax, type WorkerNotificationMessage as ay, type CreateRemoteSourceFactory as az, SQLiteDbWrapper as b, type SyncSchemaTables as c, type SyncDbSchema as d, TableBuilder as e, type TableColumns as f, type InferRow as g, type ExecuteParams as h, type SQLiteTransactionWrapper as i, compareHLC as j, deserializeHLC as k, HLCCounter as l, type ColumnMetadata as m, type DatabaseIntrospection as n, introspectDb as o, type TableMetadata as p, type LogLevel as q, startPerformanceLogger as r, serializeHLC as s, createMigrations as t, createMigrator as u, type MigratableEvent as v, type MigrationsDb as w, type SyncDbMigrator as x, applyMemoryDbSchema as y, applyWorkerDbSchema as z };
|