Skip to content

Commit 7ee4850

Browse files
committed
- Add README instructions
- Pass encryptionKey to adapter and open factory - Cleanup
1 parent a12bdeb commit 7ee4850

File tree

5 files changed

+66
-5
lines changed

5 files changed

+66
-5
lines changed

packages/web/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,54 @@ Install it in your app with:
2828
npm install @journeyapps/wa-sqlite
2929
```
3030

31+
### Encryption with Multiple Ciphers
32+
33+
To enable encryption you need to specify an encryption key when instantiating the PowerSync database.
34+
35+
> The PowerSync Web SDK uses the ChaCha20 cipher algorithm by [default](https://utelle.github.io/SQLite3MultipleCiphers/docs/ciphers/cipher_chacha20/).
36+
37+
```typescript
38+
export const db = new PowerSyncDatabase({
39+
// The schema you defined
40+
schema: AppSchema,
41+
database: {
42+
// Filename for the SQLite database — it's important to only instantiate one instance per file.
43+
dbFilename: 'powersync.db'
44+
// Optional. Directory where the database file is located.'
45+
// dbLocation: 'path/to/directory'
46+
},
47+
// Encryption key for the database.
48+
encryptionKey: 'your-encryption-key'
49+
});
50+
51+
// If you are using a custom WASQLiteOpenFactory, you need specify the encryption key inside the factory construtor
52+
export const db = new PowerSyncDatabase({
53+
schema: AppSchema,
54+
database: new WASQLiteOpenFactory({
55+
dbFilename: 'examplsw1se112.db',
56+
vfs: WASQLiteVFS.OPFSCoopSyncVFS,
57+
// Encryption key for the database.
58+
encryptionKey: 'your-encryption-key'
59+
flags: {
60+
enableMultiTabs: typeof SharedWorker !== 'undefined'
61+
}
62+
})
63+
});
64+
// If you are using a custom WASQLiteDBAdapter, you need specify the encryption key inside the factory construtor
65+
export const db = new PowerSyncDatabase({
66+
schema: AppSchema,
67+
database: new WASQLiteDBAdapter({
68+
dbFilename: 'examplsw1se112.db',
69+
vfs: WASQLiteVFS.OPFSCoopSyncVFS,
70+
// Encryption key for the database.
71+
encryptionKey: 'your-encryption-key'
72+
flags: {
73+
enableMultiTabs: typeof SharedWorker !== 'undefined'
74+
}
75+
})
76+
});
77+
```
78+
3179
## Webpack
3280

3381
See the [example Webpack config](https://github.com/powersync-ja/powersync-js/blob/main/demos/example-webpack/webpack.config.js) for details on polyfills and requirements.

packages/web/src/db/adapters/wa-sqlite/WASQLiteConnection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export class WASqliteConnection
178178
}
179179

180180
protected async executeEncryptionPragma(): Promise<void> {
181-
if (this.options.encryptionKey && this._dbP) {
181+
if (this.options.encryptionKey) {
182182
await this.executeSingleStatement(`PRAGMA key = "${this.options.encryptionKey}"`);
183183
}
184184
return;

packages/web/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ export class WASQLiteDBAdapter extends LockedAsyncDatabaseAdapter {
5252
baseConnection: await remote({
5353
...options,
5454
temporaryStorage: temporaryStorage ?? TemporaryStorageOption.MEMORY,
55-
flags: resolveWebPowerSyncFlags(options.flags)
55+
flags: resolveWebPowerSyncFlags(options.flags),
56+
encryptionKey: options.encryptionKey
5657
})
5758
});
5859
}
@@ -64,6 +65,7 @@ export class WASQLiteDBAdapter extends LockedAsyncDatabaseAdapter {
6465
temporaryStorage,
6566
logger: options.logger,
6667
vfs: options.vfs,
68+
encryptionKey: options.encryptionKey,
6769
worker: options.worker
6870
});
6971
return openFactory.openConnection();

packages/web/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ import { WASqliteConnection, WASQLiteVFS } from './WASQLiteConnection';
1010

1111
export interface WASQLiteOpenFactoryOptions extends WebSQLOpenFactoryOptions {
1212
vfs?: WASQLiteVFS;
13-
encryptionKey?: string;
1413
}
1514

1615
export interface ResolvedWASQLiteOpenFactoryOptions extends ResolvedWebSQLOpenOptions {
1716
vfs: WASQLiteVFS;
18-
encryptionKey?: string;
1917
}
2018
/**
2119
* Opens a SQLite connection using WA-SQLite.
@@ -60,7 +58,8 @@ export class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory {
6058
optionsDbWorker({
6159
...this.options,
6260
temporaryStorage,
63-
flags: this.resolvedFlags
61+
flags: this.resolvedFlags,
62+
encryptionKey
6463
})
6564
)
6665
: openWorkerDatabasePort(this.options.dbFilename, enableMultiTabs, optionsDbWorker, this.waOptions.vfs);

packages/web/src/db/adapters/web-sql-flags.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ export interface ResolvedWebSQLOpenOptions extends SQLOpenOptions {
4646
* Setting this to `FILESYSTEM` can cause issues with larger queries or datasets.
4747
*/
4848
temporaryStorage: TemporaryStorageOption;
49+
50+
/**
51+
* Encryption key for the database.
52+
* If set, the database will be encrypted using ChaCha20.
53+
*/
54+
encryptionKey?: string;
4955
}
5056

5157
export enum TemporaryStorageOption {
@@ -73,6 +79,12 @@ export interface WebSQLOpenFactoryOptions extends SQLOpenOptions {
7379
* Setting this to `FILESYSTEM` can cause issues with larger queries or datasets.
7480
*/
7581
temporaryStorage?: TemporaryStorageOption;
82+
83+
/**
84+
* Encryption key for the database.
85+
* If set, the database will be encrypted using ChaCha20.
86+
*/
87+
encryptionKey?: string;
7688
}
7789

7890
export function isServerSide() {

0 commit comments

Comments
 (0)