-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added
watch()
to Drizzle and Kysely integrations (#414)
- Loading branch information
1 parent
a919243
commit 77a9ed2
Showing
11 changed files
with
702 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@powersync/drizzle-driver': minor | ||
--- | ||
|
||
Added `watch()` function to Drizzle wrapper to support watched queries. This function invokes `execute()` on the Drizzle query which improves support for complex queries such as those which are relational. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@powersync/kysely-driver': minor | ||
--- | ||
|
||
Added `watch()` function to Kysely wrapper to support watched queries. This function invokes `execute()` on the Kysely query which improves support for complex queries and Kysely plugins. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@powersync/common': minor | ||
--- | ||
|
||
Added `compilableQueryWatch()` utility function which allows any compilable query to be watched. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { CompilableQuery } from './../types/types.js'; | ||
import { AbstractPowerSyncDatabase, SQLWatchOptions } from './AbstractPowerSyncDatabase.js'; | ||
import { runOnSchemaChange } from './runOnSchemaChange.js'; | ||
|
||
export interface CompilableQueryWatchHandler<T> { | ||
onResult: (results: T[]) => void; | ||
onError?: (error: Error) => void; | ||
} | ||
|
||
export function compilableQueryWatch<T>( | ||
db: AbstractPowerSyncDatabase, | ||
query: CompilableQuery<T>, | ||
handler: CompilableQueryWatchHandler<T>, | ||
options?: SQLWatchOptions | ||
): void { | ||
const { onResult, onError = (e: Error) => {} } = handler ?? {}; | ||
if (!onResult) { | ||
throw new Error('onResult is required'); | ||
} | ||
|
||
const watchQuery = async (abortSignal: AbortSignal) => { | ||
try { | ||
const toSql = query.compile(); | ||
const resolvedTables = await db.resolveTables(toSql.sql, toSql.parameters as [], options); | ||
|
||
// Fetch initial data | ||
const result = await query.execute(); | ||
onResult(result); | ||
|
||
db.onChangeWithCallback( | ||
{ | ||
onChange: async () => { | ||
try { | ||
const result = await query.execute(); | ||
onResult(result); | ||
} catch (error: any) { | ||
onError(error); | ||
} | ||
}, | ||
onError | ||
}, | ||
{ | ||
...(options ?? {}), | ||
tables: resolvedTables, | ||
// Override the abort signal since we intercept it | ||
signal: abortSignal | ||
} | ||
); | ||
} catch (error: any) { | ||
onError(error); | ||
} | ||
}; | ||
|
||
runOnSchemaChange(watchQuery, db, options); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import { wrapPowerSyncWithDrizzle, type PowerSyncSQLiteDatabase } from './sqlite/db'; | ||
import { wrapPowerSyncWithDrizzle, type DrizzleQuery, type PowerSyncSQLiteDatabase } from './sqlite/db'; | ||
import { toCompilableQuery } from './utils/compilableQuery'; | ||
|
||
export { wrapPowerSyncWithDrizzle, toCompilableQuery, PowerSyncSQLiteDatabase }; | ||
export { wrapPowerSyncWithDrizzle, toCompilableQuery, DrizzleQuery, PowerSyncSQLiteDatabase }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.