1- import { AbstractPowerSyncDatabase , QueryResult } from '@powersync/common' ;
1+ import {
2+ AbstractPowerSyncDatabase ,
3+ compilableQueryWatch ,
4+ CompilableQueryWatchHandler ,
5+ QueryResult ,
6+ SQLWatchOptions
7+ } from '@powersync/common' ;
8+ import { Query } from 'drizzle-orm' ;
29import { DefaultLogger } from 'drizzle-orm/logger' ;
310import {
411 createTableRelationsHelpers ,
@@ -11,42 +18,60 @@ import { SQLiteTransaction } from 'drizzle-orm/sqlite-core';
1118import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core/db' ;
1219import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core/dialect' ;
1320import type { DrizzleConfig } from 'drizzle-orm/utils' ;
21+ import { toCompilableQuery } from './../utils/compilableQuery' ;
1422import { PowerSyncSQLiteSession , PowerSyncSQLiteTransactionConfig } from './sqlite-session' ;
1523
16- export interface PowerSyncSQLiteDatabase < TSchema extends Record < string , unknown > = Record < string , never > >
17- extends BaseSQLiteDatabase < 'async' , QueryResult , TSchema > {
18- transaction < T > (
24+ export type DrizzleQuery < T > = { toSQL ( ) : Query ; execute ( ) : Promise < T > } ;
25+
26+ export class PowerSyncSQLiteDatabase <
27+ TSchema extends Record < string , unknown > = Record < string , never >
28+ > extends BaseSQLiteDatabase < 'async' , QueryResult , TSchema > {
29+ private db : AbstractPowerSyncDatabase ;
30+
31+ constructor ( db : AbstractPowerSyncDatabase , config : DrizzleConfig < TSchema > = { } ) {
32+ const dialect = new SQLiteAsyncDialect ( { casing : config . casing } ) ;
33+ let logger ;
34+ if ( config . logger === true ) {
35+ logger = new DefaultLogger ( ) ;
36+ } else if ( config . logger !== false ) {
37+ logger = config . logger ;
38+ }
39+
40+ let schema : RelationalSchemaConfig < TablesRelationalConfig > | undefined ;
41+ if ( config . schema ) {
42+ const tablesConfig = extractTablesRelationalConfig ( config . schema , createTableRelationsHelpers ) ;
43+ schema = {
44+ fullSchema : config . schema ,
45+ schema : tablesConfig . tables ,
46+ tableNamesMap : tablesConfig . tableNamesMap
47+ } ;
48+ }
49+
50+ const session = new PowerSyncSQLiteSession ( db , dialect , schema , {
51+ logger
52+ } ) ;
53+
54+ super ( 'async' , dialect , session as any , schema as any ) ;
55+ this . db = db ;
56+ }
57+
58+ override transaction < T > (
1959 transaction : (
2060 tx : SQLiteTransaction < 'async' , QueryResult , TSchema , ExtractTablesWithRelations < TSchema > >
2161 ) => Promise < T > ,
2262 config ?: PowerSyncSQLiteTransactionConfig
23- ) : Promise < T > ;
63+ ) : Promise < T > {
64+ return super . transaction ( transaction , config ) ;
65+ }
66+
67+ watch < T > ( query : DrizzleQuery < T > , handler : CompilableQueryWatchHandler < T > , options ?: SQLWatchOptions ) : void {
68+ compilableQueryWatch ( this . db , toCompilableQuery ( query ) , handler , options ) ;
69+ }
2470}
2571
2672export function wrapPowerSyncWithDrizzle < TSchema extends Record < string , unknown > = Record < string , never > > (
2773 db : AbstractPowerSyncDatabase ,
2874 config : DrizzleConfig < TSchema > = { }
2975) : PowerSyncSQLiteDatabase < TSchema > {
30- const dialect = new SQLiteAsyncDialect ( { casing : config . casing } ) ;
31- let logger ;
32- if ( config . logger === true ) {
33- logger = new DefaultLogger ( ) ;
34- } else if ( config . logger !== false ) {
35- logger = config . logger ;
36- }
37-
38- let schema : RelationalSchemaConfig < TablesRelationalConfig > | undefined ;
39- if ( config . schema ) {
40- const tablesConfig = extractTablesRelationalConfig ( config . schema , createTableRelationsHelpers ) ;
41- schema = {
42- fullSchema : config . schema ,
43- schema : tablesConfig . tables ,
44- tableNamesMap : tablesConfig . tableNamesMap
45- } ;
46- }
47-
48- const session = new PowerSyncSQLiteSession ( db , dialect , schema , {
49- logger
50- } ) ;
51- return new BaseSQLiteDatabase ( 'async' , dialect , session , schema ) as PowerSyncSQLiteDatabase < TSchema > ;
76+ return new PowerSyncSQLiteDatabase < TSchema > ( db , config ) ;
5277}
0 commit comments