tanstack-db-pglite 1.1.4 → 1.1.6
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 +8 -34
- package/dist/drizzle.js +10 -1
- package/dist/sql.d.ts +7 -33
- package/dist/sql.js +6 -0
- package/dist/utils.d.ts +5 -0
- package/dist/utils.js +1 -0
- package/package.json +1 -1
package/dist/drizzle.d.ts
CHANGED
|
@@ -1,46 +1,20 @@
|
|
|
1
|
-
import type { DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
|
|
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
|
-
import type {
|
|
4
|
+
import type { PgliteUtils } from './utils';
|
|
5
|
+
declare function getSchema<Table extends PgTable>(table: Table): import("drizzle-zod").BuildSchema<"select", Table["_"]["columns"], undefined, undefined>;
|
|
5
6
|
type SyncParams<Table extends PgTable> = Parameters<SyncConfig<Table['$inferSelect'], string>['sync']>[0];
|
|
6
7
|
export declare function drizzleCollectionOptions<Table extends PgTable>({ startSync, ...config }: {
|
|
7
8
|
db: PgliteDatabase<any>;
|
|
9
|
+
startSync?: boolean;
|
|
8
10
|
table: Table;
|
|
9
11
|
primaryColumn: IndexColumn;
|
|
12
|
+
sync?: (params: Pick<SyncParams<Table>, 'write' | 'collection'>) => Promise<void>;
|
|
13
|
+
prepare?: () => Promise<unknown> | unknown;
|
|
10
14
|
onInsert?: (params: InsertMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
11
15
|
onUpdate?: (params: UpdateMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
12
16
|
onDelete?: (params: DeleteMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
};
|
|
17
|
+
}): CollectionConfig<Table['$inferSelect'], string, ReturnType<typeof getSchema<Table>>, PgliteUtils> & {
|
|
18
|
+
schema: ReturnType<typeof getSchema<Table>>;
|
|
45
19
|
};
|
|
46
20
|
export {};
|
package/dist/drizzle.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { eq, inArray } from 'drizzle-orm';
|
|
2
2
|
import { createSelectSchema } from 'drizzle-zod';
|
|
3
|
+
function getSchema(table) {
|
|
4
|
+
return createSelectSchema(table);
|
|
5
|
+
}
|
|
3
6
|
export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
7
|
+
let resolvers = Promise.withResolvers();
|
|
4
8
|
// Sync params can be null while running PGLite migrations
|
|
5
9
|
const { promise: syncParams, resolve: resolveSyncParams } = Promise.withResolvers();
|
|
6
10
|
// eslint-disable-next-line ts/no-explicit-any
|
|
@@ -91,6 +95,7 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
91
95
|
sync: {
|
|
92
96
|
sync: (params) => {
|
|
93
97
|
resolveSyncParams(params);
|
|
98
|
+
resolvers = Promise.withResolvers();
|
|
94
99
|
(async () => {
|
|
95
100
|
try {
|
|
96
101
|
await config.prepare?.();
|
|
@@ -103,6 +108,7 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
103
108
|
params.commit();
|
|
104
109
|
if (config.sync && startSync) {
|
|
105
110
|
await config.sync(await getSyncParams());
|
|
111
|
+
resolvers.resolve(undefined);
|
|
106
112
|
}
|
|
107
113
|
}
|
|
108
114
|
finally {
|
|
@@ -112,7 +118,7 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
112
118
|
},
|
|
113
119
|
},
|
|
114
120
|
gcTime: 0,
|
|
115
|
-
schema:
|
|
121
|
+
schema: getSchema(config.table),
|
|
116
122
|
getKey: t => t[config.primaryColumn.name],
|
|
117
123
|
onInsert: async (params) => {
|
|
118
124
|
await config.db.transaction(async (tx) => {
|
|
@@ -154,6 +160,9 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
154
160
|
await params.collection.stateWhenReady();
|
|
155
161
|
await config.sync(params);
|
|
156
162
|
},
|
|
163
|
+
waitForSync: async () => {
|
|
164
|
+
await resolvers.promise;
|
|
165
|
+
},
|
|
157
166
|
},
|
|
158
167
|
};
|
|
159
168
|
}
|
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
|
-
|
|
17
|
-
|
|
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
|
}
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|