tanstack-db-pglite 1.0.1 → 1.0.3

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, SyncPara
10
10
  onDelete?: (params: DeleteMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
11
11
  startSync?: boolean;
12
12
  prepare?: () => Promise<unknown> | unknown;
13
- sync: (params: Pick<SyncParams, 'write' | 'collection'>) => Promise<void>;
13
+ sync?: (params: Pick<SyncParams, 'write' | 'collection'>) => Promise<void>;
14
14
  }): CollectionConfig<Table['$inferSelect'], string> & {
15
15
  utils: {
16
16
  runSync: () => Promise<void>;
package/dist/drizzle.js CHANGED
@@ -49,6 +49,22 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
49
49
  });
50
50
  commit();
51
51
  }
52
+ /**
53
+ * https://github.com/drizzle-team/drizzle-orm/issues/1723
54
+ *
55
+ * Each query in the db using transaction will not be committed
56
+ * So I added this trick to select all changed data from the db
57
+ * Because after select, the transaction will be committed
58
+ *
59
+ * Yeah, it's stupid, but it works
60
+ */
61
+ async function finishTransaction(mutations) {
62
+ await Promise.all(mutations.map(m => config.db
63
+ .select({ id: config.primaryColumn })
64
+ // @ts-expect-error drizzle types
65
+ .from(config.table)
66
+ .where(eq(config.primaryColumn, m.key))));
67
+ }
52
68
  return {
53
69
  startSync: true,
54
70
  sync: {
@@ -78,26 +94,38 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
78
94
  onInsert: async (params) => {
79
95
  await config.db.transaction(async (tx) => {
80
96
  await onDrizzleInsert(params.transaction.mutations.map(m => m.modified), tx);
81
- await config.onInsert?.(params);
97
+ if (config.onInsert) {
98
+ await config.onInsert(params);
99
+ }
82
100
  });
101
+ await finishTransaction(params.transaction.mutations);
83
102
  await runMutations(params.transaction.mutations);
84
103
  },
85
104
  onUpdate: async (params) => {
86
105
  await config.db.transaction(async (tx) => {
87
106
  await Promise.all(params.transaction.mutations.map(m => onDrizzleUpdate(m.key, m.changes, tx)));
88
- await config.onUpdate?.(params);
107
+ if (config.onUpdate) {
108
+ await config.onUpdate(params);
109
+ }
89
110
  });
111
+ await finishTransaction(params.transaction.mutations);
90
112
  await runMutations(params.transaction.mutations);
91
113
  },
92
114
  onDelete: async (params) => {
93
115
  await config.db.transaction(async (tx) => {
94
116
  await onDrizzleDelete(params.transaction.mutations.map(m => m.key), tx);
95
- await config.onDelete?.(params);
117
+ if (config.onDelete) {
118
+ await config.onDelete(params);
119
+ }
96
120
  });
121
+ await finishTransaction(params.transaction.mutations);
97
122
  await runMutations(params.transaction.mutations);
98
123
  },
99
124
  utils: {
100
125
  runSync: async () => {
126
+ if (!config.sync) {
127
+ throw new Error('Sync is not defined');
128
+ }
101
129
  const params = await getSyncParams();
102
130
  // To wait the first sync
103
131
  await params.collection.stateWhenReady();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tanstack-db-pglite",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "packageManager": "pnpm@10.16.1",
5
5
  "description": "",
6
6
  "author": "Valerii Strilets",