@@ -53,7 +53,7 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
5353 const { lockTimeoutMs, journalMode, journalSizeLimit, synchronous, encryptionKey } = this . options . sqliteOptions ;
5454 const dbFilename = this . options . name ;
5555
56- const DB : DB = this . openDatabase ( dbFilename , encryptionKey ) ;
56+ this . writeConnection = await this . openConnection ( dbFilename ) ;
5757
5858 const statements : string [ ] = [
5959 `PRAGMA busy_timeout = ${ lockTimeoutMs } ` ,
@@ -65,7 +65,7 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
6565 for ( const statement of statements ) {
6666 for ( let tries = 0 ; tries < 30 ; tries ++ ) {
6767 try {
68- await DB . execute ( statement ) ;
68+ await this . writeConnection ! . execute ( statement ) ;
6969 break ;
7070 } catch ( e : any ) {
7171 if ( e instanceof Error && e . message . includes ( 'database is locked' ) && tries < 29 ) {
@@ -77,9 +77,11 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
7777 }
7878 }
7979
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+ } ) ;
8385
8486 this . readConnections = [ ] ;
8587 for ( let i = 0 ; i < READ_CONNECTIONS ; i ++ ) {
@@ -89,19 +91,11 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
8991 await conn . execute ( 'PRAGMA query_only = true' ) ;
9092 this . readConnections . push ( conn ) ;
9193 }
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- } ) ;
10194 }
10295
10396 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 ) ;
10599
106100 //Load extension for all connections
107101 this . loadExtension ( DB ) ;
0 commit comments