tanstack-db-pglite 1.2.5 → 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
@@ -1,7 +1,7 @@
1
+ import { BasicIndex } from '@tanstack/db';
1
2
  import { eq, inArray } from 'drizzle-orm';
2
3
  import { createSelectSchema } from 'drizzle-zod';
3
4
  export function drizzleCollectionOptions({ startSync = true, ...config }) {
4
- let resolvers = Promise.withResolvers();
5
5
  // Sync params can be null while running PGLite migrations
6
6
  const { promise: syncParams, resolve: resolveSyncParams } = Promise.withResolvers();
7
7
  // eslint-disable-next-line ts/no-explicit-any
@@ -63,13 +63,11 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
63
63
  .where(eq(config.primaryColumn, m.key))));
64
64
  }
65
65
  const sync = async () => {
66
+ const params = await syncParams;
66
67
  if (!config.sync) {
68
+ params.markReady();
67
69
  return;
68
70
  }
69
- const params = await syncParams;
70
- const previousResolvers = resolvers;
71
- resolvers = Promise.withResolvers();
72
- previousResolvers.resolve({ continue: true });
73
71
  await config.sync({
74
72
  write: async (message) => {
75
73
  if (message.type === 'insert') {
@@ -87,34 +85,27 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
87
85
  params.commit();
88
86
  },
89
87
  collection: params.collection,
88
+ markReady: params.markReady,
90
89
  });
91
- resolvers.resolve({ continue: false });
92
- };
93
- const waitForSync = async () => {
94
- await resolvers.promise.then(r => r.continue ? waitForSync() : undefined);
95
90
  };
96
91
  return {
97
92
  startSync: true,
98
93
  autoIndex: 'eager',
94
+ defaultIndexType: BasicIndex,
99
95
  sync: {
100
96
  sync: (params) => {
101
97
  resolveSyncParams(params);
102
98
  (async () => {
103
- try {
104
- await config.prepare?.();
105
- // @ts-expect-error drizzle types
106
- const dbs = await config.db.select().from(config.table);
107
- params.begin();
108
- dbs.forEach((db) => {
109
- params.write({ type: 'insert', value: db });
110
- });
111
- params.commit();
112
- if (startSync) {
113
- sync();
114
- }
115
- }
116
- finally {
117
- 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();
118
109
  }
119
110
  })();
120
111
  },
@@ -160,7 +151,6 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
160
151
  await params.collection.stateWhenReady();
161
152
  await sync();
162
153
  },
163
- waitForSync,
164
154
  },
165
155
  };
166
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
@@ -1,9 +1,9 @@
1
+ import { BasicIndex, } from '@tanstack/db';
1
2
  function quoteId(name) {
2
3
  // eslint-disable-next-line e18e/prefer-static-regex
3
4
  return `"${String(name).replace(/"/g, '""')}"`;
4
5
  }
5
6
  export function sqlCollectionOptions({ startSync = true, ...config }) {
6
- let resolvers = Promise.withResolvers();
7
7
  const table = quoteId(config.tableName);
8
8
  const primaryKey = quoteId(config.primaryKeyColumn);
9
9
  const getKey = config.getKey ?? ((row) => String(row[config.primaryKeyColumn]));
@@ -50,13 +50,11 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
50
50
  commit();
51
51
  }
52
52
  const sync = async () => {
53
+ const params = await syncParams;
53
54
  if (!config.sync) {
55
+ params.markReady();
54
56
  return;
55
57
  }
56
- const params = await syncParams;
57
- const previousResolvers = resolvers;
58
- resolvers = Promise.withResolvers();
59
- previousResolvers.resolve({ continue: true });
60
58
  await config.sync({
61
59
  write: async (p) => {
62
60
  if (p.type === 'insert') {
@@ -74,33 +72,26 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
74
72
  params.commit();
75
73
  },
76
74
  collection: params.collection,
75
+ markReady: params.markReady,
77
76
  });
78
- resolvers.resolve({ continue: false });
79
- };
80
- const waitForSync = async () => {
81
- await resolvers.promise.then(r => r.continue ? waitForSync() : undefined);
82
77
  };
83
78
  return {
84
79
  startSync,
85
80
  autoIndex: 'eager',
81
+ defaultIndexType: BasicIndex,
86
82
  sync: {
87
83
  sync: (params) => {
88
84
  resolveSyncParams(params);
89
85
  (async () => {
90
- try {
91
- await config.prepare?.();
92
- const rows = await runSelect(config.db);
93
- params.begin();
94
- rows.forEach((row) => {
95
- params.write({ type: 'insert', value: row });
96
- });
97
- params.commit();
98
- if (startSync) {
99
- sync();
100
- }
101
- }
102
- finally {
103
- 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();
104
95
  }
105
96
  })();
106
97
  },
@@ -143,7 +134,6 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
143
134
  await params.collection.stateWhenReady();
144
135
  await sync();
145
136
  },
146
- waitForSync,
147
137
  },
148
138
  };
149
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.5",
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",