tanstack-db-pglite 1.2.6 → 1.3.0

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/drizzle.d.ts CHANGED
@@ -10,7 +10,7 @@ export declare function drizzleCollectionOptions<Table extends PgTable>({ startS
10
10
  startSync?: boolean;
11
11
  table: Table;
12
12
  primaryColumn: IndexColumn;
13
- sync?: (params: Pick<SyncParams<Table>, 'write' | 'collection'>) => Promise<void>;
13
+ sync?: (params: Pick<SyncParams<Table>, 'write' | 'collection' | 'markReady'>) => Promise<void>;
14
14
  prepare?: () => Promise<unknown> | unknown;
15
15
  onInsert?: (params: InsertMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
16
16
  onUpdate?: (params: UpdateMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
package/dist/drizzle.js CHANGED
@@ -2,7 +2,6 @@ import { BasicIndex } from '@tanstack/db';
2
2
  import { eq, inArray } from 'drizzle-orm';
3
3
  import { createSelectSchema } from 'drizzle-zod';
4
4
  export function drizzleCollectionOptions({ startSync = true, ...config }) {
5
- let resolvers = Promise.withResolvers();
6
5
  // Sync params can be null while running PGLite migrations
7
6
  const { promise: syncParams, resolve: resolveSyncParams } = Promise.withResolvers();
8
7
  // eslint-disable-next-line ts/no-explicit-any
@@ -64,13 +63,11 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
64
63
  .where(eq(config.primaryColumn, m.key))));
65
64
  }
66
65
  const sync = async () => {
66
+ const params = await syncParams;
67
67
  if (!config.sync) {
68
+ params.markReady();
68
69
  return;
69
70
  }
70
- const params = await syncParams;
71
- const previousResolvers = resolvers;
72
- resolvers = Promise.withResolvers();
73
- previousResolvers.resolve({ continue: true });
74
71
  await config.sync({
75
72
  write: async (message) => {
76
73
  if (message.type === 'insert') {
@@ -88,11 +85,8 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
88
85
  params.commit();
89
86
  },
90
87
  collection: params.collection,
88
+ markReady: params.markReady,
91
89
  });
92
- resolvers.resolve({ continue: false });
93
- };
94
- const waitForSync = async () => {
95
- await resolvers.promise.then(r => r.continue ? waitForSync() : undefined);
96
90
  };
97
91
  return {
98
92
  startSync: true,
@@ -102,21 +96,16 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
102
96
  sync: (params) => {
103
97
  resolveSyncParams(params);
104
98
  (async () => {
105
- try {
106
- await config.prepare?.();
107
- // @ts-expect-error drizzle types
108
- const dbs = await config.db.select().from(config.table);
109
- params.begin();
110
- dbs.forEach((db) => {
111
- params.write({ type: 'insert', value: db });
112
- });
113
- params.commit();
114
- if (startSync) {
115
- sync();
116
- }
117
- }
118
- finally {
119
- params.markReady();
99
+ await config.prepare?.();
100
+ // @ts-expect-error drizzle types
101
+ const dbs = await config.db.select().from(config.table);
102
+ params.begin();
103
+ dbs.forEach((db) => {
104
+ params.write({ type: 'insert', value: db });
105
+ });
106
+ params.commit();
107
+ if (startSync) {
108
+ await sync();
120
109
  }
121
110
  })();
122
111
  },
@@ -162,7 +151,6 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
162
151
  await params.collection.stateWhenReady();
163
152
  await sync();
164
153
  },
165
- waitForSync,
166
154
  },
167
155
  };
168
156
  }
package/dist/sql.d.ts CHANGED
@@ -13,7 +13,7 @@ export declare function sqlCollectionOptions<Schema extends StandardSchemaV1<Rec
13
13
  schema: Schema;
14
14
  getKey?: (row: Output<Schema>) => string;
15
15
  prepare?: () => Promise<unknown> | unknown;
16
- sync?: (params: Pick<SyncParams<Output<Schema>>, 'write' | 'collection'>) => Promise<void>;
16
+ sync?: (params: Pick<SyncParams<Output<Schema>>, 'write' | 'collection' | 'markReady'>) => Promise<void>;
17
17
  onInsert?: (params: InsertMutationFnParams<Output<Schema>, string>) => Promise<void>;
18
18
  onUpdate?: (params: UpdateMutationFnParams<Output<Schema>, string>) => Promise<void>;
19
19
  onDelete?: (params: DeleteMutationFnParams<Output<Schema>, string>) => Promise<void>;
package/dist/sql.js CHANGED
@@ -4,7 +4,6 @@ function quoteId(name) {
4
4
  return `"${String(name).replace(/"/g, '""')}"`;
5
5
  }
6
6
  export function sqlCollectionOptions({ startSync = true, ...config }) {
7
- let resolvers = Promise.withResolvers();
8
7
  const table = quoteId(config.tableName);
9
8
  const primaryKey = quoteId(config.primaryKeyColumn);
10
9
  const getKey = config.getKey ?? ((row) => String(row[config.primaryKeyColumn]));
@@ -51,13 +50,11 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
51
50
  commit();
52
51
  }
53
52
  const sync = async () => {
53
+ const params = await syncParams;
54
54
  if (!config.sync) {
55
+ params.markReady();
55
56
  return;
56
57
  }
57
- const params = await syncParams;
58
- const previousResolvers = resolvers;
59
- resolvers = Promise.withResolvers();
60
- previousResolvers.resolve({ continue: true });
61
58
  await config.sync({
62
59
  write: async (p) => {
63
60
  if (p.type === 'insert') {
@@ -75,11 +72,8 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
75
72
  params.commit();
76
73
  },
77
74
  collection: params.collection,
75
+ markReady: params.markReady,
78
76
  });
79
- resolvers.resolve({ continue: false });
80
- };
81
- const waitForSync = async () => {
82
- await resolvers.promise.then(r => r.continue ? waitForSync() : undefined);
83
77
  };
84
78
  return {
85
79
  startSync,
@@ -89,20 +83,15 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
89
83
  sync: (params) => {
90
84
  resolveSyncParams(params);
91
85
  (async () => {
92
- try {
93
- await config.prepare?.();
94
- const rows = await runSelect(config.db);
95
- params.begin();
96
- rows.forEach((row) => {
97
- params.write({ type: 'insert', value: row });
98
- });
99
- params.commit();
100
- if (startSync) {
101
- sync();
102
- }
103
- }
104
- finally {
105
- params.markReady();
86
+ await config.prepare?.();
87
+ const rows = await runSelect(config.db);
88
+ params.begin();
89
+ rows.forEach((row) => {
90
+ params.write({ type: 'insert', value: row });
91
+ });
92
+ params.commit();
93
+ if (startSync) {
94
+ await sync();
106
95
  }
107
96
  })();
108
97
  },
@@ -145,7 +134,6 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
145
134
  await params.collection.stateWhenReady();
146
135
  await sync();
147
136
  },
148
- waitForSync,
149
137
  },
150
138
  };
151
139
  }
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { UtilsRecord } from '@tanstack/db';
2
2
  export interface PgliteUtils extends UtilsRecord {
3
3
  runSync: () => Promise<void>;
4
- waitForSync: () => Promise<void>;
5
4
  }
package/package.json CHANGED
@@ -1,7 +1,6 @@
1
1
  {
2
2
  "name": "tanstack-db-pglite",
3
- "version": "1.2.6",
4
- "description": "",
3
+ "version": "1.3.0",
5
4
  "author": "Valerii Strilets",
6
5
  "license": "MIT",
7
6
  "repository": {
@@ -12,7 +11,8 @@
12
11
  "tanstack",
13
12
  "db",
14
13
  "pglite",
15
- "drizzle"
14
+ "drizzle",
15
+ "sql"
16
16
  ],
17
17
  "main": "dist/index.js",
18
18
  "types": "dist/index.d.ts",