tanstack-db-pglite 1.0.2 → 1.0.4

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 (2) hide show
  1. package/dist/drizzle.js +48 -20
  2. package/package.json +5 -3
package/dist/drizzle.js CHANGED
@@ -49,51 +49,79 @@ 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: {
55
- sync: async (params) => {
56
- try {
57
- resolveSyncParams(params);
58
- await config.prepare?.();
59
- params.begin();
60
- // @ts-expect-error drizzle types
61
- const dbs = await config.db.select().from(config.table);
62
- dbs.forEach((db) => {
63
- params.write({ type: 'insert', value: db });
64
- });
65
- params.commit();
66
- if (config.sync && startSync) {
67
- await config.sync(await getSyncParams());
71
+ sync: (params) => {
72
+ resolveSyncParams(params);
73
+ (async () => {
74
+ try {
75
+ await config.prepare?.();
76
+ params.begin();
77
+ // @ts-expect-error drizzle types
78
+ const dbs = await config.db.select().from(config.table);
79
+ dbs.forEach((db) => {
80
+ params.write({ type: 'insert', value: db });
81
+ });
82
+ params.commit();
83
+ if (config.sync && startSync) {
84
+ await config.sync(await getSyncParams());
85
+ }
68
86
  }
69
- }
70
- finally {
71
- params.markReady();
72
- }
87
+ finally {
88
+ params.markReady();
89
+ }
90
+ })();
73
91
  },
74
92
  },
75
93
  gcTime: 0,
94
+ // @ts-expect-error wrong types
76
95
  schema: createSelectSchema(config.table),
77
96
  getKey: t => t[config.primaryColumn.name],
78
97
  onInsert: async (params) => {
79
98
  await config.db.transaction(async (tx) => {
80
99
  await onDrizzleInsert(params.transaction.mutations.map(m => m.modified), tx);
81
- await config.onInsert?.(params);
100
+ if (config.onInsert) {
101
+ await config.onInsert(params);
102
+ }
82
103
  });
104
+ await finishTransaction(params.transaction.mutations);
83
105
  await runMutations(params.transaction.mutations);
84
106
  },
85
107
  onUpdate: async (params) => {
86
108
  await config.db.transaction(async (tx) => {
87
109
  await Promise.all(params.transaction.mutations.map(m => onDrizzleUpdate(m.key, m.changes, tx)));
88
- await config.onUpdate?.(params);
110
+ if (config.onUpdate) {
111
+ await config.onUpdate(params);
112
+ }
89
113
  });
114
+ await finishTransaction(params.transaction.mutations);
90
115
  await runMutations(params.transaction.mutations);
91
116
  },
92
117
  onDelete: async (params) => {
93
118
  await config.db.transaction(async (tx) => {
94
119
  await onDrizzleDelete(params.transaction.mutations.map(m => m.key), tx);
95
- await config.onDelete?.(params);
120
+ if (config.onDelete) {
121
+ await config.onDelete(params);
122
+ }
96
123
  });
124
+ await finishTransaction(params.transaction.mutations);
97
125
  await runMutations(params.transaction.mutations);
98
126
  },
99
127
  utils: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tanstack-db-pglite",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "packageManager": "pnpm@10.16.1",
5
5
  "description": "",
6
6
  "author": "Valerii Strilets",
@@ -18,7 +18,9 @@
18
18
  ],
19
19
  "main": "dist/index.js",
20
20
  "types": "dist/index.d.ts",
21
- "files": ["dist"],
21
+ "files": [
22
+ "dist"
23
+ ],
22
24
  "scripts": {
23
25
  "prepublishOnly": "pnpm build",
24
26
  "build": "tsc",
@@ -26,7 +28,7 @@
26
28
  "lint:fix": "eslint . --fix"
27
29
  },
28
30
  "peerDependencies": {
29
- "@tanstack/db": ">=0.2.0",
31
+ "@tanstack/db": ">=0.4.0",
30
32
  "drizzle-orm": ">=0.44.0"
31
33
  },
32
34
  "dependencies": {