Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added temporaryStorage option to WASQLiteOpenFactory. #419

Merged
merged 5 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tasty-birds-lay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/web': minor
---

Added `temporaryStorage` option to `WebSQLOpenFactoryOptions`. The `temp_store` value will now defaults to "MEMORY".
2 changes: 1 addition & 1 deletion demos/vue-supabase-todolist/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"vite-plugin-top-level-await": "^1.4.1",
"vite-plugin-vuetify": "^2.0.3",
"vite-plugin-wasm": "^3.3.0",
"vue-tsc": "^2.0.6"
"vue-tsc": "2.0.6"
}
}
8 changes: 7 additions & 1 deletion packages/web/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Logger, { type ILogger } from 'js-logger';
import type { DBFunctionsInterface, OpenDB } from '../../../shared/types';
import { _openDB } from '../../../shared/open-db';
import { getWorkerDatabaseOpener, resolveWorkerDatabasePortFactory } from '../../../worker/db/open-worker-database';
import { ResolvedWebSQLOpenOptions, resolveWebSQLFlags, WebSQLFlags } from '../web-sql-flags';
import { ResolvedWebSQLOpenOptions, resolveWebSQLFlags, TemporaryStorageOption, WebSQLFlags } from '../web-sql-flags';
import { getNavigatorLocks } from '../../../shared/navigator';

/**
Expand All @@ -32,6 +32,8 @@ export interface WASQLiteDBAdapterOptions extends Omit<PowerSyncOpenFactoryOptio
workerPort?: MessagePort;

worker?: string | URL | ((options: ResolvedWebSQLOpenOptions) => Worker | SharedWorker);

temporaryStorage?: TemporaryStorageOption;
}

/**
Expand Down Expand Up @@ -86,6 +88,8 @@ export class WASQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
this.logger.warn('Multiple tabs are not enabled in this browser');
}

const tempStoreQuery = `PRAGMA temp_store = ${this.options.temporaryStorage ?? TemporaryStorageOption.MEMORY};`;

if (useWebWorker) {
const optionsDbWorker = this.options.worker;

Expand All @@ -103,6 +107,7 @@ export class WASQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
: getWorkerDatabaseOpener(this.options.dbFilename, enableMultiTabs, optionsDbWorker);

this.methods = await dbOpener(this.options.dbFilename);
await this.methods!.execute(tempStoreQuery);
this.methods.registerOnTableChange(
Comlink.proxy((event) => {
this.iterateListeners((cb) => cb.tablesUpdated?.(event));
Expand All @@ -112,6 +117,7 @@ export class WASQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
return;
}
this.methods = await _openDB(this.options.dbFilename, { useWebWorker: false });
await this.methods!.execute(tempStoreQuery);
this.methods.registerOnTableChange((event) => {
this.iterateListeners((cb) => cb.tablesUpdated?.(event));
});
Expand Down
11 changes: 11 additions & 0 deletions packages/web/src/db/adapters/web-sql-flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export interface ResolvedWebSQLOpenOptions extends SQLOpenOptions {
flags: ResolvedWebSQLFlags;
}

export enum TemporaryStorageOption {
MEMORY = 'memory',
FILESYSTEM = 'file'
}

/**
* Options for opening a Web SQL connection
*/
Expand All @@ -55,6 +60,12 @@ export interface WebSQLOpenFactoryOptions extends SQLOpenOptions {
* or a factory method that returns a worker.
*/
worker?: string | URL | ((options: ResolvedWebSQLOpenOptions) => Worker | SharedWorker);

/**
* Where to store SQLite temporary files. Defaults to 'MEMORY'.
* Setting this to `FILESYSTEM` can cause issues with larger queries or datasets.
*/
temporaryStorage?: TemporaryStorageOption;
}

export function isServerSide() {
Expand Down
Loading