@sqlite-sync/core 0.4.0 → 0.4.2
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/README.md +81 -0
- package/dist/{crdt-sync-remote-source-DyEELVsx.d.ts → crdt-sync-remote-source-Bv39_fZl.d.ts} +21 -1
- package/dist/index.d.ts +4 -4
- package/dist/{reset-state-BeduI9vB.d.ts → reset-state-BR4Zs-Jy.d.ts} +1 -1
- package/dist/server.d.ts +2 -2
- package/dist/worker.d.ts +3 -3
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# @sqlite-sync/core
|
|
2
|
+
|
|
3
|
+
Core sync engine for [sqlite-sync](https://github.com/krolebord-dev/sqlite-sync) — a local-first SQLite sync engine for web apps, with reactive queries, offline persistence, and CRDT-based replication.
|
|
4
|
+
|
|
5
|
+
This package provides:
|
|
6
|
+
|
|
7
|
+
- `createSyncedDb()` for client orchestration (worker attach, snapshot hydration, sync state).
|
|
8
|
+
- The schema builder (`createSyncDbSchema`) and migrations (`createMigrations`).
|
|
9
|
+
- Live query primitives (`db.createLiveQuery(...)`), typed through [Kysely](https://kysely.dev/).
|
|
10
|
+
- CRDT primitives with Last-Write-Wins per-field replication and HLC timestamps.
|
|
11
|
+
- The worker runtime (`@sqlite-sync/core/worker`) and server protocol types (`@sqlite-sync/core/server`).
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm add @sqlite-sync/core kysely
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
For React bindings, add [`@sqlite-sync/react`](https://www.npmjs.com/package/@sqlite-sync/react).
|
|
20
|
+
|
|
21
|
+
## Quick start
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { createMigrations, createSyncDbSchema, createSyncedDb } from "@sqlite-sync/core";
|
|
25
|
+
|
|
26
|
+
type Todo = {
|
|
27
|
+
id: string;
|
|
28
|
+
title: string;
|
|
29
|
+
completed: boolean;
|
|
30
|
+
tombstone?: boolean;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const migrations = createMigrations((b) => ({
|
|
34
|
+
0: [
|
|
35
|
+
b.createTable("_todo", (t) =>
|
|
36
|
+
t
|
|
37
|
+
.addColumn("id", "text", (col) => col.primaryKey().notNull())
|
|
38
|
+
.addColumn("title", "text", (col) => col.notNull())
|
|
39
|
+
.addColumn("completed", "boolean", (col) => col.notNull().defaultTo(false))
|
|
40
|
+
.addColumn("tombstone", "boolean", (col) => col.notNull().defaultTo(false)),
|
|
41
|
+
),
|
|
42
|
+
],
|
|
43
|
+
}));
|
|
44
|
+
|
|
45
|
+
export const syncDbSchema = createSyncDbSchema({ migrations })
|
|
46
|
+
.addTable<Todo>()
|
|
47
|
+
.withConfig({ baseTableName: "_todo", crdtTableName: "todo" })
|
|
48
|
+
.build();
|
|
49
|
+
|
|
50
|
+
const worker = new Worker(new URL("./db-worker.ts", import.meta.url), { type: "module" });
|
|
51
|
+
|
|
52
|
+
export const db = await createSyncedDb({
|
|
53
|
+
dbId: "app-db",
|
|
54
|
+
worker,
|
|
55
|
+
workerProps: undefined,
|
|
56
|
+
syncDbSchema,
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Start the worker (remote sync optional):
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
// db-worker.ts
|
|
64
|
+
import { startDbWorker } from "@sqlite-sync/core/worker";
|
|
65
|
+
import { syncDbSchema } from "./db-schema";
|
|
66
|
+
|
|
67
|
+
await startDbWorker({ syncDbSchema });
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Requirements
|
|
71
|
+
|
|
72
|
+
- Browser: Web Workers + Web Locks + an OPFS-capable SQLite WASM environment.
|
|
73
|
+
- Peer dependency: `kysely` (`^0.28.0 || ^0.29.0`).
|
|
74
|
+
|
|
75
|
+
## Documentation
|
|
76
|
+
|
|
77
|
+
See the [full documentation](https://github.com/krolebord-dev/sqlite-sync/blob/main/docs.md) and the [project README](https://github.com/krolebord-dev/sqlite-sync) for guides, recovery/storage versioning, and the Cloudflare sync backend.
|
|
78
|
+
|
|
79
|
+
## License
|
|
80
|
+
|
|
81
|
+
MIT
|
package/dist/{crdt-sync-remote-source-DyEELVsx.d.ts → crdt-sync-remote-source-Bv39_fZl.d.ts}
RENAMED
|
@@ -452,6 +452,26 @@ declare function createCrdtStorage(storage: DbSyncerStorage): {
|
|
|
452
452
|
}[K]>) => void) => void;
|
|
453
453
|
};
|
|
454
454
|
|
|
455
|
+
type WorkerNotificationMessage = {
|
|
456
|
+
notificationType: "new-event-chunk-applied";
|
|
457
|
+
newSyncId: number;
|
|
458
|
+
/** Worker's HLC checksum after applying up to `newSyncId`. */
|
|
459
|
+
eventHlcSum: string | null;
|
|
460
|
+
} | {
|
|
461
|
+
notificationType: "state-changed";
|
|
462
|
+
state: WorkerState;
|
|
463
|
+
} | {
|
|
464
|
+
notificationType: "reload-requested";
|
|
465
|
+
reloadEpoch: string;
|
|
466
|
+
clean: boolean;
|
|
467
|
+
} | {
|
|
468
|
+
notificationType: "de-sync-detected";
|
|
469
|
+
reason: DeSyncDetectedReason;
|
|
470
|
+
} | {
|
|
471
|
+
notificationType: "remote-schema-version-mismatch";
|
|
472
|
+
remoteSchemaVersion: number;
|
|
473
|
+
localSchemaVersion: number;
|
|
474
|
+
};
|
|
455
475
|
type WorkerState = {
|
|
456
476
|
remoteState: "online" | "offline" | "pending";
|
|
457
477
|
deSynced: boolean;
|
|
@@ -569,4 +589,4 @@ declare const createCrdtSyncRemoteSource: ({ bufferSize, storage, migrator, pull
|
|
|
569
589
|
}[K]>) => void) => void;
|
|
570
590
|
};
|
|
571
591
|
|
|
572
|
-
export { type PersistedCrdtEvent as $, createSQLiteCrdtApplyFunction as A, createCrdtStorage as B, type CrdtStorage as C, type DeSyncDetectedReason as D, type ExecuteParams as E, type CrdtSyncRemoteSource as F, createCrdtSyncRemoteSource as G, type HLC as H, type InternalSQLiteWrapper as I, type EventsPullRequest as J, type KyselyQueryFactory as K, type Logger as L, type MigratableEvent as M, type EventsPushRequest as N, type EventsPushResponse as O, type PendingCrdtEvent as P, type QueryBuilderOutput as Q, CRDT_EVENT_NO_OP_PAYLOAD as R, SQLiteDbWrapper as S, TypedEvent as T, type CrdtEventOrigin as U, type CrdtEventStatus as V, type WorkerState as W, type CrdtEventType as X, type CrdtUpdateLogItem as Y, type CrdtUpdateLogPayload as Z, isNoOpCrdtEventPayload as _, type StoredValue as a, createStoredValue as a0, type KyselyStatementFactory as a1, type PreparedStatement as a2, createDeferredPromise as a3, createTypedEventTarget as a4, type DeferredPromise as a5, type DistributiveOmit as a6, generateId as a7, jsonSafeParse as a8, quoteId as a9, TypedBroadcastChannel as aa, type TypedEventTarget as ab, tryCatch as ac, tryCatchAsync as ad, type WorkerConfig as ae, type
|
|
592
|
+
export { type PersistedCrdtEvent as $, createSQLiteCrdtApplyFunction as A, createCrdtStorage as B, type CrdtStorage as C, type DeSyncDetectedReason as D, type ExecuteParams as E, type CrdtSyncRemoteSource as F, createCrdtSyncRemoteSource as G, type HLC as H, type InternalSQLiteWrapper as I, type EventsPullRequest as J, type KyselyQueryFactory as K, type Logger as L, type MigratableEvent as M, type EventsPushRequest as N, type EventsPushResponse as O, type PendingCrdtEvent as P, type QueryBuilderOutput as Q, CRDT_EVENT_NO_OP_PAYLOAD as R, SQLiteDbWrapper as S, TypedEvent as T, type CrdtEventOrigin as U, type CrdtEventStatus as V, type WorkerState as W, type CrdtEventType as X, type CrdtUpdateLogItem as Y, type CrdtUpdateLogPayload as Z, isNoOpCrdtEventPayload as _, type StoredValue as a, createStoredValue as a0, type KyselyStatementFactory as a1, type PreparedStatement as a2, createDeferredPromise as a3, createTypedEventTarget as a4, type DeferredPromise as a5, type DistributiveOmit as a6, generateId as a7, jsonSafeParse as a8, quoteId as a9, TypedBroadcastChannel as aa, type TypedEventTarget as ab, tryCatch as ac, tryCatchAsync as ad, type WorkerConfig as ae, type WorkerNotificationMessage as af, type CreateRemoteSourceFactory as ag, type GetEventsBatch as ah, type EventsPullResponse as ai, type ExecuteResult as b, type SQLiteTransactionWrapper as c, compareHLC as d, deserializeHLC as e, HLCCounter as f, type DatabaseIntrospection as g, type TableMetadata as h, introspectDb as i, type LogLevel as j, startPerformanceLogger as k, createMigrations as l, createMigrator as m, type Migrations as n, type MigrationsDb as o, type SyncDbMigrator as p, applyMemoryDbSchema as q, applyWorkerDbSchema as r, serializeHLC as s, baseSystemMigrations as t, createSystemDbConfig as u, runSystemMigrations as v, type SystemDbConfig as w, type SystemMigration as x, type SystemMigrationContext as y, createCrdtApplyFunction as z };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import * as kysely from 'kysely';
|
|
2
2
|
import { Kysely, SchemaModule } from 'kysely';
|
|
3
|
-
import { S as SQLiteDbWrapper, L as Logger, T as TypedEvent, I as InternalSQLiteWrapper, a as StoredValue, C as CrdtStorage, E as ExecuteParams, b as ExecuteResult, Q as QueryBuilderOutput, K as KyselyQueryFactory, c as SQLiteTransactionWrapper, W as WorkerState, D as DeSyncDetectedReason } from './crdt-sync-remote-source-
|
|
4
|
-
export { R as CRDT_EVENT_NO_OP_PAYLOAD, U as CrdtEventOrigin, V as CrdtEventStatus, X as CrdtEventType, F as CrdtSyncRemoteSource, Y as CrdtUpdateLogItem, Z as CrdtUpdateLogPayload, g as DatabaseIntrospection, a5 as DeferredPromise, a6 as DistributiveOmit, J as EventsPullRequest, N as EventsPushRequest, O as EventsPushResponse, H as HLC, f as HLCCounter, a1 as KyselyStatementFactory, j as LogLevel, M as MigratableEvent, n as Migrations, o as MigrationsDb, P as PendingCrdtEvent, $ as PersistedCrdtEvent, a2 as PreparedStatement, p as SyncDbMigrator, w as SystemDbConfig, x as SystemMigration, y as SystemMigrationContext, h as TableMetadata, aa as TypedBroadcastChannel, ab as TypedEventTarget, ae as WorkerConfig, q as applyMemoryDbSchema, r as applyWorkerDbSchema, t as baseSystemMigrations, d as compareHLC, z as createCrdtApplyFunction, B as createCrdtStorage, G as createCrdtSyncRemoteSource, a3 as createDeferredPromise, l as createMigrations, m as createMigrator, A as createSQLiteCrdtApplyFunction, a0 as createStoredValue, u as createSystemDbConfig, a4 as createTypedEventTarget, e as deserializeHLC, a7 as generateId, i as introspectDb, _ as isNoOpCrdtEventPayload, a8 as jsonSafeParse, a9 as quoteId, v as runSystemMigrations, s as serializeHLC, k as startPerformanceLogger, ac as tryCatch, ad as tryCatchAsync } from './crdt-sync-remote-source-
|
|
5
|
-
import { S as SyncDbSchema, C as CrdtTableConfig } from './reset-state-
|
|
6
|
-
export { a as CreateCrdtSchemaOptions, R as RESET_REQUEST_TTL_MS, b as ResetRequest, d as ResetStore, c as createSyncDbSchema } from './reset-state-
|
|
3
|
+
import { S as SQLiteDbWrapper, L as Logger, T as TypedEvent, I as InternalSQLiteWrapper, a as StoredValue, C as CrdtStorage, E as ExecuteParams, b as ExecuteResult, Q as QueryBuilderOutput, K as KyselyQueryFactory, c as SQLiteTransactionWrapper, W as WorkerState, D as DeSyncDetectedReason } from './crdt-sync-remote-source-Bv39_fZl.js';
|
|
4
|
+
export { R as CRDT_EVENT_NO_OP_PAYLOAD, U as CrdtEventOrigin, V as CrdtEventStatus, X as CrdtEventType, F as CrdtSyncRemoteSource, Y as CrdtUpdateLogItem, Z as CrdtUpdateLogPayload, g as DatabaseIntrospection, a5 as DeferredPromise, a6 as DistributiveOmit, J as EventsPullRequest, N as EventsPushRequest, O as EventsPushResponse, H as HLC, f as HLCCounter, a1 as KyselyStatementFactory, j as LogLevel, M as MigratableEvent, n as Migrations, o as MigrationsDb, P as PendingCrdtEvent, $ as PersistedCrdtEvent, a2 as PreparedStatement, p as SyncDbMigrator, w as SystemDbConfig, x as SystemMigration, y as SystemMigrationContext, h as TableMetadata, aa as TypedBroadcastChannel, ab as TypedEventTarget, ae as WorkerConfig, af as WorkerNotificationMessage, q as applyMemoryDbSchema, r as applyWorkerDbSchema, t as baseSystemMigrations, d as compareHLC, z as createCrdtApplyFunction, B as createCrdtStorage, G as createCrdtSyncRemoteSource, a3 as createDeferredPromise, l as createMigrations, m as createMigrator, A as createSQLiteCrdtApplyFunction, a0 as createStoredValue, u as createSystemDbConfig, a4 as createTypedEventTarget, e as deserializeHLC, a7 as generateId, i as introspectDb, _ as isNoOpCrdtEventPayload, a8 as jsonSafeParse, a9 as quoteId, v as runSystemMigrations, s as serializeHLC, k as startPerformanceLogger, ac as tryCatch, ad as tryCatchAsync } from './crdt-sync-remote-source-Bv39_fZl.js';
|
|
5
|
+
import { S as SyncDbSchema, C as CrdtTableConfig } from './reset-state-BR4Zs-Jy.js';
|
|
6
|
+
export { a as CreateCrdtSchemaOptions, R as RESET_REQUEST_TTL_MS, b as ResetRequest, d as ResetStore, c as createSyncDbSchema } from './reset-state-BR4Zs-Jy.js';
|
|
7
7
|
import '@sqlite.org/sqlite-wasm';
|
|
8
8
|
|
|
9
9
|
declare const dummyKysely: Kysely<any>;
|
package/dist/server.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { a8 as jsonSafeParse } from './crdt-sync-remote-source-
|
|
1
|
+
import { ai as EventsPullResponse, O as EventsPushResponse } from './crdt-sync-remote-source-Bv39_fZl.js';
|
|
2
|
+
export { a8 as jsonSafeParse } from './crdt-sync-remote-source-Bv39_fZl.js';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
import 'kysely';
|
|
5
5
|
import '@sqlite.org/sqlite-wasm';
|
package/dist/worker.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { J as EventsPullRequest, N as EventsPushRequest, O as EventsPushResponse,
|
|
3
|
-
import { S as SyncDbSchema, d as ResetStore } from './reset-state-
|
|
1
|
+
import { ag as CreateRemoteSourceFactory, ae as WorkerConfig, L as Logger } from './crdt-sync-remote-source-Bv39_fZl.js';
|
|
2
|
+
export { J as EventsPullRequest, N as EventsPushRequest, O as EventsPushResponse, ah as GetEventsBatch } from './crdt-sync-remote-source-Bv39_fZl.js';
|
|
3
|
+
import { S as SyncDbSchema, d as ResetStore } from './reset-state-BR4Zs-Jy.js';
|
|
4
4
|
import 'kysely';
|
|
5
5
|
import '@sqlite.org/sqlite-wasm';
|
|
6
6
|
|