tanstack-db-pglite 1.3.0 → 1.3.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/dist/drizzle.d.ts +10 -1
- package/dist/drizzle.js +28 -5
- package/dist/sql.d.ts +10 -1
- package/dist/sql.js +27 -4
- package/package.json +1 -1
package/dist/drizzle.d.ts
CHANGED
|
@@ -5,12 +5,21 @@ import type { PgliteDatabase } from 'drizzle-orm/pglite';
|
|
|
5
5
|
import type { PgliteUtils } from './utils';
|
|
6
6
|
type Schema<Table extends PgTable> = StandardSchemaV1<Table['$inferSelect'], Table['$inferSelect']>;
|
|
7
7
|
type SyncParams<Table extends PgTable> = Parameters<SyncConfig<Table['$inferSelect'], string>['sync']>[0];
|
|
8
|
+
/**
|
|
9
|
+
* Creates collection options backed by Drizzle ORM on PGlite.
|
|
10
|
+
*
|
|
11
|
+
* The adapter loads initial data from PGlite before invoking the user-provided `sync` callback.
|
|
12
|
+
* If your sync establishes a real-time subscription, events emitted during the initial PGlite
|
|
13
|
+
* read may be missed. To avoid data loss, subscribe to real-time events first and buffer them
|
|
14
|
+
* until `markReady()` is called.
|
|
15
|
+
*/
|
|
8
16
|
export declare function drizzleCollectionOptions<Table extends PgTable>({ startSync, ...config }: {
|
|
9
17
|
db: PgliteDatabase<any>;
|
|
10
18
|
startSync?: boolean;
|
|
11
19
|
table: Table;
|
|
12
20
|
primaryColumn: IndexColumn;
|
|
13
|
-
|
|
21
|
+
rowUpdateMode?: 'partial' | 'full';
|
|
22
|
+
sync?: (params: Pick<SyncParams<Table>, 'write' | 'collection' | 'markReady' | 'metadata'>) => Promise<(() => void) | void>;
|
|
14
23
|
prepare?: () => Promise<unknown> | unknown;
|
|
15
24
|
onInsert?: (params: InsertMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
|
16
25
|
onUpdate?: (params: UpdateMutationFnParams<Table['$inferSelect'], string>) => Promise<void>;
|
package/dist/drizzle.js
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
1
1
|
import { BasicIndex } from '@tanstack/db';
|
|
2
2
|
import { eq, inArray } from 'drizzle-orm';
|
|
3
3
|
import { createSelectSchema } from 'drizzle-zod';
|
|
4
|
+
/**
|
|
5
|
+
* Creates collection options backed by Drizzle ORM on PGlite.
|
|
6
|
+
*
|
|
7
|
+
* The adapter loads initial data from PGlite before invoking the user-provided `sync` callback.
|
|
8
|
+
* If your sync establishes a real-time subscription, events emitted during the initial PGlite
|
|
9
|
+
* read may be missed. To avoid data loss, subscribe to real-time events first and buffer them
|
|
10
|
+
* until `markReady()` is called.
|
|
11
|
+
*/
|
|
4
12
|
export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
5
13
|
// Sync params can be null while running PGLite migrations
|
|
6
14
|
const { promise: syncParams, resolve: resolveSyncParams } = Promise.withResolvers();
|
|
@@ -62,13 +70,16 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
62
70
|
.from(config.table)
|
|
63
71
|
.where(eq(config.primaryColumn, m.key))));
|
|
64
72
|
}
|
|
65
|
-
const sync = async () => {
|
|
73
|
+
const sync = async (force = false) => {
|
|
66
74
|
const params = await syncParams;
|
|
75
|
+
if (force) {
|
|
76
|
+
params.truncate();
|
|
77
|
+
}
|
|
67
78
|
if (!config.sync) {
|
|
68
79
|
params.markReady();
|
|
69
80
|
return;
|
|
70
81
|
}
|
|
71
|
-
|
|
82
|
+
return config.sync({
|
|
72
83
|
write: async (message) => {
|
|
73
84
|
if (message.type === 'insert') {
|
|
74
85
|
await onDrizzleInsert([message.value]);
|
|
@@ -86,15 +97,18 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
86
97
|
},
|
|
87
98
|
collection: params.collection,
|
|
88
99
|
markReady: params.markReady,
|
|
100
|
+
...(params.metadata && { metadata: params.metadata }),
|
|
89
101
|
});
|
|
90
102
|
};
|
|
91
103
|
return {
|
|
92
|
-
startSync
|
|
104
|
+
startSync,
|
|
93
105
|
autoIndex: 'eager',
|
|
94
106
|
defaultIndexType: BasicIndex,
|
|
95
107
|
sync: {
|
|
108
|
+
...(config.rowUpdateMode && { rowUpdateMode: config.rowUpdateMode }),
|
|
96
109
|
sync: (params) => {
|
|
97
110
|
resolveSyncParams(params);
|
|
111
|
+
let cleanup;
|
|
98
112
|
(async () => {
|
|
99
113
|
await config.prepare?.();
|
|
100
114
|
// @ts-expect-error drizzle types
|
|
@@ -105,9 +119,18 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
105
119
|
});
|
|
106
120
|
params.commit();
|
|
107
121
|
if (startSync) {
|
|
108
|
-
await sync();
|
|
122
|
+
const result = await sync();
|
|
123
|
+
if (typeof result === 'function') {
|
|
124
|
+
cleanup = result;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
params.markReady();
|
|
109
129
|
}
|
|
110
130
|
})();
|
|
131
|
+
return () => {
|
|
132
|
+
cleanup?.();
|
|
133
|
+
};
|
|
111
134
|
},
|
|
112
135
|
},
|
|
113
136
|
schema: createSelectSchema(config.table),
|
|
@@ -149,7 +172,7 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
149
172
|
}
|
|
150
173
|
const params = await syncParams;
|
|
151
174
|
await params.collection.stateWhenReady();
|
|
152
|
-
await sync();
|
|
175
|
+
await sync(true);
|
|
153
176
|
},
|
|
154
177
|
},
|
|
155
178
|
};
|
package/dist/sql.d.ts
CHANGED
|
@@ -5,15 +5,24 @@ import type { CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams,
|
|
|
5
5
|
import type { PgliteUtils } from './utils';
|
|
6
6
|
type Output<T extends StandardSchemaV1> = StandardSchemaV1.InferOutput<T>;
|
|
7
7
|
type SyncParams<ItemType extends Record<string, unknown>> = Parameters<SyncConfig<ItemType, string>['sync']>[0];
|
|
8
|
+
/**
|
|
9
|
+
* Creates collection options backed by raw SQL on PGlite.
|
|
10
|
+
*
|
|
11
|
+
* The adapter loads initial data from PGlite before invoking the user-provided `sync` callback.
|
|
12
|
+
* If your sync establishes a real-time subscription, events emitted during the initial PGlite
|
|
13
|
+
* read may be missed. To avoid data loss, subscribe to real-time events first and buffer them
|
|
14
|
+
* until `markReady()` is called.
|
|
15
|
+
*/
|
|
8
16
|
export declare function sqlCollectionOptions<Schema extends StandardSchemaV1<Record<string, unknown>>>({ startSync, ...config }: {
|
|
9
17
|
db: PGlite | PGliteWorker;
|
|
10
18
|
startSync?: boolean;
|
|
11
19
|
tableName: string;
|
|
12
20
|
primaryKeyColumn: Extract<keyof Output<Schema>, string>;
|
|
21
|
+
rowUpdateMode?: 'partial' | 'full';
|
|
13
22
|
schema: Schema;
|
|
14
23
|
getKey?: (row: Output<Schema>) => string;
|
|
15
24
|
prepare?: () => Promise<unknown> | unknown;
|
|
16
|
-
sync?: (params: Pick<SyncParams<Output<Schema>>, 'write' | 'collection' | 'markReady'>) => Promise<void>;
|
|
25
|
+
sync?: (params: Pick<SyncParams<Output<Schema>>, 'write' | 'collection' | 'markReady' | 'metadata'>) => Promise<(() => void) | void>;
|
|
17
26
|
onInsert?: (params: InsertMutationFnParams<Output<Schema>, string>) => Promise<void>;
|
|
18
27
|
onUpdate?: (params: UpdateMutationFnParams<Output<Schema>, string>) => Promise<void>;
|
|
19
28
|
onDelete?: (params: DeleteMutationFnParams<Output<Schema>, string>) => Promise<void>;
|
package/dist/sql.js
CHANGED
|
@@ -3,6 +3,14 @@ function quoteId(name) {
|
|
|
3
3
|
// eslint-disable-next-line e18e/prefer-static-regex
|
|
4
4
|
return `"${String(name).replace(/"/g, '""')}"`;
|
|
5
5
|
}
|
|
6
|
+
/**
|
|
7
|
+
* Creates collection options backed by raw SQL on PGlite.
|
|
8
|
+
*
|
|
9
|
+
* The adapter loads initial data from PGlite before invoking the user-provided `sync` callback.
|
|
10
|
+
* If your sync establishes a real-time subscription, events emitted during the initial PGlite
|
|
11
|
+
* read may be missed. To avoid data loss, subscribe to real-time events first and buffer them
|
|
12
|
+
* until `markReady()` is called.
|
|
13
|
+
*/
|
|
6
14
|
export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
7
15
|
const table = quoteId(config.tableName);
|
|
8
16
|
const primaryKey = quoteId(config.primaryKeyColumn);
|
|
@@ -49,13 +57,16 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
49
57
|
});
|
|
50
58
|
commit();
|
|
51
59
|
}
|
|
52
|
-
const sync = async () => {
|
|
60
|
+
const sync = async (force = false) => {
|
|
53
61
|
const params = await syncParams;
|
|
62
|
+
if (force) {
|
|
63
|
+
params.truncate();
|
|
64
|
+
}
|
|
54
65
|
if (!config.sync) {
|
|
55
66
|
params.markReady();
|
|
56
67
|
return;
|
|
57
68
|
}
|
|
58
|
-
|
|
69
|
+
return config.sync({
|
|
59
70
|
write: async (p) => {
|
|
60
71
|
if (p.type === 'insert') {
|
|
61
72
|
await runInsert(config.db, [p.value]);
|
|
@@ -73,6 +84,7 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
73
84
|
},
|
|
74
85
|
collection: params.collection,
|
|
75
86
|
markReady: params.markReady,
|
|
87
|
+
...(params.metadata && { metadata: params.metadata }),
|
|
76
88
|
});
|
|
77
89
|
};
|
|
78
90
|
return {
|
|
@@ -80,8 +92,10 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
80
92
|
autoIndex: 'eager',
|
|
81
93
|
defaultIndexType: BasicIndex,
|
|
82
94
|
sync: {
|
|
95
|
+
...(config.rowUpdateMode && { rowUpdateMode: config.rowUpdateMode }),
|
|
83
96
|
sync: (params) => {
|
|
84
97
|
resolveSyncParams(params);
|
|
98
|
+
let cleanup;
|
|
85
99
|
(async () => {
|
|
86
100
|
await config.prepare?.();
|
|
87
101
|
const rows = await runSelect(config.db);
|
|
@@ -91,9 +105,18 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
91
105
|
});
|
|
92
106
|
params.commit();
|
|
93
107
|
if (startSync) {
|
|
94
|
-
await sync();
|
|
108
|
+
const result = await sync();
|
|
109
|
+
if (typeof result === 'function') {
|
|
110
|
+
cleanup = result;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
params.markReady();
|
|
95
115
|
}
|
|
96
116
|
})();
|
|
117
|
+
return () => {
|
|
118
|
+
cleanup?.();
|
|
119
|
+
};
|
|
97
120
|
},
|
|
98
121
|
},
|
|
99
122
|
schema: config.schema,
|
|
@@ -132,7 +155,7 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
132
155
|
}
|
|
133
156
|
const params = await syncParams;
|
|
134
157
|
await params.collection.stateWhenReady();
|
|
135
|
-
await sync();
|
|
158
|
+
await sync(true);
|
|
136
159
|
},
|
|
137
160
|
},
|
|
138
161
|
};
|