Closed carper7 closed 3 years ago
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?
Hi Andrea,
Thank you for getting back to me this soon. It helps but still I have not been able to make it work. Let's say I want to use it as a backup; how do I do that? And let's say I want to retrieve that backed-up file and continue using it; how do I do it?
I tried this and still don't see it doing that.
import {init} from '//unpkg.com/sqlite-worker';
init({name: 'my-db'}).then(async ({all, get, query}) => {
// ... same code as before ...
const {total} = await getSELECT COUNT(id) as total FROM todos
;
//await queryDELETE FROM todos WHERE id=${5}
;
if (total < 1) {
console.log('Inserting some value');
//await queryINSERT INTO todos (id,value) VALUES (${4},${'d'})
;
}
console.log(await allSELECT * FROM todos
);
init({name: 'my-db', update(uInt8Array) {
// store the latest uInt8Array somewhere
console.log("update/export");
}});
});
Note: Let me see if I clearly understand this. The way I see it is that the buffer created in indexeddb is the back-up file which I will use either to synchronize to the server or to continue working on performing all the crud operations. So what is the update option for? I don't see it does anything at all or at least it is not entering on that part of the code and printing the message in the console. Please see the code above.
Thank you.
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.
Hi There,
Sorry to bug you too much. This is not an issue but I cannot seem to get my head wrapped around sqlite-worker. I am trying to create a db and then store it in indexeddb; that part is ok but what I don't understand is what if I want to use this db everywhere in my js files. E.g. I want to perform an insert or delete a record; how do I do that; it is not possible to use these:
import {init, SQLiteWorker} from '//unpkg.com/sqlite-worker'; import {init, SQLiteWorker} from './thirdparty/sqlite-worker/dist/index.js';
because imports are allowed from script tags with module type; so, I don't know how to use these methods once initialized. I also saw somewhere in your github you use importScripts but this does not work for me. I am sorry again to bother you it is just that I don't really know how to decouple all the components. I would like to be able to once I import all the required libs to just go and calling its methods from anywhere so I could perform all crud operations without a problem.
This is it. I want to thank you for any help you could give and have a wonderful weekend.
Regards,
C.Peretz.
From: Andrea Giammarchi notifications@github.com Sent: Thursday, February 18, 2021 6:04 AM To: WebReflection/sqlite-worker sqlite-worker@noreply.github.com Cc: carper7 charlip1@hotmail.com; Author author@noreply.github.com Subject: Re: [WebReflection/sqlite-worker] Testing Plugin (#6)
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.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/WebReflection/sqlite-worker/issues/6#issuecomment-781080425, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AELQMV6UI3XIN55FC6KM3OLS7SUWFANCNFSM4XZCOWJA.
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.
I know I am doing something wrong. The problem is I don't know exactly what; when I do it the same way you have it in your examples that works but when I try to do it the way I want it doesn't.
This works from html file within the script tags:
Now, this is what I am trying to do and it does not work:
HTML file:
JS file:
init({name: 'my-db', update(uInt8Array) {//enters here only when some sort of update to the db occurs
// store the latest uInt8Array somewhere
console.log('last db length', uInt8Array.length);
}})
.then(async ({all, get, query}) => {
// ... same code as before ...
await queryCREATE TABLE IF NOT EXISTS todos (id INTEGER PRIMARY KEY, value TEXT)
;
const {total} = await getSELECT COUNT(id) as total FROM todos
;
await queryDELETE FROM todos WHERE id=${4}
;
if (total < 1) {
console.log('Inserting some value');
await queryINSERT INTO todos (value) VALUES (${'a'})
;
await queryINSERT INTO todos (value) VALUES (${'b'})
;
await queryINSERT INTO todos (value) VALUES (${'c'})
;
}
await query`INSERT INTO todos (id,value) VALUES (${4},${'d'})`;
console.log(await all`SELECT * FROM todos`);
await query`CREATE TABLE IF NOT EXISTS todos1 (id INTEGER PRIMARY KEY, value TEXT)`;
console.log('Inserting some value');
await query`INSERT INTO todos1 (value) VALUES (${'a1'})`;
await query`INSERT INTO todos1 (value) VALUES (${'b1'})`;
await query`INSERT INTO todos1 (value) VALUES (${'c1'})`;
console.log(await all`SELECT * FROM todos1`);
console.log(await all`SELECT * FROM todos1 WHERE id=2`);
});
Here is the error when using Api from JS file:
Uncaught TypeError: Cannot read property 'then' of undefined
at Object.onDeviceReady (formList.js:871)
at Object.bindEvents (formList.js:41)
at Object.initialize (formList.js:30)
at HTMLDocument.<anonymous> (formList.js:2610)
at j (jquery-1.11.2.min.js:2)
at Object.fireWith [as resolveWith] (jquery-1.11.2.min.js:2)
at Function.ready (jquery-1.11.2.min.js:2)
at HTMLDocument.J (jquery-1.11.2.min.js:2)
From: Andrea Giammarchi notifications@github.com Sent: Saturday, February 20, 2021 8:22 AM To: WebReflection/sqlite-worker sqlite-worker@noreply.github.com Cc: carper7 charlip1@hotmail.com; Author author@noreply.github.com Subject: Re: [WebReflection/sqlite-worker] Testing Plugin (#6)
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.
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/WebReflection/sqlite-worker/issues/6#issuecomment-782585345, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AELQMVZYN4REC7HY24DFUF3S75WNBANCNFSM4XZCOWJA.
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.
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.