tanstack-db-pglite 1.0.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/LICENSE +21 -0
- package/README.md +61 -0
- package/dist/drizzle-collection-options.d.ts +20 -0
- package/dist/drizzle-collection-options.js +106 -0
- package/dist/drizzle.d.ts +18 -0
- package/dist/drizzle.js +106 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Valerii Strilets
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# tanstack-db-pglite
|
|
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, providing powerful collection management with automatic synchronization and offline-first capabilities.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install tanstack-db-pglite @tanstack/db drizzle-orm @electric-sql/pglite
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
> **Note:** `@tanstack/db` and `drizzle-orm` are peer dependencies and must be installed separately.
|
|
12
|
+
|
|
13
|
+
## Quick Start
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { PGLite } from '@electric-sql/pglite'
|
|
17
|
+
import { createCollection } from '@tanstack/react-db'
|
|
18
|
+
import { drizzle } from 'drizzle-orm/pglite'
|
|
19
|
+
import { drizzleCollectionOptions } from 'tanstack-db-pglite'
|
|
20
|
+
import { chats } from '~/drizzle'
|
|
21
|
+
|
|
22
|
+
const pglite = new PGLite()
|
|
23
|
+
const db = drizzle(pglite)
|
|
24
|
+
|
|
25
|
+
export const chatsCollection = createCollection(drizzleCollectionOptions({
|
|
26
|
+
db,
|
|
27
|
+
table: chats,
|
|
28
|
+
primaryColumn: chats.id,
|
|
29
|
+
prepare: async () => {
|
|
30
|
+
// Prepare your database before starting the collection (e.g., run migrations)
|
|
31
|
+
await waitForMigrations()
|
|
32
|
+
},
|
|
33
|
+
sync: async ({ collection, write }) => {
|
|
34
|
+
// Send some data to your backend to sync and receive the response
|
|
35
|
+
const sync = await syncWithCloud(
|
|
36
|
+
collection.toArray.map(c => ({
|
|
37
|
+
id: c.id,
|
|
38
|
+
updatedAt: c.updatedAt
|
|
39
|
+
}))
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
sync.forEach((item) => {
|
|
43
|
+
if (item.type === 'delete') {
|
|
44
|
+
write({ type: 'delete', value: collection.get(item.value)! })
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
write(item)
|
|
48
|
+
}
|
|
49
|
+
})
|
|
50
|
+
},
|
|
51
|
+
onInsert: async (params) => {
|
|
52
|
+
await saveInCloud(params)
|
|
53
|
+
},
|
|
54
|
+
onUpdate: async (params) => {
|
|
55
|
+
await updateInCloud(params)
|
|
56
|
+
},
|
|
57
|
+
onDelete: async (params) => {
|
|
58
|
+
await deleteInCloud(params)
|
|
59
|
+
},
|
|
60
|
+
}))
|
|
61
|
+
```
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
|
|
2
|
+
import type { IndexColumn, PgTable } from 'drizzle-orm/pg-core';
|
|
3
|
+
import type { PgliteDatabase } from 'drizzle-orm/pglite';
|
|
4
|
+
export declare function drizzleCollectionOptions<Table extends PgTable>({ startSync, ...config }: {
|
|
5
|
+
db: PgliteDatabase<any>;
|
|
6
|
+
table: Table;
|
|
7
|
+
primaryColumn: IndexColumn;
|
|
8
|
+
onInsert?: (params: InsertMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
9
|
+
onUpdate?: (params: UpdateMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
10
|
+
onDelete?: (params: DeleteMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
11
|
+
startSync?: boolean;
|
|
12
|
+
sync: {
|
|
13
|
+
prepare?: () => Promise<any> | any;
|
|
14
|
+
sync: (params: Pick<Parameters<SyncConfig<Table['$inferSelect'], string>['sync']>[0], 'write' | 'collection'>) => Promise<void>;
|
|
15
|
+
};
|
|
16
|
+
}): CollectionConfig<Table['$inferSelect'], string> & {
|
|
17
|
+
utils: {
|
|
18
|
+
runSync: () => Promise<void>;
|
|
19
|
+
};
|
|
20
|
+
};
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { eq, inArray } from 'drizzle-orm';
|
|
2
|
+
import { createSelectSchema } from 'drizzle-zod';
|
|
3
|
+
export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
4
|
+
// Sync params can be null while running PGLite migrations
|
|
5
|
+
const { promise: syncParams, resolve: resolveSyncParams } = Promise.withResolvers();
|
|
6
|
+
async function runMutations(mutations) {
|
|
7
|
+
const { begin, write, commit } = await syncParams;
|
|
8
|
+
begin();
|
|
9
|
+
mutations.forEach((m) => {
|
|
10
|
+
write({ type: m.type, value: m.modified });
|
|
11
|
+
});
|
|
12
|
+
commit();
|
|
13
|
+
}
|
|
14
|
+
async function onDrizzleInsert(data) {
|
|
15
|
+
// @ts-expect-error drizzle types
|
|
16
|
+
await config.db.insert(config.table).values(data);
|
|
17
|
+
}
|
|
18
|
+
async function onDrizzleUpdate(id, changes) {
|
|
19
|
+
await config.db
|
|
20
|
+
.update(config.table)
|
|
21
|
+
.set(changes)
|
|
22
|
+
.where(eq(config.primaryColumn, id));
|
|
23
|
+
}
|
|
24
|
+
async function onDrizzleDelete(ids) {
|
|
25
|
+
await config.db
|
|
26
|
+
.delete(config.table)
|
|
27
|
+
.where(inArray(config.primaryColumn, ids));
|
|
28
|
+
}
|
|
29
|
+
const getSyncParams = async () => {
|
|
30
|
+
const params = await syncParams;
|
|
31
|
+
return {
|
|
32
|
+
write: async (p) => {
|
|
33
|
+
params.begin();
|
|
34
|
+
try {
|
|
35
|
+
if (p.type === 'insert') {
|
|
36
|
+
await onDrizzleInsert([p.value]);
|
|
37
|
+
}
|
|
38
|
+
else if (p.type === 'update') {
|
|
39
|
+
await onDrizzleUpdate(params.collection.getKeyFromItem(p.value), p.value);
|
|
40
|
+
}
|
|
41
|
+
else if (p.type === 'delete') {
|
|
42
|
+
await onDrizzleDelete([params.collection.getKeyFromItem(p.value)]);
|
|
43
|
+
}
|
|
44
|
+
params.write(p);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
params.commit();
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
collection: params.collection,
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
startSync: true,
|
|
55
|
+
sync: {
|
|
56
|
+
sync: async (params) => {
|
|
57
|
+
try {
|
|
58
|
+
resolveSyncParams(params);
|
|
59
|
+
await config.sync.prepare?.();
|
|
60
|
+
params.begin();
|
|
61
|
+
// @ts-expect-error drizzle types
|
|
62
|
+
const dbs = await config.db.select().from(config.table);
|
|
63
|
+
dbs.forEach((db) => {
|
|
64
|
+
params.write({ type: 'insert', value: db });
|
|
65
|
+
});
|
|
66
|
+
params.commit();
|
|
67
|
+
if (config.sync && startSync) {
|
|
68
|
+
await config.sync.sync(await getSyncParams());
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
params.markReady();
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
gcTime: 0,
|
|
77
|
+
schema: createSelectSchema(config.table),
|
|
78
|
+
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
|
+
onInsert: async (params) => {
|
|
86
|
+
await onDrizzleInsert(params.transaction.mutations.map(m => m.modified));
|
|
87
|
+
const result = await config.onInsert?.(params);
|
|
88
|
+
await runMutations(params.transaction.mutations);
|
|
89
|
+
return result;
|
|
90
|
+
},
|
|
91
|
+
onUpdate: async (params) => {
|
|
92
|
+
await Promise.all(params.transaction.mutations.map(m => onDrizzleUpdate(m.key, m.changes)));
|
|
93
|
+
const result = await config.onUpdate?.(params);
|
|
94
|
+
await runMutations(params.transaction.mutations);
|
|
95
|
+
return result;
|
|
96
|
+
},
|
|
97
|
+
utils: {
|
|
98
|
+
runSync: async () => {
|
|
99
|
+
const params = await getSyncParams();
|
|
100
|
+
// To wait the first sync
|
|
101
|
+
await params.collection.stateWhenReady();
|
|
102
|
+
await config.sync.sync(params);
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
|
|
2
|
+
import type { IndexColumn, PgTable } from 'drizzle-orm/pg-core';
|
|
3
|
+
import type { PgliteDatabase } from 'drizzle-orm/pglite';
|
|
4
|
+
export declare function drizzleCollectionOptions<Table extends PgTable>({ startSync, ...config }: {
|
|
5
|
+
db: PgliteDatabase<any>;
|
|
6
|
+
table: Table;
|
|
7
|
+
primaryColumn: IndexColumn;
|
|
8
|
+
onInsert?: (params: InsertMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
9
|
+
onUpdate?: (params: UpdateMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
10
|
+
onDelete?: (params: DeleteMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
11
|
+
startSync?: boolean;
|
|
12
|
+
prepare?: () => Promise<any> | any;
|
|
13
|
+
sync: (params: Pick<Parameters<SyncConfig<Table['$inferSelect'], string>['sync']>[0], 'write' | 'collection'>) => Promise<void>;
|
|
14
|
+
}): CollectionConfig<Table['$inferSelect'], string> & {
|
|
15
|
+
utils: {
|
|
16
|
+
runSync: () => Promise<void>;
|
|
17
|
+
};
|
|
18
|
+
};
|
package/dist/drizzle.js
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { eq, inArray } from 'drizzle-orm';
|
|
2
|
+
import { createSelectSchema } from 'drizzle-zod';
|
|
3
|
+
export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
4
|
+
// Sync params can be null while running PGLite migrations
|
|
5
|
+
const { promise: syncParams, resolve: resolveSyncParams } = Promise.withResolvers();
|
|
6
|
+
async function runMutations(mutations) {
|
|
7
|
+
const { begin, write, commit } = await syncParams;
|
|
8
|
+
begin();
|
|
9
|
+
mutations.forEach((m) => {
|
|
10
|
+
write({ type: m.type, value: m.modified });
|
|
11
|
+
});
|
|
12
|
+
commit();
|
|
13
|
+
}
|
|
14
|
+
async function onDrizzleInsert(data) {
|
|
15
|
+
// @ts-expect-error drizzle types
|
|
16
|
+
await config.db.insert(config.table).values(data);
|
|
17
|
+
}
|
|
18
|
+
async function onDrizzleUpdate(id, changes) {
|
|
19
|
+
await config.db
|
|
20
|
+
.update(config.table)
|
|
21
|
+
.set(changes)
|
|
22
|
+
.where(eq(config.primaryColumn, id));
|
|
23
|
+
}
|
|
24
|
+
async function onDrizzleDelete(ids) {
|
|
25
|
+
await config.db
|
|
26
|
+
.delete(config.table)
|
|
27
|
+
.where(inArray(config.primaryColumn, ids));
|
|
28
|
+
}
|
|
29
|
+
const getSyncParams = async () => {
|
|
30
|
+
const params = await syncParams;
|
|
31
|
+
return {
|
|
32
|
+
write: async (p) => {
|
|
33
|
+
params.begin();
|
|
34
|
+
try {
|
|
35
|
+
if (p.type === 'insert') {
|
|
36
|
+
await onDrizzleInsert([p.value]);
|
|
37
|
+
}
|
|
38
|
+
else if (p.type === 'update') {
|
|
39
|
+
await onDrizzleUpdate(params.collection.getKeyFromItem(p.value), p.value);
|
|
40
|
+
}
|
|
41
|
+
else if (p.type === 'delete') {
|
|
42
|
+
await onDrizzleDelete([params.collection.getKeyFromItem(p.value)]);
|
|
43
|
+
}
|
|
44
|
+
params.write(p);
|
|
45
|
+
}
|
|
46
|
+
finally {
|
|
47
|
+
params.commit();
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
collection: params.collection,
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
startSync: true,
|
|
55
|
+
sync: {
|
|
56
|
+
sync: async (params) => {
|
|
57
|
+
try {
|
|
58
|
+
resolveSyncParams(params);
|
|
59
|
+
await config.prepare?.();
|
|
60
|
+
params.begin();
|
|
61
|
+
// @ts-expect-error drizzle types
|
|
62
|
+
const dbs = await config.db.select().from(config.table);
|
|
63
|
+
dbs.forEach((db) => {
|
|
64
|
+
params.write({ type: 'insert', value: db });
|
|
65
|
+
});
|
|
66
|
+
params.commit();
|
|
67
|
+
if (config.sync && startSync) {
|
|
68
|
+
await config.sync(await getSyncParams());
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
params.markReady();
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
gcTime: 0,
|
|
77
|
+
schema: createSelectSchema(config.table),
|
|
78
|
+
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
|
+
onInsert: async (params) => {
|
|
86
|
+
await onDrizzleInsert(params.transaction.mutations.map(m => m.modified));
|
|
87
|
+
const result = await config.onInsert?.(params);
|
|
88
|
+
await runMutations(params.transaction.mutations);
|
|
89
|
+
return result;
|
|
90
|
+
},
|
|
91
|
+
onUpdate: async (params) => {
|
|
92
|
+
await Promise.all(params.transaction.mutations.map(m => onDrizzleUpdate(m.key, m.changes)));
|
|
93
|
+
const result = await config.onUpdate?.(params);
|
|
94
|
+
await runMutations(params.transaction.mutations);
|
|
95
|
+
return result;
|
|
96
|
+
},
|
|
97
|
+
utils: {
|
|
98
|
+
runSync: async () => {
|
|
99
|
+
const params = await getSyncParams();
|
|
100
|
+
// To wait the first sync
|
|
101
|
+
await params.collection.stateWhenReady();
|
|
102
|
+
await config.sync(params);
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
};
|
|
106
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './drizzle';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './drizzle';
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "tanstack-db-pglite",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"packageManager": "pnpm@10.16.1",
|
|
5
|
+
"description": "",
|
|
6
|
+
"author": "Valerii Strilets",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/letstri/tanstack-db-pglite.git",
|
|
11
|
+
"directory": "tanstack-db-pglite"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"tanstack",
|
|
15
|
+
"db",
|
|
16
|
+
"pglite",
|
|
17
|
+
"drizzle"
|
|
18
|
+
],
|
|
19
|
+
"main": "dist/index.js",
|
|
20
|
+
"types": "dist/index.d.ts",
|
|
21
|
+
"files": ["dist"],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"prepublishOnly": "pnpm build",
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"lint": "eslint .",
|
|
26
|
+
"lint:fix": "eslint . --fix"
|
|
27
|
+
},
|
|
28
|
+
"peerDependencies": {
|
|
29
|
+
"@tanstack/db": ">=0.2.0",
|
|
30
|
+
"drizzle-orm": ">=0.44.0"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"drizzle-zod": "^0.8.3"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@antfu/eslint-config": "^5.3.0",
|
|
37
|
+
"eslint": "^9.35.0",
|
|
38
|
+
"jiti": "^2.5.1",
|
|
39
|
+
"typescript": "^5.9.2"
|
|
40
|
+
}
|
|
41
|
+
}
|