WebReflection/sqlite-worker

Testing Plugin

carper7 opened this issue · 8 comments

Hello,

I am testing your plugin and find this to be an awesome idea. I have a question in regards of the update option; what buffer do I use? The one created in indexeddb and how? Or one created somewhere else in memory/server? Could you please elaborate a bit more on this concept? Probably with an example of its use.

import {init} from '//unpkg.com/sqlite-worker';
init({name: 'my-db', update(uInt8Array) {
// store the latest uInt8Array somewhere
}});

My understanding is when the above code is run, it will grab same buffer used so I can continue doing crud operations on it. Am I right? I tried just like that but it does not seem to be doing anything at all.

Thank You.

the uInt8Array argument the update callback receives, is the entirety of the database as binary array.

that is the exact same binary array that would get stored into IndexedDB, hence a binary buffer usable by sqlite via emscripten directly, either on the server, or somewhere else.

the reason this possibility exists, is to be able to debounce, or synchronize on demand, the latest status of the database, in a remote server, or as a backup option.

does this help?

The example is clear though, and the readme too, you pass update as property of the direct init option:

init({name: 'my-db', update(uInt8Array) {
  // uInt8Array is *already* stored for you in the IndexedDB
  // but if you'd like to store it somewhere else you can
  console.log('last db length', uInt8Array.length);
}})
.then(async ({all, get, query}) => {
        // ... same code as before ...
       const {total} = await get`SELECT COUNT(id) as total FROM todos`;
      //await query`DELETE FROM todos WHERE id=${5}`;
      if (total < 1) {
            console.log('Inserting some value');
            //await query`INSERT INTO todos (id,value) VALUES (${4},${'d'})`;
      }
     console.log(await all`SELECT * FROM todos`);
  });

That array could be passed to the user as fallback in case there is no data anymore, as explained in the readme.

The "dance" is that if the user is known, but the user has no data, you can pass along a database file previously generated, so that previous data can be used... any better?

Also closing as this is not a bug per se.

P.S. the update is never called if no writing operations happen ... INSERT, DELETE, or UPDATE, are those that trigger updates and store in the IndexedDB, if you don't change anything, nothing happens.

I am trying to create a db and then store it in indexeddb

You don't need to do that, this library does that already.

what if I want to use this db everywhere in my js files.

all files need to pass through the DB handler ... you need to think ab out this like a remote DB, you have some API calling it and it's always pointing at 1 db.

You init the db more than once, you are already doing something wrong.

The API to query the DB is exposed once the DB is initialized:
https://github.com/WebReflection/sqlite-worker#importing-on-web-pages-via-esm

From that time on you can bootstrap any other script and pass along those utilities to query or provide a different API around those.

look, I've no idea what you are doing, the env is not clear, there's no live demo, the JS file means nothing, and I read jQuery within the stack of errors.

I don't now how you expect me to help ... but I won't reply anymore unless there is something concrete and reproducible I can test.