tanstack-db-pglite 1.4.0 → 1.5.0
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 +4 -4
- package/dist/drizzle.d.ts +8 -2
- package/dist/drizzle.js +1 -3
- package/dist/sql.d.ts +8 -2
- package/dist/sql.js +1 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -38,12 +38,12 @@ export const chatsCollection = createCollection(drizzleCollectionOptions({
|
|
|
38
38
|
prepare: async () => {
|
|
39
39
|
await waitForMigrations()
|
|
40
40
|
},
|
|
41
|
-
sync: async ({
|
|
41
|
+
sync: async ({ writeAsync, markReady }) => {
|
|
42
42
|
const eventSource = new EventSource('/api/chats/sync')
|
|
43
43
|
|
|
44
44
|
eventSource.onmessage = (event) => {
|
|
45
45
|
const item = JSON.parse(event.data)
|
|
46
|
-
|
|
46
|
+
writeAsync(item)
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
eventSource.addEventListener('ready', () => markReady())
|
|
@@ -94,12 +94,12 @@ export const chatsCollection = createCollection(sqlCollectionOptions({
|
|
|
94
94
|
)
|
|
95
95
|
`)
|
|
96
96
|
},
|
|
97
|
-
sync: async ({
|
|
97
|
+
sync: async ({ writeAsync, markReady }) => {
|
|
98
98
|
const eventSource = new EventSource('/api/chats/sync')
|
|
99
99
|
|
|
100
100
|
eventSource.onmessage = (event) => {
|
|
101
101
|
const item = JSON.parse(event.data)
|
|
102
|
-
|
|
102
|
+
writeAsync(item)
|
|
103
103
|
}
|
|
104
104
|
|
|
105
105
|
eventSource.addEventListener('ready', () => markReady())
|
package/dist/drizzle.d.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
2
|
-
import type { CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
|
|
2
|
+
import type { ChangeMessageOrDeleteKeyMessage, CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
|
|
3
3
|
import type { IndexColumn, PgTable } from 'drizzle-orm/pg-core';
|
|
4
4
|
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
|
+
type CustomSyncParams<Table extends PgTable> = Pick<SyncParams<Table>, 'collection' | 'markReady' | 'metadata'> & {
|
|
9
|
+
/**
|
|
10
|
+
* We created async version of "write" callback due to synchronization with PGLite
|
|
11
|
+
*/
|
|
12
|
+
writeAsync: (message: ChangeMessageOrDeleteKeyMessage<Table['$inferSelect'], string>) => Promise<void>;
|
|
13
|
+
};
|
|
8
14
|
/**
|
|
9
15
|
* Creates collection options backed by Drizzle ORM on PGlite.
|
|
10
16
|
*
|
|
@@ -17,7 +23,7 @@ export declare function drizzleCollectionOptions<Table extends PgTable>({ startS
|
|
|
17
23
|
db: PgliteDatabase<any>;
|
|
18
24
|
startSync?: boolean;
|
|
19
25
|
sync?: {
|
|
20
|
-
sync: (params:
|
|
26
|
+
sync: (params: CustomSyncParams<Table>) => Promise<(() => void) | void>;
|
|
21
27
|
} & Omit<SyncConfig, 'sync'>;
|
|
22
28
|
table: Table;
|
|
23
29
|
primaryColumn: IndexColumn;
|
package/dist/drizzle.js
CHANGED
|
@@ -78,7 +78,7 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
78
78
|
return;
|
|
79
79
|
}
|
|
80
80
|
return config.sync.sync({
|
|
81
|
-
|
|
81
|
+
writeAsync: async (message) => {
|
|
82
82
|
if (message.type === 'insert') {
|
|
83
83
|
await onDrizzleInsert([message.value]);
|
|
84
84
|
}
|
|
@@ -167,8 +167,6 @@ export function drizzleCollectionOptions({ startSync = true, ...config }) {
|
|
|
167
167
|
}
|
|
168
168
|
syncCleanup?.();
|
|
169
169
|
syncCleanup = undefined;
|
|
170
|
-
const params = await syncParams;
|
|
171
|
-
await params.collection.stateWhenReady();
|
|
172
170
|
const result = await sync();
|
|
173
171
|
if (typeof result === 'function') {
|
|
174
172
|
syncCleanup = result;
|
package/dist/sql.d.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
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 { CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
|
|
4
|
+
import type { ChangeMessageOrDeleteKeyMessage, CollectionConfig, DeleteMutationFnParams, InsertMutationFnParams, SyncConfig, UpdateMutationFnParams } from '@tanstack/db';
|
|
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
|
+
type CustomSyncParams<ItemType extends Record<string, unknown>> = Pick<SyncParams<ItemType>, 'collection' | 'markReady' | 'metadata'> & {
|
|
9
|
+
/**
|
|
10
|
+
* We created async version of "write" callback due to synchronization with PGLite
|
|
11
|
+
*/
|
|
12
|
+
writeAsync: (message: ChangeMessageOrDeleteKeyMessage<ItemType, string>) => Promise<void>;
|
|
13
|
+
};
|
|
8
14
|
/**
|
|
9
15
|
* Creates collection options backed by raw SQL on PGlite.
|
|
10
16
|
*
|
|
@@ -17,7 +23,7 @@ export declare function sqlCollectionOptions<Schema extends StandardSchemaV1<Rec
|
|
|
17
23
|
db: PGlite | PGliteWorker;
|
|
18
24
|
startSync?: boolean;
|
|
19
25
|
sync?: {
|
|
20
|
-
sync: (params:
|
|
26
|
+
sync: (params: CustomSyncParams<Output<Schema>>) => Promise<(() => void) | void>;
|
|
21
27
|
} & Omit<SyncConfig, 'sync'>;
|
|
22
28
|
tableName: string;
|
|
23
29
|
primaryKeyColumn: Extract<keyof Output<Schema>, string>;
|
package/dist/sql.js
CHANGED
|
@@ -64,7 +64,7 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
64
64
|
return;
|
|
65
65
|
}
|
|
66
66
|
return config.sync.sync({
|
|
67
|
-
|
|
67
|
+
writeAsync: async (p) => {
|
|
68
68
|
if (p.type === 'insert') {
|
|
69
69
|
await runInsert(config.db, [p.value]);
|
|
70
70
|
}
|
|
@@ -150,8 +150,6 @@ export function sqlCollectionOptions({ startSync = true, ...config }) {
|
|
|
150
150
|
if (!config.sync) {
|
|
151
151
|
throw new Error('Sync is not defined');
|
|
152
152
|
}
|
|
153
|
-
const params = await syncParams;
|
|
154
|
-
await params.collection.stateWhenReady();
|
|
155
153
|
const result = await sync();
|
|
156
154
|
if (typeof result === 'function') {
|
|
157
155
|
syncCleanup = result;
|