diff --git a/.changeset/op-sqlite-parallel-read-connections.md b/.changeset/op-sqlite-parallel-read-connections.md new file mode 100644 index 000000000..002153c47 --- /dev/null +++ b/.changeset/op-sqlite-parallel-read-connections.md @@ -0,0 +1,5 @@ +--- +'@powersync/react-native': patch +--- + +Configure the op-sqlite read connections concurrently during initialization instead of one after another, reducing time to database readiness on cold start. diff --git a/packages/react-native/src/db/adapters/op-sqlite/OPSqliteAdapter.ts b/packages/react-native/src/db/adapters/op-sqlite/OPSqliteAdapter.ts index c87c90ba7..feebd1753 100644 --- a/packages/react-native/src/db/adapters/op-sqlite/OPSqliteAdapter.ts +++ b/packages/react-native/src/db/adapters/op-sqlite/OPSqliteAdapter.ts @@ -74,14 +74,15 @@ export class OPSQLiteDBAdapter extends DBAdapter { } } - const underlyingReadConnections = []; - for (let i = 0; i < READ_CONNECTIONS; i++) { - const conn = await this.openConnection(true, dbFilename); - for (let statement of baseStatements) { - await conn.execute(statement); - } - underlyingReadConnections.push(conn); - } + const underlyingReadConnections = await Promise.all( + Array.from({ length: READ_CONNECTIONS }, async () => { + const conn = await this.openConnection(true, dbFilename); + for (const statement of baseStatements) { + await conn.execute(statement); + } + return conn; + }) + ); this.writeConnection = new Semaphore([underlyingWriteConnection]); this.readConnections = new Semaphore(underlyingReadConnections);