Skip to content

Commit 070454a

Browse files
committed
Cleanup write connection initialization
1 parent c18e00c commit 070454a

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

.changeset/strong-hotels-deliver.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/op-sqlite': patch
3+
---
4+
5+
Encryption for databases using SQLCipher.

packages/powersync-op-sqlite/src/db/OPSqliteAdapter.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)