Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

web: fix async sqlite

Abdullah Attab7f1f36090c4
diff --git a/apps/web/src/common/sqlite/sqlite.worker.ts b/apps/web/src/common/sqlite/sqlite.worker.ts
index 7f362f65c..5ca3fd7e5 100644
--- a/apps/web/src/common/sqlite/sqlite.worker.ts
+++ b/apps/web/src/common/sqlite/sqlite.worker.ts
@@ -118,7 +118,7 @@ class _SQLiteWorker {
} else this.retryCounter[sql] = 0;
if (ex instanceof Error || ex instanceof SQLiteError)
- ex.message += ` (query: ${sql})`;
+ ex.message += ` (error preparing query: ${sql})`;
throw ex;
}
}
@@ -146,7 +146,7 @@ class _SQLiteWorker {
return rows;
} catch (e) {
if (e instanceof Error || e instanceof SQLiteError)
- e.message += ` (query: ${sql})`;
+ e.message += ` (error exec query: ${sql})`;
throw e;
} finally {
await this.sqlite
@@ -201,16 +201,16 @@ class _SQLiteWorker {
this.initialized = false;
}
- async export() {
- const vfs = await this.getVFS(this.name, this.async);
- const stream = new ReadableStream(new DatabaseSource(vfs, this.name));
+ async export(name: string, options: SQLiteOptions) {
+ const vfs = await this.getVFS(name, options.async);
+ const stream = new ReadableStream(new DatabaseSource(vfs, name));
return transfer(stream, [stream]);
}
- async delete() {
+ async delete(name: string, options: SQLiteOptions) {
await this.close();
if (this.vfs) await this.vfs.delete();
- else await (await this.getVFS(this.name, this.async)).delete();
+ else await (await this.getVFS(name, options.async)).delete();
}
async getVFS(dbName: string, async: boolean) {
diff --git a/apps/web/src/common/sqlite/wa-sqlite-kysely-driver.ts b/apps/web/src/common/sqlite/wa-sqlite-kysely-driver.ts
index 2f7e731c5..8b640471b 100644
--- a/apps/web/src/common/sqlite/wa-sqlite-kysely-driver.ts
+++ b/apps/web/src/common/sqlite/wa-sqlite-kysely-driver.ts
@@ -76,7 +76,10 @@ export class WaSqliteWorkerMultipleTabDriver implements Driver {
activated: true,
closed: false
});
- this.connection = new WaSqliteWorkerConnection(service.proxy);
+ this.connection = new WaSqliteWorkerConnection(
+ service.proxy,
+ this.config.async
+ );
}
return;
}
@@ -122,7 +125,10 @@ export class WaSqliteWorkerMultipleTabDriver implements Driver {
await service.getProviderPort();
console.timeEnd("waiting for provider port");
- this.connection = new WaSqliteWorkerConnection(service.proxy);
+ this.connection = new WaSqliteWorkerConnection(
+ service.proxy,
+ this.config.async
+ );
servicePool.set(this.serviceName, {
service,
@@ -202,12 +208,22 @@ export class WaSqliteWorkerMultipleTabDriver implements Driver {
async delete() {
const service = servicePool.get(this.serviceName);
if (!service || !service.service) return;
- await service.service?.proxy?.delete();
+ await service.service?.proxy?.delete(this.config.dbName, {
+ async: this.config.async,
+ encrypted: this.config.encrypted,
+ url: this.config.async ? SQLiteAsyncURI : SQLiteSyncURI
+ });
service.closed = true;
}
async export() {
- return servicePool.get(this.serviceName)?.service?.proxy?.export();
+ return servicePool
+ .get(this.serviceName)
+ ?.service?.proxy?.export(this.config.dbName, {
+ async: this.config.async,
+ encrypted: this.config.encrypted,
+ url: this.config.async ? SQLiteAsyncURI : SQLiteSyncURI
+ });
}
}
@@ -228,7 +244,10 @@ export class WaSqliteWorkerSingleTabDriver implements Driver {
encrypted: this.config.encrypted,
url: this.config.async ? SQLiteAsyncURI : SQLiteSyncURI
});
- this.connection = new WaSqliteWorkerConnection(this.worker);
+ this.connection = new WaSqliteWorkerConnection(
+ this.worker,
+ this.config.async
+ );
}
async acquireConnection(): Promise<DatabaseConnection> {
@@ -262,16 +281,28 @@ export class WaSqliteWorkerSingleTabDriver implements Driver {
}
async delete() {
- await this.worker.delete();
+ await this.worker.delete(this.config.dbName, {
+ async: this.config.async,
+ encrypted: this.config.encrypted,
+ url: this.config.async ? SQLiteAsyncURI : SQLiteSyncURI
+ });
}
async export() {
- return await this.worker.export();
+ return await this.worker.export(this.config.dbName, {
+ async: this.config.async,
+ encrypted: this.config.encrypted,
+ url: this.config.async ? SQLiteAsyncURI : SQLiteSyncURI
+ });
}
}
class WaSqliteWorkerConnection implements DatabaseConnection {
- constructor(private readonly worker: SQLiteWorker | Remote<SQLiteWorker>) {}
+ #queryMutex = new Mutex();
+ constructor(
+ private readonly worker: SQLiteWorker | Remote<SQLiteWorker>,
+ private readonly sequential = false
+ ) {}
streamQuery<R>(): AsyncIterableIterator<QueryResult<R>> {
throw new Error("wasqlite driver doesn't support streaming");
@@ -279,6 +310,17 @@ class WaSqliteWorkerConnection implements DatabaseConnection {
async executeQuery<R>(
compiledQuery: CompiledQuery<unknown>
+ ): Promise<QueryResult<R>> {
+ if (this.sequential) {
+ return this.#queryMutex.runExclusive(async () =>
+ this.#_executeQuery(compiledQuery)
+ );
+ }
+ return this.#_executeQuery(compiledQuery);
+ }
+
+ #_executeQuery<R>(
+ compiledQuery: CompiledQuery<unknown>
): Promise<QueryResult<R>> {
const { parameters, sql, query } = compiledQuery;
const mode =