Skip to content
Merged
5 changes: 5 additions & 0 deletions .changeset/hip-poems-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/op-sqlite': patch
---

Allow users to load additional sqlite extensions
15 changes: 12 additions & 3 deletions packages/powersync-op-sqlite/src/db/OPSqliteAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
const dbFilename = filenameOverride ?? this.options.name;
const DB: DB = this.openDatabase(dbFilename, this.options.sqliteOptions.encryptionKey);

//Load extension for all connections
this.loadExtension(DB);
//Load extensions for all connections
this.loadAdditionalExtensions(DB);
this.loadPowerSyncExtension(DB);

await DB.execute('SELECT powersync_init()');

Expand Down Expand Up @@ -124,7 +125,15 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
}
}

private loadExtension(DB: DB) {
private loadAdditionalExtensions(DB: DB) {
if (this.options.sqliteOptions.extensions.length > 0) {
for (const extension of this.options.sqliteOptions.extensions) {
DB.loadExtension(extension.path, extension.entryPoint);
}
}
}

private loadPowerSyncExtension(DB: DB) {
if (Platform.OS === 'ios') {
const bundlePath: string = NativeModules.PowerSyncOpSqlite.getBundlePath();
const libPath = `${bundlePath}/Frameworks/powersync-sqlite-core.framework/powersync-sqlite-core`;
Expand Down
12 changes: 11 additions & 1 deletion packages/powersync-op-sqlite/src/db/SqliteOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export interface SqliteOptions {
* If set, the database will be encrypted using SQLCipher.
*/
encryptionKey?: string;

/**
* Load extensions using the path and entryPoint.
* More info can be found here https://op-engineering.github.io/op-sqlite/docs/api#loading-extensions.
*/
extensions?: Array<{
path: string;
entryPoint?: string;
}>;
}

// SQLite journal mode. Set on the primary connection.
Expand Down Expand Up @@ -57,5 +66,6 @@ export const DEFAULT_SQLITE_OPTIONS: Required<SqliteOptions> = {
synchronous: SqliteSynchronous.normal,
journalSizeLimit: 6 * 1024 * 1024,
lockTimeoutMs: 30000,
encryptionKey: null
encryptionKey: null,
extensions: []
};
Loading