Skip to content

Commit 074f4cd

Browse files
authored
fix(adapter-sql-js): Only calling db.export() if a persister is specified. (#679)
1 parent a95ccbb commit 074f4cd

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

.changeset/short-hotels-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/adapter-sql-js': patch
3+
---
4+
5+
Only calling `db.export()` if a persister is specified, otherwise it is no-op. Improves in-memory performance.

packages/adapter-sql-js/src/SQLJSAdapter.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface SQLJSOpenOptions extends SQLOpenOptions {
2929
}
3030

3131
export interface ResolvedSQLJSOpenOptions extends SQLJSOpenOptions {
32-
persister: SQLJSPersister;
32+
persister?: SQLJSPersister;
3333
logger: ILogger;
3434
}
3535

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

9898
this.writeScheduler = new ControlledExecutor(async (db: SQLJs.Database) => {
99+
if (!this.options.persister) {
100+
return;
101+
}
102+
99103
await this.options.persister.writeFile(db.export());
100104
});
101105
}
102106

103107
protected resolveOptions(options: SQLJSOpenOptions): ResolvedSQLJSOpenOptions {
104-
const persister = options.persister ?? {
105-
readFile: async () => null,
106-
writeFile: async () => {}
107-
};
108-
109108
const logger = options.logger ?? createLogger('SQLJSDBAdapter');
110109

111110
return {
112111
...options,
113-
persister,
114112
logger
115113
};
116114
}
@@ -125,7 +123,7 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
125123
this.options.logger.error('[stderr]', text);
126124
}
127125
});
128-
const existing = await this.options.persister.readFile();
126+
const existing = await this.options.persister?.readFile();
129127
const db = new SQL.Database(existing);
130128
this.dbP = db['db'];
131129
this._db = db;
@@ -262,7 +260,10 @@ export class SQLJSDBAdapter extends BaseObserver<DBAdapterListener> implements D
262260
const db = await this.getDB();
263261
const result = await fn(this.generateLockContext());
264262

265-
this.writeScheduler.schedule(db);
263+
// No point to schedule a write if there's no persister.
264+
if (this.options.persister) {
265+
this.writeScheduler.schedule(db);
266+
}
266267

267268
const notification: BatchedUpdateNotification = {
268269
rawUpdates: [],

0 commit comments

Comments
 (0)