Skip to content
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/short-hotels-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/adapter-sql-js': patch
---

Only calling `db.export()` if a persister is specified, otherwise it is no-op. Improves in-memory performance.
19 changes: 10 additions & 9 deletions packages/adapter-sql-js/src/SQLJSAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface SQLJSOpenOptions extends SQLOpenOptions {
}

export interface ResolvedSQLJSOpenOptions extends SQLJSOpenOptions {
persister: SQLJSPersister;
persister?: SQLJSPersister;
logger: ILogger;
}

Expand Down Expand Up @@ -96,21 +96,19 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
});

this.writeScheduler = new ControlledExecutor(async (db: SQLJs.Database) => {
if (!this.options.persister) {
return;
}

await this.options.persister.writeFile(db.export());
});
}

protected resolveOptions(options: SQLJSOpenOptions): ResolvedSQLJSOpenOptions {
const persister = options.persister ?? {
readFile: async () => null,
writeFile: async () => {}
};

const logger = options.logger ?? createLogger('SQLJSDBAdapter');

return {
...options,
persister,
logger
};
}
Expand All @@ -125,7 +123,7 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
this.options.logger.error('[stderr]', text);
}
});
const existing = await this.options.persister.readFile();
const existing = await this.options.persister?.readFile();
const db = new SQL.Database(existing);
this.dbP = db['db'];
this._db = db;
Expand Down Expand Up @@ -262,7 +260,10 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
const db = await this.getDB();
const result = await fn(this.generateLockContext());

this.writeScheduler.schedule(db);
// No point to schedule a write if there's no persister.
if (this.options.persister) {
this.writeScheduler.schedule(db);
}

const notification: BatchedUpdateNotification = {
rawUpdates: [],
Expand Down