tanstack-db-pglite 1.1.5 → 1.1.7

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
@@ -1,46 +1,21 @@
1
- import type { DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
1
+ import type { StandardSchemaV1 } from '@standard-schema/spec';
2
+ import type { CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
2
3
  import type { IndexColumn, PgTable } from 'drizzle-orm/pg-core';
3
4
  import type { PgliteDatabase } from 'drizzle-orm/pglite';
4
- import type { BuildSchema } from 'drizzle-zod';
5
+ import type { PgliteUtils } from './utils';
6
+ type Schema<Table extends PgTable> = StandardSchemaV1<Table['$inferSelect'], Table['$inferSelect']>;
5
7
  type SyncParams<Table extends PgTable> = Parameters<SyncConfig<Table['$inferSelect'], string>['sync']>[0];
6
8
  export declare function drizzleCollectionOptions<Table extends PgTable>({ startSync, ...config }: {
7
9
  db: PgliteDatabase<any>;
10
+ startSync?: boolean;
8
11
  table: Table;
9
12
  primaryColumn: IndexColumn;
13
+ sync?: (params: Pick<SyncParams<Table>, 'write' | 'collection'>) => Promise<void>;
14
+ prepare?: () => Promise<unknown> | unknown;
10
15
  onInsert?: (params: InsertMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
11
16
  onUpdate?: (params: UpdateMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
12
17
  onDelete?: (params: DeleteMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
13
- startSync?: boolean;
14
- prepare?: () => Promise<unknown> | unknown;
15
- sync?: (params: Pick<SyncParams<Table>, 'write' | 'collection'>) => Promise<void>;
16
- }): {
17
- startSync: true;
18
- sync: {
19
- sync: (params: {
20
- collection: import("@tanstack/db").Collection<Table["$inferSelect"], string, any, any, any>;
21
- begin: (options?: {
22
- immediate?: boolean;
23
- }) => void;
24
- write: (message: import("@tanstack/db").ChangeMessageOrDeleteKeyMessage<Table["$inferSelect"], string>) => void;
25
- commit: () => void;
26
- markReady: () => void;
27
- truncate: () => void;
28
- }) => void;
29
- };
30
- gcTime: number;
31
- schema: BuildSchema<"select", Table["_"]["columns"], undefined, undefined>;
32
- getKey: (t: Table["$inferSelect"]) => string;
33
- onInsert: (params: InsertMutationFnParams<Table["$inferSelect"], string, {
34
- runSync: () => Promise<void>;
35
- }>) => Promise<void>;
36
- onUpdate: (params: UpdateMutationFnParams<Table["$inferSelect"], string, {
37
- runSync: () => Promise<void>;
38
- }>) => Promise<void>;
39
- onDelete: (params: DeleteMutationFnParams<Table["$inferSelect"], string, {
40
- runSync: () => Promise<void>;
41
- }>) => Promise<void>;
42
- utils: {
43
- runSync: () => Promise<void>;
44
- };
18
+ }): CollectionConfig<Table['$inferSelect'], string, Schema<Table>, PgliteUtils> & {
19
+ schema: Schema<Table>;
45
20
  };
46
21
  export {};
package/dist/drizzle.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { eq, inArray } from 'drizzle-orm';
2
2
  import { createSelectSchema } from 'drizzle-zod';
3
3
  export function drizzleCollectionOptions({ startSync = true, ...config }) {
4
+ let resolvers = Promise.withResolvers();
4
5
  // Sync params can be null while running PGLite migrations
5
6
  const { promise: syncParams, resolve: resolveSyncParams } = Promise.withResolvers();
6
7
  // eslint-disable-next-line ts/no-explicit-any
@@ -86,12 +87,12 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
86
87
  .from(config.table)
87
88
  .where(eq(config.primaryColumn, m.key))));
88
89
  }
89
- const schema = createSelectSchema(config.table);
90
90
  return {
91
91
  startSync: true,
92
92
  sync: {
93
93
  sync: (params) => {
94
94
  resolveSyncParams(params);
95
+ resolvers = Promise.withResolvers();
95
96
  (async () => {
96
97
  try {
97
98
  await config.prepare?.();
@@ -104,6 +105,7 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
104
105
  params.commit();
105
106
  if (config.sync && startSync) {
106
107
  await config.sync(await getSyncParams());
108
+ resolvers.resolve(undefined);
107
109
  }
108
110
  }
109
111
  finally {
@@ -113,7 +115,7 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
113
115
  },
114
116
  },
115
117
  gcTime: 0,
116
- schema,
118
+ schema: createSelectSchema(config.table),
117
119
  getKey: t => t[config.primaryColumn.name],
118
120
  onInsert: async (params) => {
119
121
  await config.db.transaction(async (tx) => {
@@ -155,6 +157,9 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
155
157
  await params.collection.stateWhenReady();
156
158
  await config.sync(params);
157
159
  },
160
+ waitForSync: async () => {
161
+ await resolvers.promise;
162
+ },
158
163
  },
159
164
  };
160
165
  }
package/dist/sql.d.ts CHANGED
@@ -1,49 +1,23 @@
1
1
  import type { PGlite } from '@electric-sql/pglite';
2
2
  import type { PGliteWorker } from '@electric-sql/pglite/worker';
3
3
  import type { StandardSchemaV1 } from '@standard-schema/spec';
4
- import type { DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
4
+ import type { CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
5
+ import type { PgliteUtils } from './utils';
5
6
  type Output<T extends StandardSchemaV1> = StandardSchemaV1.InferOutput<T>;
6
7
  type SyncParams<ItemType extends Record<string, unknown>> = Parameters<SyncConfig<ItemType, string>['sync']>[0];
7
8
  export declare function sqlCollectionOptions<Schema extends StandardSchemaV1<Record<string, unknown>>>({ startSync, ...config }: {
8
9
  db: PGlite | PGliteWorker;
10
+ startSync?: boolean;
9
11
  tableName: string;
10
12
  primaryKeyColumn: Extract<keyof Output<Schema>, string>;
11
13
  schema: Schema;
12
14
  getKey?: (row: Output<Schema>) => string;
15
+ prepare?: () => Promise<unknown> | unknown;
16
+ sync?: (params: Pick<SyncParams<Output<Schema>>, 'write' | 'collection'>) => Promise<void>;
13
17
  onInsert?: (params: InsertMutationFnParams<Output<Schema>, string>) => Promise<void>;
14
18
  onUpdate?: (params: UpdateMutationFnParams<Output<Schema>, string>) => Promise<void>;
15
19
  onDelete?: (params: DeleteMutationFnParams<Output<Schema>, string>) => Promise<void>;
16
- startSync?: boolean;
17
- prepare?: () => Promise<unknown> | unknown;
18
- sync?: (params: Pick<SyncParams<Output<Schema>>, 'write' | 'collection'>) => Promise<void>;
19
- }): {
20
- startSync: boolean;
21
- sync: {
22
- sync: (params: {
23
- collection: import("@tanstack/db").Collection<Output<Schema>, string, any, any, any>;
24
- begin: (options?: {
25
- immediate?: boolean;
26
- }) => void;
27
- write: (message: import("@tanstack/db").ChangeMessageOrDeleteKeyMessage<Output<Schema>, string>) => void;
28
- commit: () => void;
29
- markReady: () => void;
30
- truncate: () => void;
31
- }) => void;
32
- };
33
- gcTime: number;
34
- schema: Schema;
35
- getKey: (row: Output<Schema>) => string;
36
- onInsert: (params: InsertMutationFnParams<Output<Schema>, string, {
37
- runSync: () => Promise<void>;
38
- }>) => Promise<void>;
39
- onUpdate: (params: UpdateMutationFnParams<Output<Schema>, string, {
40
- runSync: () => Promise<void>;
41
- }>) => Promise<void>;
42
- onDelete: (params: DeleteMutationFnParams<Output<Schema>, string, {
43
- runSync: () => Promise<void>;
44
- }>) => Promise<void>;
45
- utils: {
46
- runSync: () => Promise<void>;
47
- };
20
+ }): CollectionConfig<Output<Schema>, string, Schema, PgliteUtils> & {
21
+ schema: typeof config.schema;
48
22
  };
49
23
  export {};
package/dist/sql.js CHANGED
@@ -3,6 +3,7 @@ function quoteId(name) {
3
3
  return `"${String(name).replace(/"/g, '""')}"`;
4
4
  }
5
5
  export function sqlCollectionOptions({ startSync = true, ...config }) {
6
+ let resolvers = Promise.withResolvers();
6
7
  const table = quoteId(config.tableName);
7
8
  const primaryKey = quoteId(config.primaryKeyColumn);
8
9
  const getKey = config.getKey ?? ((row) => String(row[config.primaryKeyColumn]));
@@ -78,6 +79,7 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
78
79
  sync: {
79
80
  sync: (params) => {
80
81
  resolveSyncParams(params);
82
+ resolvers = Promise.withResolvers();
81
83
  (async () => {
82
84
  try {
83
85
  await config.prepare?.();
@@ -89,6 +91,7 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
89
91
  params.commit();
90
92
  if (config.sync && startSync) {
91
93
  await config.sync(await getSyncParams());
94
+ resolvers.resolve(undefined);
92
95
  }
93
96
  }
94
97
  finally {
@@ -136,6 +139,9 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
136
139
  await params.collection.stateWhenReady();
137
140
  await config.sync(params);
138
141
  },
142
+ waitForSync: async () => {
143
+ await resolvers.promise;
144
+ },
139
145
  },
140
146
  };
141
147
  }
@@ -0,0 +1,5 @@
1
+ import type { UtilsRecord } from '@tanstack/db';
2
+ export interface PgliteUtils extends UtilsRecord {
3
+ runSync: () => Promise<void>;
4
+ waitForSync: () => Promise<void>;
5
+ }
package/dist/utils.js ADDED
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tanstack-db-pglite",
3
- "version": "1.1.5",
3
+ "version": "1.1.7",
4
4
  "packageManager": "pnpm@10.32.1",
5
5
  "description": "",
6
6
  "author": "Valerii Strilets",