1
- import { AbstractPowerSyncDatabase , QueryResult } from '@powersync/common' ;
1
+ import { LockContext , QueryResult } from '@powersync/common' ;
2
2
import { entityKind } from 'drizzle-orm/entity' ;
3
3
import type { Logger } from 'drizzle-orm/logger' ;
4
4
import { NoopLogger } from 'drizzle-orm/logger' ;
5
5
import type { RelationalSchemaConfig , TablesRelationalConfig } from 'drizzle-orm/relations' ;
6
- import { type Query , sql } from 'drizzle-orm/sql/sql' ;
6
+ import { type Query } from 'drizzle-orm/sql/sql' ;
7
7
import type { SQLiteAsyncDialect } from 'drizzle-orm/sqlite-core/dialect' ;
8
8
import type { SelectedFieldsOrdered } from 'drizzle-orm/sqlite-core/query-builders/select.types' ;
9
9
import {
@@ -13,7 +13,7 @@ import {
13
13
SQLiteTransaction ,
14
14
type SQLiteTransactionConfig
15
15
} from 'drizzle-orm/sqlite-core/session' ;
16
- import { PowerSyncSQLitePreparedQuery } from './sqlite-query ' ;
16
+ import { PowerSyncSQLitePreparedQuery } from './PowerSyncSQLitePreparedQuery ' ;
17
17
18
18
export interface PowerSyncSQLiteSessionOptions {
19
19
logger ?: Logger ;
@@ -30,19 +30,19 @@ export class PowerSyncSQLiteTransaction<
30
30
static readonly [ entityKind ] : string = 'PowerSyncSQLiteTransaction' ;
31
31
}
32
32
33
- export class PowerSyncSQLiteSession <
33
+ export class PowerSyncSQLiteBaseSession <
34
34
TFullSchema extends Record < string , unknown > ,
35
35
TSchema extends TablesRelationalConfig
36
36
> extends SQLiteSession < 'async' , QueryResult , TFullSchema , TSchema > {
37
- static readonly [ entityKind ] : string = 'PowerSyncSQLiteSession ' ;
37
+ static readonly [ entityKind ] : string = 'PowerSyncSQLiteBaseSession ' ;
38
38
39
- private logger : Logger ;
39
+ protected logger : Logger ;
40
40
41
41
constructor (
42
- private db : AbstractPowerSyncDatabase ,
43
- dialect : SQLiteAsyncDialect ,
44
- private schema : RelationalSchemaConfig < TSchema > | undefined ,
45
- options : PowerSyncSQLiteSessionOptions = { }
42
+ protected db : LockContext ,
43
+ protected dialect : SQLiteAsyncDialect ,
44
+ protected schema : RelationalSchemaConfig < TSchema > | undefined ,
45
+ protected options : PowerSyncSQLiteSessionOptions = { }
46
46
) {
47
47
super ( dialect ) ;
48
48
this . logger = options . logger ?? new NoopLogger ( ) ;
@@ -66,33 +66,10 @@ export class PowerSyncSQLiteSession<
66
66
) ;
67
67
}
68
68
69
- override transaction < T > (
70
- transaction : ( tx : PowerSyncSQLiteTransaction < TFullSchema , TSchema > ) => T ,
71
- config : PowerSyncSQLiteTransactionConfig = { }
69
+ transaction < T > (
70
+ _transaction : ( tx : PowerSyncSQLiteTransaction < TFullSchema , TSchema > ) => T ,
71
+ _config : PowerSyncSQLiteTransactionConfig = { }
72
72
) : T {
73
- const { accessMode = 'read write' } = config ;
74
-
75
- if ( accessMode === 'read only' ) {
76
- return this . db . readLock ( async ( ) => this . internalTransaction ( transaction , config ) ) as T ;
77
- }
78
-
79
- return this . db . writeLock ( async ( ) => this . internalTransaction ( transaction , config ) ) as T ;
80
- }
81
-
82
- async internalTransaction < T > (
83
- transaction : ( tx : PowerSyncSQLiteTransaction < TFullSchema , TSchema > ) => T ,
84
- config : PowerSyncSQLiteTransactionConfig = { }
85
- ) : Promise < T > {
86
- const tx = new PowerSyncSQLiteTransaction ( 'async' , ( this as any ) . dialect , this , this . schema ) ;
87
-
88
- await this . run ( sql . raw ( `begin${ config ?. behavior ? ' ' + config . behavior : '' } ` ) ) ;
89
- try {
90
- const result = await transaction ( tx ) ;
91
- await this . run ( sql `commit` ) ;
92
- return result ;
93
- } catch ( err ) {
94
- await this . run ( sql `rollback` ) ;
95
- throw err ;
96
- }
73
+ throw new Error ( 'Nested transactions are not supported' ) ;
97
74
}
98
75
}
0 commit comments