You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
SQLite Shared-Cache Mode supports sharing in memory databases between threads in the same process. This is potentially interesting for sharing data between Workers. However It doesn't currently work with this WebAssembly sqlite for a number of reasons:
Shared WebAssembly.Memory needs to be setup while constructing the WebAssembly.Instance.
With the Deno sqlite3 FFI module it is possible to use shared cache as follows:
// sqlite3.ts// wait for the worker to run first.constworker: Worker=awaitnewPromise((resolve,reject)=>{constworker=newWorker(newURL("./sqlite3.worker.ts",import.meta.url),{type: "module",// @ts-ignoredeno: {namespace: true},});worker.addEventListener("error",reject);worker.addEventListener("message",(msg)=>{worker.removeEventListener("error",reject);msg.data==="ready" ? resolve(worker) : reject(msg);},{once: true});});console.log("main");import*asconstantsfrom"https://deno.land/x/[email protected]/src/constants.ts";import{Database}from"https://deno.land/x/[email protected]/mod.ts";constdb=newDatabase("file:memdb1?mode=memory&cache=shared",{flags:
constants.SQLITE3_OPEN_URI|constants.SQLITE3_OPEN_CREATE|constants.SQLITE3_OPEN_READWRITE,});db.execute(`INSERT INTO kv (key, value) VALUES ('main', 'hello')`);constresult=db.queryArray(`select * from kv`);console.log("main",{ result });worker.terminate();
// sqlite3.worker.ts/// <reference lib="deno.worker" />console.log("worker");import*asconstantsfrom"https://deno.land/x/[email protected]/src/constants.ts";import{Database}from"https://deno.land/x/[email protected]/mod.ts";constdb=newDatabase("file:memdb1?mode=memory&cache=shared",{flags:
constants.SQLITE3_OPEN_URI|constants.SQLITE3_OPEN_CREATE|constants.SQLITE3_OPEN_READWRITE,});console.log("sqlite_version",db.queryArray(`select sqlite_version()`));db.execute(`CREATE TABLE kv (key TEXT PRIMARY KEY, value TEXT)`);db.execute(`INSERT INTO kv (key, value) VALUES ('worker', 'hello')`);constresult=db.queryArray(`select * from kv`);console.log("worker",{ result });self.postMessage("ready");
I think this does not fit well with the architecture of the project. The database instances are contained within their own WASM instances, since this allows them to be automatically collected by the garbage collector when DB objects go out of scope.
This is really nice for JS users, since you can rely on the garbage collector for in-memory databases, but won't mix well with having shared memory cache. (This is also a more secure setup, if you e.g. want to share multiple users databases in the same process, since the WASM binaries can't interact with each other.)
SQLite Shared-Cache Mode supports sharing in memory databases between threads in the same process. This is potentially interesting for sharing data between Workers. However It doesn't currently work with this WebAssembly sqlite for a number of reasons:
Shared WebAssembly.Memory needs to be setup while constructing the WebAssembly.Instance.
While the built wasm exports its memory, it does not appear to import it when supplied on the importObject as per: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory#creating_a_new_memory_object
WASI does not seem to support Worker-style threading currently.
With the Deno sqlite3 FFI module it is possible to use shared cache as follows:
The text was updated successfully, but these errors were encountered: