Skip to content

Commit b161d93

Browse files
add WAL support
1 parent 45cc79f commit b161d93

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

packages/capacitor/src/adapter/CapacitorSQLiteAdapter.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import {
88
DBLockOptions,
99
LockContext,
1010
QueryResult,
11-
SQLOpenOptions,
1211
Transaction
1312
} from '@powersync/web';
1413
import Lock from 'async-lock';
1514
import { PowerSyncCore } from '../plugin/PowerSyncCore';
1615
import { messageForErrorCode } from '../plugin/PowerSyncPlugin';
16+
import { CapacitorSQLiteOpenFactoryOptions, DEFAULT_SQLITE_OPTIONS } from './CapacitorSQLiteOpenFactory';
1717

1818
/**
1919
* An implementation of {@link DBAdapter} using the Capacitor Community SQLite [plugin](https://github.com/capacitor-community/sqlite).
@@ -27,7 +27,7 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
2727
protected initializedPromise: Promise<void>;
2828
protected lock: Lock;
2929

30-
constructor(protected options: SQLOpenOptions) {
30+
constructor(protected options: CapacitorSQLiteOpenFactoryOptions) {
3131
super();
3232
this._writeConnection = null;
3333
this._readConnection = null;
@@ -72,11 +72,17 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
7272
this._writeConnection = await sqlite.createConnection(this.options.dbFilename, false, 'no-encryption', 1, false);
7373
this._readConnection = await sqlite.createConnection(this.options.dbFilename, false, 'no-encryption', 1, true);
7474

75-
// TODO validate WAL mode
7675
await this._writeConnection.open();
77-
await this._readConnection.open();
7876

79-
this.writeConnection.query("SELECT powersync_update_hooks('install')");
77+
const { cacheSizeKb, journalSizeLimit, synchronous } = { ...DEFAULT_SQLITE_OPTIONS, ...this.options.sqliteOptions };
78+
await this.writeConnection.query("SELECT powersync_update_hooks('install')");
79+
await this.writeConnection.query('PRAGMA journal_mode = WAL');
80+
await this.writeConnection.query(`PRAGMA journal_size_limit = ${journalSizeLimit}`);
81+
await this.writeConnection.query(`PRAGMA temp_store = memory`);
82+
await this.writeConnection.query(`PRAGMA synchronous = ${synchronous}`);
83+
await this.writeConnection.query(`PRAGMA cache_size = -${cacheSizeKb}`);
84+
85+
await this._readConnection.open();
8086
}
8187

8288
async close(): Promise<void> {

packages/capacitor/src/adapter/CapacitorSQLiteOpenFactory.ts

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,46 @@
11
import { DBAdapter, SQLOpenFactory, SQLOpenOptions } from '@powersync/web';
22
import { CapacitorSQLiteAdapter } from './CapacitorSQLiteAdapter';
33

4+
enum SqliteSynchronous {
5+
normal = 'NORMAL',
6+
full = 'FULL',
7+
off = 'OFF'
8+
}
9+
10+
export interface CapacitorSQLiteOptions {
11+
/**
12+
* Journal/WAL size limit. Defaults to 6MB.
13+
* The WAL may grow larger than this limit during writes, but SQLite will
14+
* attempt to truncate the file afterwards.
15+
*/
16+
journalSizeLimit?: number;
17+
18+
/**
19+
* SQLite synchronous flag. Defaults to [SqliteSynchronous.normal], which
20+
* is safe for WAL mode.
21+
*/
22+
synchronous?: SqliteSynchronous;
23+
24+
/**
25+
* Maximum SQLite cache size. Defaults to 50MB.
26+
*
27+
* For details, see: https://www.sqlite.org/pragma.html#pragma_cache_size
28+
*/
29+
cacheSizeKb?: number;
30+
}
31+
32+
export interface CapacitorSQLiteOpenFactoryOptions extends SQLOpenOptions {
33+
sqliteOptions?: CapacitorSQLiteOptions;
34+
}
35+
36+
export const DEFAULT_SQLITE_OPTIONS: Required<CapacitorSQLiteOptions> = {
37+
journalSizeLimit: 6 * 1024 * 1024,
38+
synchronous: SqliteSynchronous.normal,
39+
cacheSizeKb: 50 * 1024
40+
};
41+
442
export class CapacitorSQLiteOpenFactory implements SQLOpenFactory {
5-
constructor(protected options: SQLOpenOptions) {}
43+
constructor(protected options: CapacitorSQLiteOpenFactoryOptions) {}
644

745
openDB(): DBAdapter {
846
return new CapacitorSQLiteAdapter(this.options);

0 commit comments

Comments
 (0)