@powersync/common 1.21.0 → 1.22.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.
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { CompilableQuery } from './../types/types.js';
|
|
2
|
+
import { AbstractPowerSyncDatabase, SQLWatchOptions } from './AbstractPowerSyncDatabase.js';
|
|
3
|
+
export interface CompilableQueryWatchHandler<T> {
|
|
4
|
+
onResult: (results: T[]) => void;
|
|
5
|
+
onError?: (error: Error) => void;
|
|
6
|
+
}
|
|
7
|
+
export declare function compilableQueryWatch<T>(db: AbstractPowerSyncDatabase, query: CompilableQuery<T>, handler: CompilableQueryWatchHandler<T>, options?: SQLWatchOptions): void;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { runOnSchemaChange } from './runOnSchemaChange.js';
|
|
2
|
+
export function compilableQueryWatch(db, query, handler, options) {
|
|
3
|
+
const { onResult, onError = (e) => { } } = handler ?? {};
|
|
4
|
+
if (!onResult) {
|
|
5
|
+
throw new Error('onResult is required');
|
|
6
|
+
}
|
|
7
|
+
const watchQuery = async (abortSignal) => {
|
|
8
|
+
try {
|
|
9
|
+
const toSql = query.compile();
|
|
10
|
+
const resolvedTables = await db.resolveTables(toSql.sql, toSql.parameters, options);
|
|
11
|
+
// Fetch initial data
|
|
12
|
+
const result = await query.execute();
|
|
13
|
+
onResult(result);
|
|
14
|
+
db.onChangeWithCallback({
|
|
15
|
+
onChange: async () => {
|
|
16
|
+
try {
|
|
17
|
+
const result = await query.execute();
|
|
18
|
+
onResult(result);
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
onError(error);
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
onError
|
|
25
|
+
}, {
|
|
26
|
+
...(options ?? {}),
|
|
27
|
+
tables: resolvedTables,
|
|
28
|
+
// Override the abort signal since we intercept it
|
|
29
|
+
signal: abortSignal
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
catch (error) {
|
|
33
|
+
onError(error);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
runOnSchemaChange(watchQuery, db, options);
|
|
37
|
+
}
|
package/lib/index.d.ts
CHANGED
|
@@ -5,6 +5,7 @@ export * from './client/connection/PowerSyncBackendConnector.js';
|
|
|
5
5
|
export * from './client/connection/PowerSyncCredentials.js';
|
|
6
6
|
export * from './client/sync/bucket/BucketStorageAdapter.js';
|
|
7
7
|
export { runOnSchemaChange } from './client/runOnSchemaChange.js';
|
|
8
|
+
export { CompilableQueryWatchHandler, compilableQueryWatch } from './client/compilableQueryWatch.js';
|
|
8
9
|
export { UpdateType, CrudEntry, OpId } from './client/sync/bucket/CrudEntry.js';
|
|
9
10
|
export * from './client/sync/bucket/SqliteBucketStorage.js';
|
|
10
11
|
export * from './client/sync/bucket/CrudBatch.js';
|
package/lib/index.js
CHANGED
|
@@ -5,6 +5,7 @@ export * from './client/connection/PowerSyncBackendConnector.js';
|
|
|
5
5
|
export * from './client/connection/PowerSyncCredentials.js';
|
|
6
6
|
export * from './client/sync/bucket/BucketStorageAdapter.js';
|
|
7
7
|
export { runOnSchemaChange } from './client/runOnSchemaChange.js';
|
|
8
|
+
export { compilableQueryWatch } from './client/compilableQueryWatch.js';
|
|
8
9
|
export { UpdateType, CrudEntry } from './client/sync/bucket/CrudEntry.js';
|
|
9
10
|
export * from './client/sync/bucket/SqliteBucketStorage.js';
|
|
10
11
|
export * from './client/sync/bucket/CrudBatch.js';
|