Skip to content

Commit d20c76d

Browse files
committed
Add temporaryStorage and cacheSizeKb for op-sqlite.
1 parent 78bfc2d commit d20c76d

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,28 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
4444
}
4545

4646
protected async init() {
47-
const { lockTimeoutMs, journalMode, journalSizeLimit, synchronous } = this.options.sqliteOptions!;
47+
const { lockTimeoutMs, journalMode, journalSizeLimit, synchronous, cacheSizeKb, temporaryStorage } =
48+
this.options.sqliteOptions!;
4849
const dbFilename = this.options.name;
4950

5051
this.writeConnection = await this.openConnection(dbFilename);
5152

52-
const statements: string[] = [
53+
const baseStatements = [
5354
`PRAGMA busy_timeout = ${lockTimeoutMs}`,
55+
`PRAGMA cache_size = -${cacheSizeKb}`,
56+
`PRAGMA temp_store = ${temporaryStorage}`
57+
];
58+
59+
const writeConnectionStatements = [
60+
...baseStatements,
5461
`PRAGMA journal_mode = ${journalMode}`,
5562
`PRAGMA journal_size_limit = ${journalSizeLimit}`,
5663
`PRAGMA synchronous = ${synchronous}`
5764
];
5865

59-
for (const statement of statements) {
66+
const readConnectionStatements = [...baseStatements, 'PRAGMA query_only = true'];
67+
68+
for (const statement of writeConnectionStatements) {
6069
for (let tries = 0; tries < 30; tries++) {
6170
try {
6271
await this.writeConnection!.execute(statement);
@@ -79,7 +88,9 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
7988
this.readConnections = [];
8089
for (let i = 0; i < READ_CONNECTIONS; i++) {
8190
const conn = await this.openConnection(dbFilename);
82-
await conn.execute('PRAGMA query_only = true');
91+
for (let statement of readConnectionStatements) {
92+
await conn.execute(statement);
93+
}
8394
this.readConnections.push({ busy: false, connection: conn });
8495
}
8596
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,21 @@ export interface SqliteOptions {
3030
*/
3131
encryptionKey?: string | null;
3232

33+
/**
34+
* Where to store SQLite temporary files. Defaults to 'MEMORY'.
35+
* Setting this to `FILESYSTEM` can cause issues with larger queries or datasets.
36+
*
37+
* For details, see: https://www.sqlite.org/pragma.html#pragma_temp_store
38+
*/
39+
temporaryStorage?: TemporaryStorageOption;
40+
41+
/**
42+
* Maximum SQLite cache size. Defaults to 50MB.
43+
*
44+
* For details, see: https://www.sqlite.org/pragma.html#pragma_cache_size
45+
*/
46+
cacheSizeKb?: number;
47+
3348
/**
3449
* Load extensions using the path and entryPoint.
3550
* More info can be found here https://op-engineering.github.io/op-sqlite/docs/api#loading-extensions.
@@ -40,6 +55,11 @@ export interface SqliteOptions {
4055
}>;
4156
}
4257

58+
export enum TemporaryStorageOption {
59+
MEMORY = 'memory',
60+
FILESYSTEM = 'file'
61+
}
62+
4363
// SQLite journal mode. Set on the primary connection.
4464
// This library is written with WAL mode in mind - other modes may cause
4565
// unexpected locking behavior.
@@ -65,6 +85,8 @@ export const DEFAULT_SQLITE_OPTIONS: Required<SqliteOptions> = {
6585
journalMode: SqliteJournalMode.wal,
6686
synchronous: SqliteSynchronous.normal,
6787
journalSizeLimit: 6 * 1024 * 1024,
88+
cacheSizeKb: 50 * 1024,
89+
temporaryStorage: TemporaryStorageOption.MEMORY,
6890
lockTimeoutMs: 30000,
6991
encryptionKey: null,
7092
extensions: []

tools/diagnostics-app/src/library/powersync/ConnectionManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ export const schemaManager = new DynamicSchemaManager();
2828
export const db = new PowerSyncDatabase({
2929
database: {
3030
dbFilename: 'example.db',
31-
debugMode: true
31+
debugMode: true,
32+
cacheSizeKb: 500000
3233
},
3334
schema: schemaManager.buildSchema()
3435
});
35-
db.execute('PRAGMA cache_size=-500000');
3636

3737
export const connector = new TokenConnector();
3838

0 commit comments

Comments
 (0)