tanstack-db-pglite 1.3.0 → 1.3.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/dist/drizzle.d.ts +10 -1
- package/dist/drizzle.js +30 -5
- package/dist/sql.d.ts +10 -1
- package/dist/sql.js +29 -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,18 @@ 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.begin();
|
|
77
|
+
params.truncate();
|
|
78
|
+
params.commit();
|
|
79
|
+
}
|
|
67
80
|
if (!config.sync) {
|
|
68
81
|
params.markReady();
|
|
69
82
|
return;
|
|
70
83
|
}
|
|
71
|
-
|
|
84
|
+
return config.sync({
|
|
72
85
|
write: async (message) => {
|
|
73
86
|
if (message.type === 'insert') {
|
|
74
87
|
await onDrizzleInsert([message.value]);
|
|
@@ -86,15 +99,18 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
86
99
|
},
|
|
87
100
|
collection: params.collection,
|
|
88
101
|
markReady: params.markReady,
|
|
102
|
+
...(params.metadata && { metadata: params.metadata }),
|
|
89
103
|
});
|
|
90
104
|
};
|
|
91
105
|
return {
|
|
92
|
-
startSync
|
|
106
|
+
startSync,
|
|
93
107
|
autoIndex: 'eager',
|
|
94
108
|
defaultIndexType: BasicIndex,
|
|
95
109
|
sync: {
|
|
110
|
+
...(config.rowUpdateMode && { rowUpdateMode: config.rowUpdateMode }),
|
|
96
111
|
sync: (params) => {
|
|
97
112
|
resolveSyncParams(params);
|
|
113
|
+
let cleanup;
|
|
98
114
|
(async () => {
|
|
99
115
|
await config.prepare?.();
|
|
100
116
|
// @ts-expect-error drizzle types
|
|
@@ -105,9 +121,18 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
105
121
|
});
|
|
106
122
|
params.commit();
|
|
107
123
|
if (startSync) {
|
|
108
|
-
await sync();
|
|
124
|
+
const result = await sync();
|
|
125
|
+
if (typeof result === 'function') {
|
|
126
|
+
cleanup = result;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
params.markReady();
|
|
109
131
|
}
|
|
110
132
|
})();
|
|
133
|
+
return () => {
|
|
134
|
+
cleanup?.();
|
|
135
|
+
};
|
|
111
136
|
},
|
|
112
137
|
},
|
|
113
138
|
schema: createSelectSchema(config.table),
|
|
@@ -149,7 +174,7 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
149
174
|
}
|
|
150
175
|
const params = await syncParams;
|
|
151
176
|
await params.collection.stateWhenReady();
|
|
152
|
-
await sync();
|
|
177
|
+
await sync(true);
|
|
153
178
|
},
|
|
154
179
|
},
|
|
155
180
|
};
|
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,18 @@ 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.begin();
|
|
64
|
+
params.truncate();
|
|
65
|
+
params.commit();
|
|
66
|
+
}
|
|
54
67
|
if (!config.sync) {
|
|
55
68
|
params.markReady();
|
|
56
69
|
return;
|
|
57
70
|
}
|
|
58
|
-
|
|
71
|
+
return config.sync({
|
|
59
72
|
write: async (p) => {
|
|
60
73
|
if (p.type === 'insert') {
|
|
61
74
|
await runInsert(config.db, [p.value]);
|
|
@@ -73,6 +86,7 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
73
86
|
},
|
|
74
87
|
collection: params.collection,
|
|
75
88
|
markReady: params.markReady,
|
|
89
|
+
...(params.metadata && { metadata: params.metadata }),
|
|
76
90
|
});
|
|
77
91
|
};
|
|
78
92
|
return {
|
|
@@ -80,8 +94,10 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
80
94
|
autoIndex: 'eager',
|
|
81
95
|
defaultIndexType: BasicIndex,
|
|
82
96
|
sync: {
|
|
97
|
+
...(config.rowUpdateMode && { rowUpdateMode: config.rowUpdateMode }),
|
|
83
98
|
sync: (params) => {
|
|
84
99
|
resolveSyncParams(params);
|
|
100
|
+
let cleanup;
|
|
85
101
|
(async () => {
|
|
86
102
|
await config.prepare?.();
|
|
87
103
|
const rows = await runSelect(config.db);
|
|
@@ -91,9 +107,18 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
91
107
|
});
|
|
92
108
|
params.commit();
|
|
93
109
|
if (startSync) {
|
|
94
|
-
await sync();
|
|
110
|
+
const result = await sync();
|
|
111
|
+
if (typeof result === 'function') {
|
|
112
|
+
cleanup = result;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
params.markReady();
|
|
95
117
|
}
|
|
96
118
|
})();
|
|
119
|
+
return () => {
|
|
120
|
+
cleanup?.();
|
|
121
|
+
};
|
|
97
122
|
},
|
|
98
123
|
},
|
|
99
124
|
schema: config.schema,
|
|
@@ -132,7 +157,7 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
132
157
|
}
|
|
133
158
|
const params = await syncParams;
|
|
134
159
|
await params.collection.stateWhenReady();
|
|
135
|
-
await sync();
|
|
160
|
+
await sync(true);
|
|
136
161
|
},
|
|
137
162
|
},
|
|
138
163
|
};
|