@firtoz/drizzle-sqlite-wasm 1.1.2 → 2.0.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.
Files changed (41) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/README.md +21 -4
  3. package/dist/chunk-7TDQNWT6.js +181 -0
  4. package/dist/chunk-7TDQNWT6.js.map +1 -0
  5. package/dist/{chunk-FRONXNEA.js → chunk-BQBL6E44.js} +2 -2
  6. package/dist/chunk-BQBL6E44.js.map +1 -0
  7. package/dist/{chunk-H2F2HZ2A.js → chunk-EQOHJW3Q.js} +3 -3
  8. package/dist/{chunk-H2F2HZ2A.js.map → chunk-EQOHJW3Q.js.map} +1 -1
  9. package/dist/{chunk-7JJHY44Q.js → chunk-NNPU7YTX.js} +3 -3
  10. package/dist/{chunk-7JJHY44Q.js.map → chunk-NNPU7YTX.js.map} +1 -1
  11. package/dist/{chunk-WFFFP6DB.js → chunk-QOFRLODK.js} +74 -19
  12. package/dist/chunk-QOFRLODK.js.map +1 -0
  13. package/dist/{chunk-BZVMUTJ7.js → chunk-VLFDMAFH.js} +2 -2
  14. package/dist/chunk-VLFDMAFH.js.map +1 -0
  15. package/dist/collections/sqlite-collection.d.ts +0 -1
  16. package/dist/collections/sqlite-collection.js +1 -1
  17. package/dist/collections/synced-sqlite-collection.js +2 -2
  18. package/dist/context/DrizzleSqliteProvider.d.ts +46 -4
  19. package/dist/context/DrizzleSqliteProvider.js +4 -4
  20. package/dist/context/useDrizzleSqlite.d.ts +4 -1
  21. package/dist/context/useDrizzleSqlite.js +5 -5
  22. package/dist/drizzle/worker.js +1 -1
  23. package/dist/hooks/useDrizzleSqliteDb.d.ts +35 -7
  24. package/dist/hooks/useDrizzleSqliteDb.js +2 -2
  25. package/dist/index.d.ts +3 -3
  26. package/dist/index.js +6 -7
  27. package/dist/index.js.map +1 -1
  28. package/dist/worker/schema.d.ts +4 -4
  29. package/dist/worker/sqlite.worker.js +1 -1
  30. package/dist/worker/sqlite.worker.js.map +1 -1
  31. package/package.json +5 -5
  32. package/src/collections/sqlite-collection.ts +0 -2
  33. package/src/drizzle/worker.ts +1 -4
  34. package/src/hooks/useDrizzleSqliteDb.ts +133 -15
  35. package/src/index.ts +9 -12
  36. package/src/worker/sqlite.worker.ts +4 -1
  37. package/dist/chunk-AEYHRJVN.js +0 -130
  38. package/dist/chunk-AEYHRJVN.js.map +0 -1
  39. package/dist/chunk-BZVMUTJ7.js.map +0 -1
  40. package/dist/chunk-FRONXNEA.js.map +0 -1
  41. package/dist/chunk-WFFFP6DB.js.map +0 -1
@@ -1,24 +1,39 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import * as react from 'react';
3
- import { PropsWithChildren } from 'react';
3
+ import { PropsWithChildren, ReactNode } from 'react';
4
4
  import { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
5
5
  import { Collection, InferSchemaOutput } from '@tanstack/db';
6
6
  import { ValidTableNames } from '../collections/sqlite-collection.js';
7
+ import { GetTableFromSchema, IdOf, SQLInterceptor, InferCollectionFromTable } from '@firtoz/drizzle-utils';
7
8
  import { DurableSqliteMigrationConfig } from '../migration/migrator.js';
8
9
  import { SqliteWasmWorkerOpenOptions } from '../worker/sqlite-open-options.js';
9
- import { GetTableFromSchema, IdOf, SQLInterceptor, InferCollectionFromTable } from '@firtoz/drizzle-utils';
10
+ import { ISqliteWorkerClient } from '../worker/manager.js';
10
11
  import 'drizzle-orm';
11
12
  import 'drizzle-orm/sqlite-core';
12
13
  import '@firtoz/db-helpers';
13
14
  import 'drizzle-orm/durable-sqlite/migrator';
14
15
  import 'zod';
16
+ import '@firtoz/worker-helper/WorkerClient';
17
+ import '../worker/schema.js';
15
18
 
16
19
  type SqliteCollection<TSchema extends Record<string, unknown>, TTableName extends string & ValidTableNames<TSchema>> = Collection<InferSchemaOutput<GetTableFromSchema<TSchema, TTableName>["$inferSelect"]>, IdOf<GetTableFromSchema<TSchema, TTableName>>, any, any, Omit<GetTableFromSchema<TSchema, TTableName>["$inferInsert"], "id"> & {
17
20
  id?: IdOf<GetTableFromSchema<TSchema, TTableName>>;
18
21
  }>;
22
+ /**
23
+ * Exposed only when `sessionStatus === "ready"`. `useDrizzleSqlite` and
24
+ * `useSqliteCollection` require this value — they must run under the
25
+ * ready subtree of {@link DrizzleSqliteProvider} (i.e. not during loading fallback).
26
+ */
19
27
  type DrizzleSqliteContextValue<TSchema extends Record<string, unknown>> = {
20
28
  drizzle: SqliteRemoteDatabase<TSchema>;
21
29
  readyPromise: Promise<void>;
30
+ /**
31
+ * Worker client for this DB. Present whenever the ready subtree is mounted
32
+ * (including SSR no-op client).
33
+ */
34
+ sqliteClient: ISqliteWorkerClient;
35
+ /** Bumps when collection caches must be discarded (drizzle/options identity change). */
36
+ collectionCacheEpoch: number;
22
37
  getCollection: <TTableName extends string & ValidTableNames<TSchema>>(tableName: TTableName) => SqliteCollection<TSchema, TTableName>;
23
38
  incrementRefCount: (tableName: string) => void;
24
39
  decrementRefCount: (tableName: string) => void;
@@ -26,25 +41,52 @@ type DrizzleSqliteContextValue<TSchema extends Record<string, unknown>> = {
26
41
  declare const DrizzleSqliteContext: react.Context<DrizzleSqliteContextValue<any> | null>;
27
42
  type DrizzleSqliteProviderProps<TSchema extends Record<string, unknown>> = PropsWithChildren<{
28
43
  worker: new () => Worker;
44
+ /**
45
+ * File / logical name for the OPFS (or in-memory) DB. The ready subtree remounts when
46
+ * this or `workerOpenOptions` (serialized) changes, so in-tree state resets for a
47
+ * new file/session.
48
+ */
29
49
  dbName: string;
30
50
  schema: TSchema;
31
51
  migrations: DurableSqliteMigrationConfig;
32
52
  debug?: boolean;
33
53
  enableCheckpoint?: boolean;
54
+ /**
55
+ * Shown on the client while the worker is starting and migrations are running
56
+ * (`sessionStatus` is not `"ready"`). Not shown for SSR (session is `ready` with a no-op client).
57
+ */
58
+ loadingFallback: ReactNode;
59
+ /**
60
+ * Optional UI when migrations fail. Defaults to a short error message in development.
61
+ */
62
+ errorFallback?: ReactNode;
34
63
  /**
35
64
  * Sync mode: 'eager' (immediate) or 'on-demand' (lazy)
36
65
  */
37
66
  syncMode?: "eager" | "on-demand";
38
67
  /**
39
- * Optional interceptor for tracking SQLite operations (for testing/debugging)
68
+ * Optional interceptor for tracking SQLite operations (for testing/debugging).
69
+ * Inline `{ onOperation }` objects are fine: the provider keeps a stable wrapper so
70
+ * collection caches and context are not invalidated on every parent re-render. You
71
+ * can still pass a module-level or `useMemo`d object if you prefer.
40
72
  */
41
73
  interceptor?: SQLInterceptor;
42
74
  /**
43
75
  * Worker DB pragmas on first open of `dbName` this session (see `useDrizzleSqliteDb`).
76
+ * If this changes for the same `dbName`, the global worker may still return the existing
77
+ * open DB — use a distinct `dbName` or remount the worker session if you need new pragmas.
44
78
  */
45
79
  workerOpenOptions?: SqliteWasmWorkerOpenOptions;
46
80
  }>;
47
- declare function DrizzleSqliteProvider<TSchema extends Record<string, unknown>>({ children, worker, dbName, schema, migrations, debug, enableCheckpoint, syncMode, interceptor, workerOpenOptions, }: DrizzleSqliteProviderProps<TSchema>): react_jsx_runtime.JSX.Element;
81
+ /**
82
+ * Provides a single SQLite+Worker session for `dbName`. Children that call
83
+ * {@link useDrizzleSqlite} or {@link useSqliteCollection} are only mounted
84
+ * after migrations succeed (see `loadingFallback` while not ready).
85
+ *
86
+ * **Session identity** is `dbName` + `workerOpenOptions` (the ready subtree `key` is derived
87
+ * internally; you do not need to set `key` on this component for normal DB switching).
88
+ */
89
+ declare function DrizzleSqliteProvider<TSchema extends Record<string, unknown>>({ children, worker, dbName, schema, migrations, debug, enableCheckpoint, loadingFallback, errorFallback, syncMode, interceptor, workerOpenOptions, }: DrizzleSqliteProviderProps<TSchema>): react_jsx_runtime.JSX.Element;
48
90
  declare function useSqliteCollection<TSchema extends Record<string, unknown>, TTableName extends string & ValidTableNames<TSchema>>(context: DrizzleSqliteContextValue<TSchema>, tableName: TTableName): InferCollectionFromTable<GetTableFromSchema<TSchema, TTableName>>;
49
91
 
50
92
  export { DrizzleSqliteContext, type DrizzleSqliteContextValue, DrizzleSqliteProvider, useSqliteCollection };
@@ -1,11 +1,11 @@
1
- export { DrizzleSqliteContext, DrizzleSqliteProvider, useSqliteCollection } from '../chunk-AEYHRJVN.js';
2
- import '../chunk-WFFFP6DB.js';
1
+ export { DrizzleSqliteContext, DrizzleSqliteProvider, useSqliteCollection } from '../chunk-7TDQNWT6.js';
2
+ import '../chunk-QOFRLODK.js';
3
3
  import '../chunk-NSPVPJKE.js';
4
4
  import '../chunk-FIVEPVOM.js';
5
5
  import '../chunk-AGMKOHXO.js';
6
6
  import '../chunk-GVUNHU6J.js';
7
7
  import '../chunk-6LNYKOKR.js';
8
- import '../chunk-BZVMUTJ7.js';
9
- import '../chunk-FRONXNEA.js';
8
+ import '../chunk-VLFDMAFH.js';
9
+ import '../chunk-BQBL6E44.js';
10
10
  //# sourceMappingURL=DrizzleSqliteProvider.js.map
11
11
  //# sourceMappingURL=DrizzleSqliteProvider.js.map
@@ -4,11 +4,14 @@ import 'react/jsx-runtime';
4
4
  import 'react';
5
5
  import 'drizzle-orm/sqlite-proxy';
6
6
  import '@tanstack/db';
7
+ import '@firtoz/drizzle-utils';
7
8
  import '../migration/migrator.js';
8
9
  import 'drizzle-orm/durable-sqlite/migrator';
9
10
  import '../worker/sqlite-open-options.js';
10
11
  import 'zod';
11
- import '@firtoz/drizzle-utils';
12
+ import '../worker/manager.js';
13
+ import '@firtoz/worker-helper/WorkerClient';
14
+ import '../worker/schema.js';
12
15
  import 'drizzle-orm';
13
16
  import 'drizzle-orm/sqlite-core';
14
17
  import '@firtoz/db-helpers';
@@ -1,12 +1,12 @@
1
- export { useDrizzleSqlite } from '../chunk-H2F2HZ2A.js';
2
- import '../chunk-AEYHRJVN.js';
3
- import '../chunk-WFFFP6DB.js';
1
+ export { useDrizzleSqlite } from '../chunk-EQOHJW3Q.js';
2
+ import '../chunk-7TDQNWT6.js';
3
+ import '../chunk-QOFRLODK.js';
4
4
  import '../chunk-NSPVPJKE.js';
5
5
  import '../chunk-FIVEPVOM.js';
6
6
  import '../chunk-AGMKOHXO.js';
7
7
  import '../chunk-GVUNHU6J.js';
8
8
  import '../chunk-6LNYKOKR.js';
9
- import '../chunk-BZVMUTJ7.js';
10
- import '../chunk-FRONXNEA.js';
9
+ import '../chunk-VLFDMAFH.js';
10
+ import '../chunk-BQBL6E44.js';
11
11
  //# sourceMappingURL=useDrizzleSqlite.js.map
12
12
  //# sourceMappingURL=useDrizzleSqlite.js.map
@@ -1,3 +1,3 @@
1
- export { createInstrumentedDrizzle, drizzleSqliteWasmWorker } from '../chunk-FRONXNEA.js';
1
+ export { createInstrumentedDrizzle, drizzleSqliteWasmWorker } from '../chunk-BQBL6E44.js';
2
2
  //# sourceMappingURL=worker.js.map
3
3
  //# sourceMappingURL=worker.js.map
@@ -1,4 +1,4 @@
1
- import * as drizzle_orm_sqlite_proxy from 'drizzle-orm/sqlite-proxy';
1
+ import { SqliteRemoteDatabase } from 'drizzle-orm/sqlite-proxy';
2
2
  import { DurableSqliteMigrationConfig } from '../migration/migrator.js';
3
3
  import { ISqliteWorkerClient } from '../worker/manager.js';
4
4
  import { SQLInterceptor } from '@firtoz/drizzle-utils';
@@ -8,6 +8,38 @@ import '@firtoz/worker-helper/WorkerClient';
8
8
  import '../worker/schema.js';
9
9
  import 'zod';
10
10
 
11
+ /**
12
+ * `connecting` — no worker client yet, or migrations not finished.
13
+ * `ready` — migrations applied, `readyPromise` resolved.
14
+ * `error` — migration (or init) failed; see `sessionError`.
15
+ */
16
+ type DrizzleSqliteSessionStatus = "connecting" | "ready" | "error";
17
+ /**
18
+ * Error payload when the hook reports `sessionStatus: "error"`.
19
+ * Add further `| { kind: … }` members later; discriminate on `kind` in UI or logging.
20
+ */
21
+ type DrizzleSqliteSessionError = {
22
+ kind: "migration_failed";
23
+ /** String for display or logs */
24
+ message: string;
25
+ /** The value that was thrown (often an `Error`) */
26
+ original: unknown;
27
+ };
28
+ /** Normalises `catch` bindings so UI can rely on a stable shape. */
29
+ declare function toDrizzleSqliteSessionError(caught: unknown): DrizzleSqliteSessionError;
30
+ type UseDrizzleSqliteDbResult<TSchema extends Record<string, unknown>> = {
31
+ drizzle: SqliteRemoteDatabase<TSchema>;
32
+ readyPromise: Promise<void>;
33
+ sqliteClient: ISqliteWorkerClient | null;
34
+ sessionStatus: "connecting" | "ready";
35
+ sessionError: null;
36
+ } | {
37
+ drizzle: SqliteRemoteDatabase<TSchema>;
38
+ readyPromise: Promise<void>;
39
+ sqliteClient: ISqliteWorkerClient | null;
40
+ sessionStatus: "error";
41
+ sessionError: DrizzleSqliteSessionError;
42
+ };
11
43
  declare const useDrizzleSqliteDb: <TSchema extends Record<string, unknown>>(WorkerConstructor: new () => Worker, dbName: string, schema: TSchema, migrations: DurableSqliteMigrationConfig, debug?: boolean,
12
44
  /** Optional interceptor to log ALL SQL queries (including direct Drizzle queries) */
13
45
  interceptor?: SQLInterceptor,
@@ -15,10 +47,6 @@ interceptor?: SQLInterceptor,
15
47
  * Pragmas applied when the worker first opens this `dbName` in the session.
16
48
  * Ignored if that database was already started (same global worker + dbName).
17
49
  */
18
- workerOpenOptions?: SqliteWasmWorkerOpenOptions) => {
19
- drizzle: drizzle_orm_sqlite_proxy.SqliteRemoteDatabase<TSchema>;
20
- readyPromise: Promise<void>;
21
- sqliteClient: ISqliteWorkerClient | null;
22
- };
50
+ workerOpenOptions?: SqliteWasmWorkerOpenOptions) => UseDrizzleSqliteDbResult<TSchema>;
23
51
 
24
- export { useDrizzleSqliteDb };
52
+ export { type DrizzleSqliteSessionError, type DrizzleSqliteSessionStatus, type UseDrizzleSqliteDbResult, toDrizzleSqliteSessionError, useDrizzleSqliteDb };
@@ -1,9 +1,9 @@
1
- export { useDrizzleSqliteDb } from '../chunk-WFFFP6DB.js';
1
+ export { toDrizzleSqliteSessionError, useDrizzleSqliteDb } from '../chunk-QOFRLODK.js';
2
2
  import '../chunk-NSPVPJKE.js';
3
3
  import '../chunk-FIVEPVOM.js';
4
4
  import '../chunk-AGMKOHXO.js';
5
5
  import '../chunk-GVUNHU6J.js';
6
6
  import '../chunk-6LNYKOKR.js';
7
- import '../chunk-FRONXNEA.js';
7
+ import '../chunk-BQBL6E44.js';
8
8
  //# sourceMappingURL=useDrizzleSqliteDb.js.map
9
9
  //# sourceMappingURL=useDrizzleSqliteDb.js.map
package/dist/index.d.ts CHANGED
@@ -1,8 +1,7 @@
1
1
  export { drizzleSqliteWasm } from './drizzle/direct.js';
2
- export { SqliteCollectionConfig, sqliteCollectionOptions as drizzleCollectionOptions } from './collections/sqlite-collection.js';
2
+ export { SqliteCollectionConfig, sqliteCollectionOptions } from './collections/sqlite-collection.js';
3
3
  export { createSyncedSqliteCollection } from './collections/synced-sqlite-collection.js';
4
- export { Branded, IdOf, InsertSchema, SQLInterceptor, SQLOperation, SelectSchema, TableId, makeId, syncableTable } from '@firtoz/drizzle-utils';
5
- export { useDrizzleSqliteDb } from './hooks/useDrizzleSqliteDb.js';
4
+ export { DrizzleSqliteSessionError, DrizzleSqliteSessionStatus, UseDrizzleSqliteDbResult, toDrizzleSqliteSessionError, useDrizzleSqliteDb } from './hooks/useDrizzleSqliteDb.js';
6
5
  export { DrizzleSqliteContext, DrizzleSqliteContextValue, DrizzleSqliteProvider, useSqliteCollection } from './context/DrizzleSqliteProvider.js';
7
6
  export { UseDrizzleSqliteReturn, useDrizzleSqlite } from './context/useDrizzleSqlite.js';
8
7
  export { getSqliteWorkerManager, initializeSqliteWorker, isSqliteWorkerInitialized, resetSqliteWorkerManager } from './worker/global-manager.js';
@@ -16,6 +15,7 @@ import 'drizzle-orm';
16
15
  import '@tanstack/db';
17
16
  import 'drizzle-orm/sqlite-core';
18
17
  import '@firtoz/db-helpers';
18
+ import '@firtoz/drizzle-utils';
19
19
  import '@firtoz/collection-sync';
20
20
  import 'react/jsx-runtime';
21
21
  import 'react';
package/dist/index.js CHANGED
@@ -1,16 +1,15 @@
1
- export { createSyncedSqliteCollection } from './chunk-7JJHY44Q.js';
2
- export { useDrizzleSqlite } from './chunk-H2F2HZ2A.js';
3
- export { DrizzleSqliteContext, DrizzleSqliteProvider, useSqliteCollection } from './chunk-AEYHRJVN.js';
4
- export { useDrizzleSqliteDb } from './chunk-WFFFP6DB.js';
1
+ export { createSyncedSqliteCollection } from './chunk-NNPU7YTX.js';
2
+ export { useDrizzleSqlite } from './chunk-EQOHJW3Q.js';
3
+ export { DrizzleSqliteContext, DrizzleSqliteProvider, useSqliteCollection } from './chunk-7TDQNWT6.js';
4
+ export { toDrizzleSqliteSessionError, useDrizzleSqliteDb } from './chunk-QOFRLODK.js';
5
5
  export { customSqliteMigrate } from './chunk-NSPVPJKE.js';
6
6
  export { getSqliteWorkerManager, initializeSqliteWorker, isSqliteWorkerInitialized, resetSqliteWorkerManager } from './chunk-FIVEPVOM.js';
7
7
  export { DbInstance, SqliteWorkerManager } from './chunk-AGMKOHXO.js';
8
8
  import './chunk-GVUNHU6J.js';
9
9
  export { SqliteWasmJournalModeSchema, SqliteWasmSynchronousModeSchema, SqliteWasmWorkerOpenOptionsSchema } from './chunk-6LNYKOKR.js';
10
- export { sqliteCollectionOptions as drizzleCollectionOptions } from './chunk-BZVMUTJ7.js';
10
+ export { sqliteCollectionOptions } from './chunk-VLFDMAFH.js';
11
11
  export { drizzleSqliteWasm } from './chunk-TZP4AIBR.js';
12
12
  import './chunk-BJDPMGFF.js';
13
- import './chunk-FRONXNEA.js';
14
- export { makeId, syncableTable } from '@firtoz/drizzle-utils';
13
+ import './chunk-BQBL6E44.js';
15
14
  //# sourceMappingURL=index.js.map
16
15
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":[],"names":[],"mappings":"","file":"index.js","sourcesContent":[]}
1
+ {"version":3,"sources":[],"names":[],"mappings":"","file":"index.js"}
@@ -28,10 +28,10 @@ declare const RemoteCallbackRequestSchema: z.ZodObject<{
28
28
  sql: z.ZodString;
29
29
  params: z.ZodArray<z.ZodAny>;
30
30
  method: z.ZodEnum<{
31
+ get: "get";
32
+ values: "values";
31
33
  run: "run";
32
34
  all: "all";
33
- values: "values";
34
- get: "get";
35
35
  }>;
36
36
  }, z.core.$strip>;
37
37
  type SqliteWorkerRemoteCallbackClientMessage = z.infer<typeof RemoteCallbackRequestSchema>;
@@ -67,10 +67,10 @@ declare const SqliteWorkerClientMessageSchema: z.ZodDiscriminatedUnion<[z.ZodObj
67
67
  sql: z.ZodString;
68
68
  params: z.ZodArray<z.ZodAny>;
69
69
  method: z.ZodEnum<{
70
+ get: "get";
71
+ values: "values";
70
72
  run: "run";
71
73
  all: "all";
72
- values: "values";
73
- get: "get";
74
74
  }>;
75
75
  }, z.core.$strip>, z.ZodObject<{
76
76
  type: z.ZodLiteral<SqliteWorkerClientMessageType.Checkpoint>;
@@ -105,7 +105,7 @@ var SqliteWorkerHelper = class extends WorkerHelper {
105
105
  const dbId = DbIdSchema.parse(crypto.randomUUID());
106
106
  const dbFileName = `${dbName}.sqlite3`;
107
107
  let db;
108
- if ("opfs" in sqlite3) {
108
+ if (typeof sqlite3.oo1.OpfsDb === "function") {
109
109
  db = new sqlite3.oo1.OpfsDb(dbFileName);
110
110
  this.log("OPFS database created:", db.filename);
111
111
  this.applyOpenPragmas(db, openOptions);
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/worker/sqlite.worker.ts"],"names":[],"mappings":";;;;;;AAoBA,IAAM,kBAAA,GAAN,cAAiC,YAAA,CAG/B;AAAA,EAUD,WAAA,GAAc;AACb,IAAA,KAAA,CAAM,IAAA,EAAM,iCAAiC,yBAAA,EAA2B;AAAA,MACvE,aAAA,EAAe,CAAC,IAAA,KAAS;AACxB,QAAA,IAAA,CAAK,eAAe,IAAI,CAAA;AAAA,MACzB,CAAA;AAAA,MACA,0BAAA,EAA4B,CAAC,KAAA,EAAO,YAAA,KAAiB;AACpD,QAAA,OAAA,CAAQ,KAAA,CAAM,wBAAA,EAA0B,EAAE,KAAA,EAAO,cA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AAAA,cACT,KAAA,EAAO,CAAA,0BAAA,EAA6B,IAAA,CAAK,IAAI,CAAA;AAAA,aAC7C,CAAA;AACD,YAAA;AAAA,UACD;AAGA,UAAA,MAAM,IAAA,CAAK,wBAAA,CAAyB,IAAA,EAAM,OAAA,CAAQ,EAAE,CAAA;AAAA,QACrD;AACA,QAAA;AAAA,MACD;AACC,QAAA,OAAO,gBAAgB,IAAI,CAAA;AAAA;AAC7B,EACD;AACD,CAAA;AAEA,IAAI,kBAAA,EAAmB","file":"sqlite.worker.js","sourcesContent":["import { WorkerHelper } from \"@firtoz/worker-helper\";\nimport {\n\tSqliteWorkerClientMessageSchema,\n\tSqliteWorkerClientMessageType,\n\tsqliteWorkerServerMessage,\n\tSqliteWorkerServerMessageType,\n\ttype SqliteWorkerClientMessage,\n\ttype SqliteWorkerServerMessage,\n\ttype StartRequestId,\n\tDbIdSchema,\n\ttype DbId,\n} from \"./schema\";\nimport { handleRemoteCallback } from \"../drizzle/handle-callback\";\nimport { exhaustiveGuard } from \"@firtoz/maybe-error\";\nimport type { SqliteWasmWorkerOpenOptions } from \"./sqlite-open-options\";\nimport type { Sqlite3Static, Database } from \"../types\";\n\n// Declare self as DedicatedWorkerGlobalScope for TypeScript\ndeclare var self: DedicatedWorkerGlobalScope;\n\nclass SqliteWorkerHelper extends WorkerHelper<\n\tSqliteWorkerClientMessage,\n\tSqliteWorkerServerMessage\n> {\n\tprivate initPromise: Promise<Sqlite3Static>;\n\tprivate databases = new Map<\n\t\tDbId,\n\t\t{\n\t\t\tdb: Database;\n\t\t\tinitialized: boolean;\n\t\t}\n\t>();\n\n\tconstructor() {\n\t\tsuper(self, SqliteWorkerClientMessageSchema, sqliteWorkerServerMessage, {\n\t\t\thandleMessage: (data) => {\n\t\t\t\tthis._handleMessage(data);\n\t\t\t},\n\t\t\thandleInputValidationError: (error, originalData) => {\n\t\t\t\tconsole.error(\"Input validation error\", { error, originalData });\n\t\t\t\tthrow new Error(`Invalid input: ${error.message}`);\n\t\t\t},\n\t\t\thandleOutputValidationError: (error, originalData) => {\n\t\t\t\tconsole.error(\"Output validation error\", { error, originalData });\n\t\t\t\tthrow new Error(`Invalid output: ${error.message}`);\n\t\t\t},\n\t\t\thandleProcessingError: (error, validatedData) => {\n\t\t\t\tconsole.error(\"Processing error\", { error, validatedData });\n\t\t\t\tthrow new Error(`Processing error: ${String(error)}`);\n\t\t\t},\n\t\t});\n\n\t\tthis.initPromise = import(\"@sqlite.org/sqlite-wasm\").then(\n\t\t\tasync ({ default: sqlite3InitModule }) => {\n\t\t\t\tconst result = await sqlite3InitModule();\n\n\t\t\t\treturn result;\n\t\t\t},\n\t\t);\n\n\t\tthis.send({\n\t\t\ttype: SqliteWorkerServerMessageType.Ready,\n\t\t});\n\t}\n\n\tprivate log(...args: unknown[]) {\n\t\tconsole.log(`[${new Date().toISOString()}]`, ...args);\n\t}\n\n\tprivate error(...args: unknown[]) {\n\t\tconsole.error(`[${new Date().toISOString()}]`, ...args);\n\t}\n\n\t// Helper method to process remote callback requests\n\tprivate async processRemoteCallbackRequest(\n\t\tdata: Extract<\n\t\t\tSqliteWorkerClientMessage,\n\t\t\t{ type: SqliteWorkerClientMessageType.RemoteCallbackRequest }\n\t\t>,\n\t\tsqliteDb: Database,\n\t): Promise<void> {\n\t\tconst result = await handleRemoteCallback({\n\t\t\tsqliteDb,\n\t\t\tsql: data.sql,\n\t\t\tparams: data.params,\n\t\t\tmethod: data.method,\n\t\t});\n\n\t\tif (result.success) {\n\t\t\tthis.send({\n\t\t\t\ttype: SqliteWorkerServerMessageType.RemoteCallbackResponse,\n\t\t\t\tid: data.id,\n\t\t\t\trows: result.result.rows,\n\t\t\t});\n\t\t} else {\n\t\t\tconsole.error(\"Error handling remote callback\", result.error);\n\t\t\tthis.send({\n\t\t\t\ttype: SqliteWorkerServerMessageType.RemoteCallbackError,\n\t\t\t\tid: data.id,\n\t\t\t\terror: result.error,\n\t\t\t});\n\t\t}\n\t}\n\n\t// Helper method to checkpoint the database (flush WAL to main DB file)\n\tprivate async processCheckpointRequest(\n\t\tdata: Extract<\n\t\t\tSqliteWorkerClientMessage,\n\t\t\t{ type: SqliteWorkerClientMessageType.Checkpoint }\n\t\t>,\n\t\tsqliteDb: Database,\n\t): Promise<void> {\n\t\ttry {\n\t\t\t// Execute PRAGMA wal_checkpoint(TRUNCATE) to ensure all WAL data\n\t\t\t// is written to the main database file and the WAL is truncated.\n\t\t\t// This ensures persistence to OPFS before page reload.\n\t\t\tsqliteDb.exec({\n\t\t\t\tsql: \"PRAGMA wal_checkpoint(TRUNCATE);\",\n\t\t\t\tcallback: () => {},\n\t\t\t});\n\n\t\t\tthis.send({\n\t\t\t\ttype: SqliteWorkerServerMessageType.CheckpointComplete,\n\t\t\t\tid: data.id,\n\t\t\t});\n\t\t} catch (e: unknown) {\n\t\t\tconst errorMsg = e instanceof Error ? e.message : String(e);\n\t\t\tthis.error(\"Error checkpointing database:\", errorMsg);\n\t\t\tthis.send({\n\t\t\t\ttype: SqliteWorkerServerMessageType.CheckpointError,\n\t\t\t\tid: data.id,\n\t\t\t\terror: errorMsg,\n\t\t\t});\n\t\t}\n\t}\n\n\tprivate applyOpenPragmas(\n\t\tdb: Database,\n\t\topenOptions?: SqliteWasmWorkerOpenOptions,\n\t) {\n\t\tconst journalMode = openOptions?.journalMode ?? \"WAL\";\n\t\tconst synchronous = openOptions?.synchronous ?? \"FULL\";\n\t\ttry {\n\t\t\tdb.exec(`PRAGMA journal_mode=${journalMode};`);\n\t\t\tdb.exec(`PRAGMA synchronous=${synchronous};`);\n\t\t\tthis.log(\n\t\t\t\t\"PRAGMA journal_mode=\",\n\t\t\t\tjournalMode,\n\t\t\t\t\"synchronous=\",\n\t\t\t\tsynchronous,\n\t\t\t);\n\t\t} catch (e) {\n\t\t\tthis.error(\"Error applying open pragmas:\", e);\n\t\t}\n\t}\n\n\tprivate async startDatabase(\n\t\tsqlite3: Sqlite3Static,\n\t\tdbName: string,\n\t\trequestId: StartRequestId,\n\t\topenOptions?: SqliteWasmWorkerOpenOptions,\n\t) {\n\t\tconst dbId = DbIdSchema.parse(crypto.randomUUID());\n\n\t\tconst dbFileName = `${dbName}.sqlite3`;\n\t\tlet db: Database;\n\n\t\tif (\"opfs\" in sqlite3) {\n\t\t\tdb = new sqlite3.oo1.OpfsDb(dbFileName);\n\t\t\tthis.log(\"OPFS database created:\", db.filename);\n\t\t\tthis.applyOpenPragmas(db, openOptions);\n\t\t} else {\n\t\t\tdb = new sqlite3.oo1.DB(dbFileName, \"c\");\n\t\t\tthis.log(\n\t\t\t\t\"OPFS is not available, created transient database\",\n\t\t\t\tdb.filename,\n\t\t\t);\n\t\t\tthis.applyOpenPragmas(db, openOptions);\n\t\t}\n\n\t\t// Store database with initialized flag\n\t\tthis.databases.set(dbId, { db, initialized: true });\n\n\t\t// Send Started message with dbId and requestId\n\t\tthis.send({\n\t\t\ttype: SqliteWorkerServerMessageType.Started,\n\t\t\trequestId,\n\t\t\tdbId,\n\t\t});\n\t}\n\n\tprivate async _handleMessage(data: SqliteWorkerClientMessage) {\n\t\tconst { type } = data;\n\t\tswitch (type) {\n\t\t\tcase SqliteWorkerClientMessageType.Start:\n\t\t\t\t{\n\t\t\t\t\tconst sqlite3 = await this.initPromise;\n\t\t\t\t\tawait this.startDatabase(\n\t\t\t\t\t\tsqlite3,\n\t\t\t\t\t\tdata.dbName,\n\t\t\t\t\t\tdata.requestId,\n\t\t\t\t\t\tdata.openOptions,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase SqliteWorkerClientMessageType.RemoteCallbackRequest:\n\t\t\t\t{\n\t\t\t\t\t// Get the database for this request\n\t\t\t\t\tconst dbEntry = this.databases.get(data.dbId);\n\t\t\t\t\tif (!dbEntry) {\n\t\t\t\t\t\tthis.error(`Database not found for dbId: ${data.dbId}`);\n\t\t\t\t\t\tthis.send({\n\t\t\t\t\t\t\ttype: SqliteWorkerServerMessageType.RemoteCallbackError,\n\t\t\t\t\t\t\tid: data.id,\n\t\t\t\t\t\t\terror: `Database not found: ${data.dbId}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!dbEntry.initialized) {\n\t\t\t\t\t\tthis.error(`Database not initialized for dbId: ${data.dbId}`);\n\t\t\t\t\t\tthis.send({\n\t\t\t\t\t\t\ttype: SqliteWorkerServerMessageType.RemoteCallbackError,\n\t\t\t\t\t\t\tid: data.id,\n\t\t\t\t\t\t\terror: `Database not initialized: ${data.dbId}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Process the request with the correct database\n\t\t\t\t\tawait this.processRemoteCallbackRequest(data, dbEntry.db);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase SqliteWorkerClientMessageType.Checkpoint:\n\t\t\t\t{\n\t\t\t\t\t// Get the database for this request\n\t\t\t\t\tconst dbEntry = this.databases.get(data.dbId);\n\t\t\t\t\tif (!dbEntry) {\n\t\t\t\t\t\tthis.error(`Database not found for dbId: ${data.dbId}`);\n\t\t\t\t\t\tthis.send({\n\t\t\t\t\t\t\ttype: SqliteWorkerServerMessageType.CheckpointError,\n\t\t\t\t\t\t\tid: data.id,\n\t\t\t\t\t\t\terror: `Database not found: ${data.dbId}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!dbEntry.initialized) {\n\t\t\t\t\t\tthis.error(`Database not initialized for dbId: ${data.dbId}`);\n\t\t\t\t\t\tthis.send({\n\t\t\t\t\t\t\ttype: SqliteWorkerServerMessageType.CheckpointError,\n\t\t\t\t\t\t\tid: data.id,\n\t\t\t\t\t\t\terror: `Database not initialized: ${data.dbId}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Process the checkpoint request\n\t\t\t\t\tawait this.processCheckpointRequest(data, dbEntry.db);\n\t\t\t\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UAAA,IAAI,CAAC,OAAA,EAAS;AACb,YAAA,IAAA,CAAK,KAAA,CAAM,CAAA,6BAAA,EAAgC,IAAA,CAAK,IAAI,CAAA,CAAE,CAAA;AACtD,YAAA,IAAA,CAAK,IAAA,CAAK;AAAA,cACT,IAAA,EAAA,kBAAA;AAAA,cACA,IAAI,IAAA,CAAK,EAAA;AAAA,cACT,KAAA,EAAO,CAAA,oBAAA,EAAuB,IAAA,CAAK,IAAI,CAAA;AAAA,aACvC,CAAA;AACD,YAAA;AAAA,UACD;AAEA,UAAA,IAAI,CAAC,QAAQ,WAAA,EAAa;AACzB,YAAA,IAAA,CAAK,KAAA,CAAM,CAAA,mCAAA,EAAsC,IAAA,CAAK,IAAI,CAAA,CAAE,CAAA;AAC5D,YAAA,IAAA,CAAK,IAAA,CAAK;AAAA,cACT,IAAA,EAAA,kBAAA;AAAA,cACA,IAAI,IAAA,CAAK,EAAA;AAAA,cACT,KAAA,EAAO,CAAA,0BAAA,EAA6B,IAAA,CAAK,IAAI,CAAA;AAAA,aAC7C,CAAA;AACD,YAAA;AAAA,UACD;AAGA,UAAA,MAAM,IAAA,CAAK,wBAAA,CAAyB,IAAA,EAAM,OAAA,CAAQ,EAAE,CAAA;AAAA,QACrD;AACA,QAAA;AAAA,MACD;AACC,QAAA,OAAO,gBAAgB,IAAI,CAAA;AAAA;AAC7B,EACD;AACD,CAAA;AAEA,IAAI,kBAAA,EAAmB","file":"sqlite.worker.js","sourcesContent":["import { WorkerHelper } from \"@firtoz/worker-helper\";\nimport {\n\tSqliteWorkerClientMessageSchema,\n\tSqliteWorkerClientMessageType,\n\tsqliteWorkerServerMessage,\n\tSqliteWorkerServerMessageType,\n\ttype SqliteWorkerClientMessage,\n\ttype SqliteWorkerServerMessage,\n\ttype StartRequestId,\n\tDbIdSchema,\n\ttype DbId,\n} from \"./schema\";\nimport { handleRemoteCallback } from \"../drizzle/handle-callback\";\nimport { exhaustiveGuard } from \"@firtoz/maybe-error\";\nimport type { SqliteWasmWorkerOpenOptions } from \"./sqlite-open-options\";\nimport type { Sqlite3Static, Database } from \"../types\";\n\n// Declare self as DedicatedWorkerGlobalScope for TypeScript\ndeclare var self: DedicatedWorkerGlobalScope;\n\nclass SqliteWorkerHelper extends WorkerHelper<\n\tSqliteWorkerClientMessage,\n\tSqliteWorkerServerMessage\n> {\n\tprivate initPromise: Promise<Sqlite3Static>;\n\tprivate databases = new Map<\n\t\tDbId,\n\t\t{\n\t\t\tdb: Database;\n\t\t\tinitialized: boolean;\n\t\t}\n\t>();\n\n\tconstructor() {\n\t\tsuper(self, SqliteWorkerClientMessageSchema, sqliteWorkerServerMessage, {\n\t\t\thandleMessage: (data) => {\n\t\t\t\tthis._handleMessage(data);\n\t\t\t},\n\t\t\thandleInputValidationError: (error, originalData) => {\n\t\t\t\tconsole.error(\"Input validation error\", { error, originalData });\n\t\t\t\tthrow new Error(`Invalid input: ${error.message}`);\n\t\t\t},\n\t\t\thandleOutputValidationError: (error, originalData) => {\n\t\t\t\tconsole.error(\"Output validation error\", { error, originalData });\n\t\t\t\tthrow new Error(`Invalid output: ${error.message}`);\n\t\t\t},\n\t\t\thandleProcessingError: (error, validatedData) => {\n\t\t\t\tconsole.error(\"Processing error\", { error, validatedData });\n\t\t\t\tthrow new Error(`Processing error: ${String(error)}`);\n\t\t\t},\n\t\t});\n\n\t\tthis.initPromise = import(\"@sqlite.org/sqlite-wasm\").then(\n\t\t\tasync ({ default: sqlite3InitModule }) => {\n\t\t\t\tconst result = await sqlite3InitModule();\n\n\t\t\t\treturn result;\n\t\t\t},\n\t\t);\n\n\t\tthis.send({\n\t\t\ttype: SqliteWorkerServerMessageType.Ready,\n\t\t});\n\t}\n\n\tprivate log(...args: unknown[]) {\n\t\tconsole.log(`[${new Date().toISOString()}]`, ...args);\n\t}\n\n\tprivate error(...args: unknown[]) {\n\t\tconsole.error(`[${new Date().toISOString()}]`, ...args);\n\t}\n\n\t// Helper method to process remote callback requests\n\tprivate async processRemoteCallbackRequest(\n\t\tdata: Extract<\n\t\t\tSqliteWorkerClientMessage,\n\t\t\t{ type: SqliteWorkerClientMessageType.RemoteCallbackRequest }\n\t\t>,\n\t\tsqliteDb: Database,\n\t): Promise<void> {\n\t\tconst result = await handleRemoteCallback({\n\t\t\tsqliteDb,\n\t\t\tsql: data.sql,\n\t\t\tparams: data.params,\n\t\t\tmethod: data.method,\n\t\t});\n\n\t\tif (result.success) {\n\t\t\tthis.send({\n\t\t\t\ttype: SqliteWorkerServerMessageType.RemoteCallbackResponse,\n\t\t\t\tid: data.id,\n\t\t\t\trows: result.result.rows,\n\t\t\t});\n\t\t} else {\n\t\t\tconsole.error(\"Error handling remote callback\", result.error);\n\t\t\tthis.send({\n\t\t\t\ttype: SqliteWorkerServerMessageType.RemoteCallbackError,\n\t\t\t\tid: data.id,\n\t\t\t\terror: result.error,\n\t\t\t});\n\t\t}\n\t}\n\n\t// Helper method to checkpoint the database (flush WAL to main DB file)\n\tprivate async processCheckpointRequest(\n\t\tdata: Extract<\n\t\t\tSqliteWorkerClientMessage,\n\t\t\t{ type: SqliteWorkerClientMessageType.Checkpoint }\n\t\t>,\n\t\tsqliteDb: Database,\n\t): Promise<void> {\n\t\ttry {\n\t\t\t// Execute PRAGMA wal_checkpoint(TRUNCATE) to ensure all WAL data\n\t\t\t// is written to the main database file and the WAL is truncated.\n\t\t\t// This ensures persistence to OPFS before page reload.\n\t\t\tsqliteDb.exec({\n\t\t\t\tsql: \"PRAGMA wal_checkpoint(TRUNCATE);\",\n\t\t\t\tcallback: () => {},\n\t\t\t});\n\n\t\t\tthis.send({\n\t\t\t\ttype: SqliteWorkerServerMessageType.CheckpointComplete,\n\t\t\t\tid: data.id,\n\t\t\t});\n\t\t} catch (e: unknown) {\n\t\t\tconst errorMsg = e instanceof Error ? e.message : String(e);\n\t\t\tthis.error(\"Error checkpointing database:\", errorMsg);\n\t\t\tthis.send({\n\t\t\t\ttype: SqliteWorkerServerMessageType.CheckpointError,\n\t\t\t\tid: data.id,\n\t\t\t\terror: errorMsg,\n\t\t\t});\n\t\t}\n\t}\n\n\tprivate applyOpenPragmas(\n\t\tdb: Database,\n\t\topenOptions?: SqliteWasmWorkerOpenOptions,\n\t) {\n\t\tconst journalMode = openOptions?.journalMode ?? \"WAL\";\n\t\tconst synchronous = openOptions?.synchronous ?? \"FULL\";\n\t\ttry {\n\t\t\tdb.exec(`PRAGMA journal_mode=${journalMode};`);\n\t\t\tdb.exec(`PRAGMA synchronous=${synchronous};`);\n\t\t\tthis.log(\n\t\t\t\t\"PRAGMA journal_mode=\",\n\t\t\t\tjournalMode,\n\t\t\t\t\"synchronous=\",\n\t\t\t\tsynchronous,\n\t\t\t);\n\t\t} catch (e) {\n\t\t\tthis.error(\"Error applying open pragmas:\", e);\n\t\t}\n\t}\n\n\tprivate async startDatabase(\n\t\tsqlite3: Sqlite3Static,\n\t\tdbName: string,\n\t\trequestId: StartRequestId,\n\t\topenOptions?: SqliteWasmWorkerOpenOptions,\n\t) {\n\t\tconst dbId = DbIdSchema.parse(crypto.randomUUID());\n\n\t\tconst dbFileName = `${dbName}.sqlite3`;\n\t\tlet db: Database;\n\n\t\t// After full init, sqlite-wasm removes `sqlite3.opfs` in non-test builds\n\t\t// (`asyncPostInit`), so `\"opfs\" in sqlite3` is a false negative. Prefer the\n\t\t// OpfsDb constructor installed by the OPFS VFS initializer.\n\t\tif (typeof sqlite3.oo1.OpfsDb === \"function\") {\n\t\t\tdb = new sqlite3.oo1.OpfsDb(dbFileName);\n\t\t\tthis.log(\"OPFS database created:\", db.filename);\n\t\t\tthis.applyOpenPragmas(db, openOptions);\n\t\t} else {\n\t\t\tdb = new sqlite3.oo1.DB(dbFileName, \"c\");\n\t\t\tthis.log(\n\t\t\t\t\"OPFS is not available, created transient database\",\n\t\t\t\tdb.filename,\n\t\t\t);\n\t\t\tthis.applyOpenPragmas(db, openOptions);\n\t\t}\n\n\t\t// Store database with initialized flag\n\t\tthis.databases.set(dbId, { db, initialized: true });\n\n\t\t// Send Started message with dbId and requestId\n\t\tthis.send({\n\t\t\ttype: SqliteWorkerServerMessageType.Started,\n\t\t\trequestId,\n\t\t\tdbId,\n\t\t});\n\t}\n\n\tprivate async _handleMessage(data: SqliteWorkerClientMessage) {\n\t\tconst { type } = data;\n\t\tswitch (type) {\n\t\t\tcase SqliteWorkerClientMessageType.Start:\n\t\t\t\t{\n\t\t\t\t\tconst sqlite3 = await this.initPromise;\n\t\t\t\t\tawait this.startDatabase(\n\t\t\t\t\t\tsqlite3,\n\t\t\t\t\t\tdata.dbName,\n\t\t\t\t\t\tdata.requestId,\n\t\t\t\t\t\tdata.openOptions,\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase SqliteWorkerClientMessageType.RemoteCallbackRequest:\n\t\t\t\t{\n\t\t\t\t\t// Get the database for this request\n\t\t\t\t\tconst dbEntry = this.databases.get(data.dbId);\n\t\t\t\t\tif (!dbEntry) {\n\t\t\t\t\t\tthis.error(`Database not found for dbId: ${data.dbId}`);\n\t\t\t\t\t\tthis.send({\n\t\t\t\t\t\t\ttype: SqliteWorkerServerMessageType.RemoteCallbackError,\n\t\t\t\t\t\t\tid: data.id,\n\t\t\t\t\t\t\terror: `Database not found: ${data.dbId}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!dbEntry.initialized) {\n\t\t\t\t\t\tthis.error(`Database not initialized for dbId: ${data.dbId}`);\n\t\t\t\t\t\tthis.send({\n\t\t\t\t\t\t\ttype: SqliteWorkerServerMessageType.RemoteCallbackError,\n\t\t\t\t\t\t\tid: data.id,\n\t\t\t\t\t\t\terror: `Database not initialized: ${data.dbId}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Process the request with the correct database\n\t\t\t\t\tawait this.processRemoteCallbackRequest(data, dbEntry.db);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tcase SqliteWorkerClientMessageType.Checkpoint:\n\t\t\t\t{\n\t\t\t\t\t// Get the database for this request\n\t\t\t\t\tconst dbEntry = this.databases.get(data.dbId);\n\t\t\t\t\tif (!dbEntry) {\n\t\t\t\t\t\tthis.error(`Database not found for dbId: ${data.dbId}`);\n\t\t\t\t\t\tthis.send({\n\t\t\t\t\t\t\ttype: SqliteWorkerServerMessageType.CheckpointError,\n\t\t\t\t\t\t\tid: data.id,\n\t\t\t\t\t\t\terror: `Database not found: ${data.dbId}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\tif (!dbEntry.initialized) {\n\t\t\t\t\t\tthis.error(`Database not initialized for dbId: ${data.dbId}`);\n\t\t\t\t\t\tthis.send({\n\t\t\t\t\t\t\ttype: SqliteWorkerServerMessageType.CheckpointError,\n\t\t\t\t\t\t\tid: data.id,\n\t\t\t\t\t\t\terror: `Database not initialized: ${data.dbId}`,\n\t\t\t\t\t\t});\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Process the checkpoint request\n\t\t\t\t\tawait this.processCheckpointRequest(data, dbEntry.db);\n\t\t\t\t}\n\t\t\t\tbreak;\n\t\t\tdefault:\n\t\t\t\treturn exhaustiveGuard(type);\n\t\t}\n\t}\n}\n\nnew SqliteWorkerHelper();\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@firtoz/drizzle-sqlite-wasm",
3
- "version": "1.1.2",
3
+ "version": "2.0.1",
4
4
  "description": "Drizzle SQLite WASM bindings",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -19,7 +19,7 @@
19
19
  "types": "./dist/migration/migrator.d.ts",
20
20
  "import": "./dist/migration/migrator.js"
21
21
  },
22
- "./drizzleCollectionOptions": {
22
+ "./sqliteCollectionOptions": {
23
23
  "types": "./dist/collections/sqlite-collection.d.ts",
24
24
  "import": "./dist/collections/sqlite-collection.js"
25
25
  },
@@ -69,12 +69,12 @@
69
69
  "access": "public"
70
70
  },
71
71
  "dependencies": {
72
- "@firtoz/collection-sync": "^6.0.1",
72
+ "@firtoz/collection-sync": "^6.0.3",
73
73
  "@firtoz/db-helpers": "^2.2.1",
74
74
  "@firtoz/drizzle-utils": "^1.3.1",
75
75
  "@firtoz/maybe-error": "^1.6.1",
76
- "@firtoz/worker-helper": "^1.6.1",
77
- "@sqlite.org/sqlite-wasm": "^3.51.2-build9",
76
+ "@firtoz/worker-helper": "^1.6.2",
77
+ "@sqlite.org/sqlite-wasm": "^3.53.0-build1",
78
78
  "@tanstack/db": "^0.6.5",
79
79
  "drizzle-orm": "^0.45.2",
80
80
  "drizzle-valibot": "^0.4.2",
@@ -18,10 +18,8 @@ import {
18
18
  createInsertSchemaWithIdDefault,
19
19
  createCollectionConfig,
20
20
  createSqliteTableSyncBackend,
21
- type SQLOperation,
22
21
  type SQLInterceptor,
23
22
  } from "@firtoz/drizzle-utils";
24
- export type { SQLOperation, SQLInterceptor };
25
23
 
26
24
  export type AnyDrizzleDatabase = BaseSQLiteDatabase<
27
25
  "async",
@@ -1,10 +1,7 @@
1
1
  import type { DrizzleConfig } from "drizzle-orm";
2
2
  import { drizzle as drizzleSqliteProxy } from "drizzle-orm/sqlite-proxy";
3
3
  import type { ISqliteWorkerClient } from "../worker/client";
4
- import type {
5
- SQLInterceptor,
6
- SQLOperation,
7
- } from "../collections/sqlite-collection";
4
+ import type { SQLInterceptor, SQLOperation } from "@firtoz/drizzle-utils";
8
5
 
9
6
  export const drizzleSqliteWasmWorker = <
10
7
  TSchema extends Record<string, unknown> = Record<string, never>,