tanstack-db-pglite 1.0.0 → 1.0.2

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 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, providing powerful collection management with automatic synchronization and offline-first capabilities.
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<any> | any;
13
- sync: (params: Pick<Parameters<SyncConfig<Table['$inferSelect'], string>['sync']>[0], 'write' | 'collection'>) => Promise<void>;
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
- 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) {
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
- async function onDrizzleUpdate(id, changes) {
19
- await config.db
20
- .update(config.table)
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
- async function onDrizzleDelete(ids) {
25
- await config.db
26
- .delete(config.table)
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,26 +75,32 @@ 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 onDrizzleInsert(params.transaction.mutations.map(m => m.modified));
87
- const result = await config.onInsert?.(params);
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 Promise.all(params.transaction.mutations.map(m => onDrizzleUpdate(m.key, m.changes)));
93
- const result = await config.onUpdate?.(params);
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 () => {
101
+ if (!config.sync) {
102
+ throw new Error('Sync is not defined');
103
+ }
99
104
  const params = await getSyncParams();
100
105
  // To wait the first sync
101
106
  await params.collection.stateWhenReady();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tanstack-db-pglite",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "packageManager": "pnpm@10.16.1",
5
5
  "description": "",
6
6
  "author": "Valerii Strilets",