Skip to content

Commit

Permalink
- Add README instructions
Browse files Browse the repository at this point in the history
- Pass encryptionKey to adapter and open factory
- Cleanup
  • Loading branch information
mugikhan committed Dec 12, 2024
1 parent a12bdeb commit 7ee4850
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
48 changes: 48 additions & 0 deletions packages/web/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,54 @@ Install it in your app with:
npm install @journeyapps/wa-sqlite
```

### Encryption with Multiple Ciphers

To enable encryption you need to specify an encryption key when instantiating the PowerSync database.

> The PowerSync Web SDK uses the ChaCha20 cipher algorithm by [default](https://utelle.github.io/SQLite3MultipleCiphers/docs/ciphers/cipher_chacha20/).
```typescript
export const db = new PowerSyncDatabase({
// The schema you defined
schema: AppSchema,
database: {
// Filename for the SQLite database — it's important to only instantiate one instance per file.
dbFilename: 'powersync.db'
// Optional. Directory where the database file is located.'
// dbLocation: 'path/to/directory'
},
// Encryption key for the database.
encryptionKey: 'your-encryption-key'
});

// If you are using a custom WASQLiteOpenFactory, you need specify the encryption key inside the factory construtor
export const db = new PowerSyncDatabase({
schema: AppSchema,
database: new WASQLiteOpenFactory({
dbFilename: 'examplsw1se112.db',
vfs: WASQLiteVFS.OPFSCoopSyncVFS,
// Encryption key for the database.
encryptionKey: 'your-encryption-key'
flags: {
enableMultiTabs: typeof SharedWorker !== 'undefined'
}
})
});
// If you are using a custom WASQLiteDBAdapter, you need specify the encryption key inside the factory construtor
export const db = new PowerSyncDatabase({
schema: AppSchema,
database: new WASQLiteDBAdapter({
dbFilename: 'examplsw1se112.db',
vfs: WASQLiteVFS.OPFSCoopSyncVFS,
// Encryption key for the database.
encryptionKey: 'your-encryption-key'
flags: {
enableMultiTabs: typeof SharedWorker !== 'undefined'
}
})
});
```

## Webpack

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export class WASqliteConnection
}

protected async executeEncryptionPragma(): Promise<void> {
if (this.options.encryptionKey && this._dbP) {
if (this.options.encryptionKey) {
await this.executeSingleStatement(`PRAGMA key = "${this.options.encryptionKey}"`);
}
return;
Expand Down
4 changes: 3 additions & 1 deletion packages/web/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export class WASQLiteDBAdapter extends LockedAsyncDatabaseAdapter {
baseConnection: await remote({
...options,
temporaryStorage: temporaryStorage ?? TemporaryStorageOption.MEMORY,
flags: resolveWebPowerSyncFlags(options.flags)
flags: resolveWebPowerSyncFlags(options.flags),
encryptionKey: options.encryptionKey
})
});
}
Expand All @@ -64,6 +65,7 @@ export class WASQLiteDBAdapter extends LockedAsyncDatabaseAdapter {
temporaryStorage,
logger: options.logger,
vfs: options.vfs,
encryptionKey: options.encryptionKey,
worker: options.worker
});
return openFactory.openConnection();
Expand Down
5 changes: 2 additions & 3 deletions packages/web/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import { WASqliteConnection, WASQLiteVFS } from './WASQLiteConnection';

export interface WASQLiteOpenFactoryOptions extends WebSQLOpenFactoryOptions {
vfs?: WASQLiteVFS;
encryptionKey?: string;
}

export interface ResolvedWASQLiteOpenFactoryOptions extends ResolvedWebSQLOpenOptions {
vfs: WASQLiteVFS;
encryptionKey?: string;
}
/**
* Opens a SQLite connection using WA-SQLite.
Expand Down Expand Up @@ -60,7 +58,8 @@ export class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory {
optionsDbWorker({
...this.options,
temporaryStorage,
flags: this.resolvedFlags
flags: this.resolvedFlags,
encryptionKey
})
)
: openWorkerDatabasePort(this.options.dbFilename, enableMultiTabs, optionsDbWorker, this.waOptions.vfs);
Expand Down
12 changes: 12 additions & 0 deletions packages/web/src/db/adapters/web-sql-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ export interface ResolvedWebSQLOpenOptions extends SQLOpenOptions {
* Setting this to `FILESYSTEM` can cause issues with larger queries or datasets.
*/
temporaryStorage: TemporaryStorageOption;

/**
* Encryption key for the database.
* If set, the database will be encrypted using ChaCha20.
*/
encryptionKey?: string;
}

export enum TemporaryStorageOption {
Expand Down Expand Up @@ -73,6 +79,12 @@ export interface WebSQLOpenFactoryOptions extends SQLOpenOptions {
* Setting this to `FILESYSTEM` can cause issues with larger queries or datasets.
*/
temporaryStorage?: TemporaryStorageOption;

/**
* Encryption key for the database.
* If set, the database will be encrypted using ChaCha20.
*/
encryptionKey?: string;
}

export function isServerSide() {
Expand Down

0 comments on commit 7ee4850

Please sign in to comment.