Skip to content

Commit 06a6a74

Browse files
committed
Introducing WIP compilableQueryWatch().
1 parent 42a6294 commit 06a6a74

File tree

5 files changed

+241
-204
lines changed

5 files changed

+241
-204
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { CompilableQuery } from './../types/types.js';
2+
import { AbstractPowerSyncDatabase, SQLWatchOptions } from './AbstractPowerSyncDatabase.js';
3+
import { runOnSchemaChange } from './runOnSchemaChange.js';
4+
5+
export interface CompilableQueryWatchHandler<T> {
6+
onResult: (results: T[]) => void;
7+
onError?: (error: Error) => void;
8+
}
9+
10+
export function compilableQueryWatch<T>(
11+
db: AbstractPowerSyncDatabase,
12+
query: CompilableQuery<T>,
13+
handler: CompilableQueryWatchHandler<T>,
14+
options?: SQLWatchOptions
15+
): void {
16+
const { onResult, onError = (e: Error) => {} } = handler ?? {};
17+
if (!onResult) {
18+
throw new Error('onResult is required');
19+
}
20+
21+
const watchQuery = async (abortSignal: AbortSignal) => {
22+
try {
23+
const toSql = query.compile();
24+
const resolvedTables = await db.resolveTables(toSql.sql, toSql.parameters as [], options);
25+
26+
// Fetch initial data
27+
const result = await query.execute();
28+
onResult(result);
29+
30+
db.onChangeWithCallback(
31+
{
32+
onChange: async () => {
33+
try {
34+
const result = await query.execute();
35+
onResult(result);
36+
} catch (error: any) {
37+
onError(error);
38+
}
39+
},
40+
onError
41+
},
42+
{
43+
...(options ?? {}),
44+
tables: resolvedTables,
45+
// Override the abort signal since we intercept it
46+
signal: abortSignal
47+
}
48+
);
49+
} catch (error: any) {
50+
onError(error);
51+
}
52+
};
53+
54+
runOnSchemaChange(watchQuery, db, options);
55+
}

packages/common/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './client/connection/PowerSyncBackendConnector.js';
55
export * from './client/connection/PowerSyncCredentials.js';
66
export * from './client/sync/bucket/BucketStorageAdapter.js';
77
export { runOnSchemaChange } from './client/runOnSchemaChange.js';
8+
export { CompilableQueryWatchHandler, compilableQueryWatch } from './client/compilableQueryWatch.js';
89
export { UpdateType, CrudEntry, OpId } from './client/sync/bucket/CrudEntry.js';
910
export * from './client/sync/bucket/SqliteBucketStorage.js';
1011
export * from './client/sync/bucket/CrudBatch.js';
Lines changed: 14 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {
22
AbstractPowerSyncDatabase,
3+
compilableQueryWatch,
4+
CompilableQueryWatchHandler,
35
QueryResult,
46
runOnSchemaChange,
57
SQLWatchOptions,
@@ -19,8 +21,9 @@ import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core/db';
1921
import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core/dialect';
2022
import type { DrizzleConfig } from 'drizzle-orm/utils';
2123
import { PowerSyncSQLiteSession, PowerSyncSQLiteTransactionConfig } from './sqlite-session';
24+
import { toCompilableQuery } from 'src/utils/compilableQuery';
2225

23-
type WatchQuery = { toSQL(): Query; execute(): Promise<any> };
26+
type WatchQuery<T> = { toSQL(): Query; execute(): Promise<T> };
2427

2528
export interface PowerSyncSQLiteDatabase<TSchema extends Record<string, unknown> = Record<string, never>>
2629
extends BaseSQLiteDatabase<'async', QueryResult, TSchema> {
@@ -31,7 +34,7 @@ export interface PowerSyncSQLiteDatabase<TSchema extends Record<string, unknown>
3134
config?: PowerSyncSQLiteTransactionConfig
3235
): Promise<T>;
3336

34-
watch(query: WatchQuery, handler?: WatchHandler, options?: SQLWatchOptions): void;
37+
watch<T>(query: WatchQuery<T>, handler?: CompilableQueryWatchHandler<T>, options?: SQLWatchOptions): void;
3538
}
3639

3740
export function wrapPowerSyncWithDrizzle<TSchema extends Record<string, unknown> = Record<string, never>>(
@@ -60,50 +63,17 @@ export function wrapPowerSyncWithDrizzle<TSchema extends Record<string, unknown>
6063
logger
6164
});
6265

63-
const watch = (query: WatchQuery, handler?: WatchHandler, options?: SQLWatchOptions): void => {
64-
const { onResult, onError = (e: Error) => {} } = handler ?? {};
65-
if (!onResult) {
66-
throw new Error('onResult is required');
67-
}
68-
69-
const watchQuery = async (abortSignal: AbortSignal) => {
70-
try {
71-
const toSql = query.toSQL();
72-
const resolvedTables = await db.resolveTables(toSql.sql, toSql.params, options);
73-
74-
// Fetch initial data
75-
const result = await query.execute();
76-
onResult(result);
77-
78-
db.onChangeWithCallback(
79-
{
80-
onChange: async () => {
81-
try {
82-
const result = await query.execute();
83-
onResult(result);
84-
} catch (error: any) {
85-
onError(error);
86-
}
87-
},
88-
onError
89-
},
90-
{
91-
...(options ?? {}),
92-
tables: resolvedTables,
93-
// Override the abort signal since we intercept it
94-
signal: abortSignal
95-
}
96-
);
97-
} catch (error: any) {
98-
onError(error);
99-
}
100-
};
101-
102-
runOnSchemaChange(watchQuery, db, options);
103-
};
66+
// const watch = <T>(
67+
// query: WatchQuery<T>,
68+
// handler?: CompilableQueryWatchHandler<T>,
69+
// options?: SQLWatchOptions
70+
// ): void => {
71+
// compilableQueryWatch(db, toCompilableQuery<T>(query), handler, options);
72+
// };
10473

10574
const baseDatabase = new BaseSQLiteDatabase('async', dialect, session, schema) as PowerSyncSQLiteDatabase<TSchema>;
10675
return Object.assign(baseDatabase, {
107-
watch: (query: WatchQuery, handler?: WatchHandler, options?: SQLWatchOptions) => watch(query, handler, options)
76+
watch: <T>(query: WatchQuery<T>, handler?: CompilableQueryWatchHandler<T>, options?: SQLWatchOptions) => {}
77+
// watch(query, handler, options)
10878
});
10979
}

packages/drizzle-driver/src/utils/compilableQuery.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ import { Query } from 'drizzle-orm';
2020
* </View>
2121
* );
2222
*/
23-
export function toCompilableQuery<T>(query: {
24-
execute: () => Promise<T | T[]>;
25-
toSQL: () => Query;
26-
}): CompilableQuery<T> {
23+
export function toCompilableQuery<T>(query: { execute: () => Promise<T[]>; toSQL: () => Query }): CompilableQuery<T> {
2724
return {
2825
compile: () => {
2926
const sql = query.toSQL();

0 commit comments

Comments
 (0)