@@ -53,7 +53,7 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
53
53
const { lockTimeoutMs, journalMode, journalSizeLimit, synchronous, encryptionKey } = this . options . sqliteOptions ;
54
54
const dbFilename = this . options . name ;
55
55
56
- const DB : DB = this . openDatabase ( dbFilename , encryptionKey ) ;
56
+ this . writeConnection = await this . openConnection ( dbFilename ) ;
57
57
58
58
const statements : string [ ] = [
59
59
`PRAGMA busy_timeout = ${ lockTimeoutMs } ` ,
@@ -65,7 +65,7 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
65
65
for ( const statement of statements ) {
66
66
for ( let tries = 0 ; tries < 30 ; tries ++ ) {
67
67
try {
68
- await DB . execute ( statement ) ;
68
+ await this . writeConnection ! . execute ( statement ) ;
69
69
break ;
70
70
} catch ( e : any ) {
71
71
if ( e instanceof Error && e . message . includes ( 'database is locked' ) && tries < 29 ) {
@@ -77,9 +77,11 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
77
77
}
78
78
}
79
79
80
- this . loadExtension ( DB ) ;
81
-
82
- await DB . execute ( 'SELECT powersync_init()' ) ;
80
+ await this . writeConnection ! . execute ( 'SELECT powersync_init()' ) ;
81
+ // Changes should only occur in the write connection
82
+ this . writeConnection ! . registerListener ( {
83
+ tablesUpdated : ( notification ) => this . iterateListeners ( ( cb ) => cb . tablesUpdated ?.( notification ) )
84
+ } ) ;
83
85
84
86
this . readConnections = [ ] ;
85
87
for ( let i = 0 ; i < READ_CONNECTIONS ; i ++ ) {
@@ -89,19 +91,11 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
89
91
await conn . execute ( 'PRAGMA query_only = true' ) ;
90
92
this . readConnections . push ( conn ) ;
91
93
}
92
-
93
- this . writeConnection = new OPSQLiteConnection ( {
94
- baseDB : DB
95
- } ) ;
96
-
97
- // Changes should only occur in the write connection
98
- this . writeConnection ! . registerListener ( {
99
- tablesUpdated : ( notification ) => this . iterateListeners ( ( cb ) => cb . tablesUpdated ?.( notification ) )
100
- } ) ;
101
94
}
102
95
103
96
protected async openConnection ( filenameOverride ?: string ) : Promise < OPSQLiteConnection > {
104
- const DB : DB = this . openDatabase ( filenameOverride ?? this . options . name , this . options . sqliteOptions . encryptionKey ) ;
97
+ const dbFilename = filenameOverride ?? this . options . name ;
98
+ const DB : DB = this . openDatabase ( dbFilename , this . options . sqliteOptions . encryptionKey ) ;
105
99
106
100
//Load extension for all connections
107
101
this . loadExtension ( DB ) ;
0 commit comments