Only `query` tag trigger `update` callback timer
Thanks for the lib, it works like charm 😄
Btw, I read the code and see only the query tag from init.js (https://github.com/WebReflection/sqlite-worker/blob/main/esm/init.js#L96) triggers the update callback timer.
I tried the transaction tag and it doesn't work
// no ?module needed, it's the main export in unpkg
import { init } from "//unpkg.com/sqlite-worker";
// `dist` option resolved automatically via import.meta.url
init({
name: "my-db1",
update(uInt8Array) {
console.log("updated", uInt8Array);
},
}).then(async ({ all, get, query, raw, transaction }) => {
const table = raw`todos`;
await query`CREATE TABLE IF NOT EXISTS ${table} (id INTEGER PRIMARY KEY, value TEXT)`;
const { total } = await get`SELECT COUNT(id) as total FROM ${table}`;
if (total < 10) {
console.log("Inserting some value");
await query`INSERT INTO ${table} (value) VALUES (${"a"})`;
await query`INSERT INTO ${table} (value) VALUES (${"b"})`;
await query`INSERT INTO ${table} (value) VALUES (${"c"})`;
}
const insert = transaction();
for (let i = 0; i < 10; i++)
insert`INSERT INTO todos (value) VALUES (${"Ipsum " + i})`;
await insert.commit();
console.log(await all`SELECT * FROM ${table}`);
});
Can we export a defer update function, save function to manual trigger save ?
Thanks
I wonder if exporting explicitly that save instead would be a better option ... and btw, these days I suggest https://github.com/WebReflection/sql.js instead, where you can add sqlite-tag or any other extra logic you need. It works in both main and workers too.
Oh I see, but this repo like a heavy lifting about "worker messaging", it's like a shortest way to kick start and use sqlite wasm in worker. So I think this repo is still good as its purpose.
Yah, exporting save is good enough option :) Until that, I think have to use the trick
await query`SELECT 'update' as ok`;
Thanks
await query`SELECT 'update' as 1`;
probably, but that's why I think the current "magic handler on changes" is too magic ... having a whole scan/parser for the syntax feels overkill, not having it might always allow hacks or fail in some occasion ... explicit is better than implicit as users writing queries surely know when they want these to be updated, me delaying an interval after guessing some action feels ... meh ...
Before searching your repo, I think I will use sql.js export with a timer for checking checksum the Uint8Array then saving to storage. Even checksum crc32 is good for detecting changes. Just an idea :)
thing is ... timers get easily discarded when you unload a page while IndexedDB operations that have been already triggered might get some time to complete their operation before the browser fully discard that page ... so timers might not a robust solution, but then again I am not sure there are guarantees with IDB transactions neither, but I expect that to be the case.