tanstack-db-pglite 1.0.0 → 1.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.
- package/README.md +1 -1
- package/dist/drizzle.d.ts +3 -3
- package/dist/drizzle.js +33 -31
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# tanstack-db-pglite
|
|
2
2
|
|
|
3
|
-
A seamless integration between [TanStack DB](https://tanstack.com/db) and [PGLite](https://github.com/electric-sql/pglite) with [Drizzle ORM](https://orm.drizzle.team/) for browser-based database management
|
|
3
|
+
A seamless integration between [TanStack DB](https://tanstack.com/db) and [PGLite](https://github.com/electric-sql/pglite) with [Drizzle ORM](https://orm.drizzle.team/) for browser-based database management.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
package/dist/drizzle.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
|
|
2
2
|
import type { IndexColumn, PgTable } from 'drizzle-orm/pg-core';
|
|
3
3
|
import type { PgliteDatabase } from 'drizzle-orm/pglite';
|
|
4
|
-
export declare function drizzleCollectionOptions<Table extends PgTable>({ startSync, ...config }: {
|
|
4
|
+
export declare function drizzleCollectionOptions<Table extends PgTable, SyncParams extends Parameters<SyncConfig<Table['$inferSelect'], string>['sync']>[0] = Parameters<SyncConfig<Table['$inferSelect'], string>['sync']>[0]>({ startSync, ...config }: {
|
|
5
5
|
db: PgliteDatabase<any>;
|
|
6
6
|
table: Table;
|
|
7
7
|
primaryColumn: IndexColumn;
|
|
@@ -9,8 +9,8 @@ export declare function drizzleCollectionOptions<Table extends PgTable>({ startS
|
|
|
9
9
|
onUpdate?: (params: UpdateMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
10
10
|
onDelete?: (params: DeleteMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
11
11
|
startSync?: boolean;
|
|
12
|
-
prepare?: () => Promise<
|
|
13
|
-
sync: (params: Pick<
|
|
12
|
+
prepare?: () => Promise<unknown> | unknown;
|
|
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
|
@@ -3,28 +3,18 @@ import { createSelectSchema } from 'drizzle-zod';
|
|
|
3
3
|
export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
4
4
|
// Sync params can be null while running PGLite migrations
|
|
5
5
|
const { promise: syncParams, resolve: resolveSyncParams } = Promise.withResolvers();
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
begin();
|
|
9
|
-
mutations.forEach((m) => {
|
|
10
|
-
write({ type: m.type, value: m.modified });
|
|
11
|
-
});
|
|
12
|
-
commit();
|
|
13
|
-
}
|
|
14
|
-
async function onDrizzleInsert(data) {
|
|
6
|
+
// eslint-disable-next-line ts/no-explicit-any
|
|
7
|
+
async function onDrizzleInsert(data, tx) {
|
|
15
8
|
// @ts-expect-error drizzle types
|
|
16
|
-
await config.db.insert(config.table).values(data);
|
|
9
|
+
await (tx || config.db).insert(config.table).values(data);
|
|
17
10
|
}
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
.set(changes)
|
|
22
|
-
.where(eq(config.primaryColumn, id));
|
|
11
|
+
// eslint-disable-next-line ts/no-explicit-any
|
|
12
|
+
async function onDrizzleUpdate(id, changes, tx) {
|
|
13
|
+
await (tx || config.db).update(config.table).set(changes).where(eq(config.primaryColumn, id));
|
|
23
14
|
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
.where(inArray(config.primaryColumn, ids));
|
|
15
|
+
// eslint-disable-next-line ts/no-explicit-any
|
|
16
|
+
async function onDrizzleDelete(ids, tx) {
|
|
17
|
+
await (tx || config.db).delete(config.table).where(inArray(config.primaryColumn, ids));
|
|
28
18
|
}
|
|
29
19
|
const getSyncParams = async () => {
|
|
30
20
|
const params = await syncParams;
|
|
@@ -50,6 +40,15 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
50
40
|
collection: params.collection,
|
|
51
41
|
};
|
|
52
42
|
};
|
|
43
|
+
// Mutations should run if everything is okay inside "on" handlers
|
|
44
|
+
async function runMutations(mutations) {
|
|
45
|
+
const { begin, write, commit } = await syncParams;
|
|
46
|
+
begin();
|
|
47
|
+
mutations.forEach((m) => {
|
|
48
|
+
write({ type: m.type, value: m.modified });
|
|
49
|
+
});
|
|
50
|
+
commit();
|
|
51
|
+
}
|
|
53
52
|
return {
|
|
54
53
|
startSync: true,
|
|
55
54
|
sync: {
|
|
@@ -76,23 +75,26 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
76
75
|
gcTime: 0,
|
|
77
76
|
schema: createSelectSchema(config.table),
|
|
78
77
|
getKey: t => t[config.primaryColumn.name],
|
|
79
|
-
onDelete: async (params) => {
|
|
80
|
-
await onDrizzleDelete(params.transaction.mutations.map(m => m.key));
|
|
81
|
-
const result = await config.onDelete?.(params);
|
|
82
|
-
await runMutations(params.transaction.mutations);
|
|
83
|
-
return result;
|
|
84
|
-
},
|
|
85
78
|
onInsert: async (params) => {
|
|
86
|
-
await
|
|
87
|
-
|
|
79
|
+
await config.db.transaction(async (tx) => {
|
|
80
|
+
await onDrizzleInsert(params.transaction.mutations.map(m => m.modified), tx);
|
|
81
|
+
await config.onInsert?.(params);
|
|
82
|
+
});
|
|
88
83
|
await runMutations(params.transaction.mutations);
|
|
89
|
-
return result;
|
|
90
84
|
},
|
|
91
85
|
onUpdate: async (params) => {
|
|
92
|
-
await
|
|
93
|
-
|
|
86
|
+
await config.db.transaction(async (tx) => {
|
|
87
|
+
await Promise.all(params.transaction.mutations.map(m => onDrizzleUpdate(m.key, m.changes, tx)));
|
|
88
|
+
await config.onUpdate?.(params);
|
|
89
|
+
});
|
|
90
|
+
await runMutations(params.transaction.mutations);
|
|
91
|
+
},
|
|
92
|
+
onDelete: async (params) => {
|
|
93
|
+
await config.db.transaction(async (tx) => {
|
|
94
|
+
await onDrizzleDelete(params.transaction.mutations.map(m => m.key), tx);
|
|
95
|
+
await config.onDelete?.(params);
|
|
96
|
+
});
|
|
94
97
|
await runMutations(params.transaction.mutations);
|
|
95
|
-
return result;
|
|
96
98
|
},
|
|
97
99
|
utils: {
|
|
98
100
|
runSync: async () => {
|