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' ;
2
9
import { DefaultLogger } from 'drizzle-orm/logger' ;
3
10
import {
4
11
createTableRelationsHelpers ,
@@ -11,42 +18,60 @@ import { SQLiteTransaction } from 'drizzle-orm/sqlite-core';
11
18
import { BaseSQLiteDatabase } from 'drizzle-orm/sqlite-core/db' ;
12
19
import { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core/dialect' ;
13
20
import type { DrizzleConfig } from 'drizzle-orm/utils' ;
21
+ import { toCompilableQuery } from './../utils/compilableQuery' ;
14
22
import { PowerSyncSQLiteSession , PowerSyncSQLiteTransactionConfig } from './sqlite-session' ;
15
23
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 > (
19
59
transaction : (
20
60
tx : SQLiteTransaction < 'async' , QueryResult , TSchema , ExtractTablesWithRelations < TSchema > >
21
61
) => Promise < T > ,
22
62
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
+ }
24
70
}
25
71
26
72
export function wrapPowerSyncWithDrizzle < TSchema extends Record < string , unknown > = Record < string , never > > (
27
73
db : AbstractPowerSyncDatabase ,
28
74
config : DrizzleConfig < TSchema > = { }
29
75
) : 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 ) ;
52
77
}
0 commit comments