Loading and saving to a sqlite.db file #82
Replies: 7 comments 26 replies
-
I guess I do have a few thoughts and suggestions... As you excavated from the other thread, I would instantiate a VFS and make calls to its methods defined in the VFS base class. It might be instructive to uncomment the browser debug console logging in the VFS that you use, e.g. here for IDBBatchAtomicVFS, and open your application or the demo to watch SQLite make calls because you'll need to mimic that to some degree. I think you can do what you need with only One critically important thing to know is the IndexedDB VFS classes store each write as a separate IndexedDB object, and the VFS assumes that 1 object = 1 page in a database file (except during a VACUUM which you won't encounter) and reads will never cross a page boundary, because that's what SQLite does (and there are advantages for that). That's a key constraint on how you perform I/O with the VFS. In addition, page size is not a known constant value (it can be changed with SQL) so you'll need to read the page size from the file header first. Also the API for VFS classes is changing so you'll almost certainly want to write to the new API. I would recommend that the interface to your code stream file data if possible (e.g. with the Streams API), to allow for the possibility of managing databases larger than available memory. I don't know how well this is supported yet, though - I do know you can't use fetch to upload a stream. You're welcome to develop this in your own repo. As for adding it to this project, it might make sense to have this in examples/ and a demo page in demos/. Integration with the main demo might be nice, too, but be aware it has been rewritten in the same branch with the new APIs. This would be the first substantive code submission, so you might have to bear with me while I figure out how I want to manage PRs, could be a little annoying for you as I make things up on the fly. 🤪 (but not entirely kidding) |
Beta Was this translation helpful? Give feedback.
-
A potential gotcha: some VFSes require that the lengths given to xWrite and xRead be a legal db page size (a power of 2 between 512 and 64k). The library internals only ever pass such sizes to those functions. Not all VFSes are picky about that, though. |
Beta Was this translation helpful? Give feedback.
-
To summarize and ensure I understand, the basic idea for importing a db file is to:
|
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
The above looks suspiciously like it works... I get the contents of the tables I'm expecting and a EDIT: Added the missing |
Beta Was this translation helpful? Give feedback.
-
@AntonOfTheWoods - did you end up writing an exporter to export the DB File from the IndexedDBBatchAtomic VFS? |
Beta Was this translation helpful? Give feedback.
-
I created a demo page, online here (source) to demonstrate database file import (local file to wa-sqlite) and export (wa-sqlite to local file). The demo uses direct calls to IDBBatchAtomicVFS, as suggested earlier in this thread. I think it should work with other VFS classes, but I have not tried any. I built this to investigate Issue #110 (now resolved), based on racy behavior mentioned earlier in this thread. The implementation streams data using ReadableStream in both import and export, which allows handling database files larger than available memory. Note that the only cross-browser way (that I know of) to stream an export to a local file is with a service worker, so that's what I did. I wrote my service worker as an ES6 module, so it would need to be bundled to work on Firefox (which doesn't yet support ES6 module service workers). |
Beta Was this translation helpful? Give feedback.
-
I would like to add a
save
andload
database functions to@vlcn.io/crsqlite-wasm
.I found that package depends on
wa-sqlite
, and I think the addition would be best suited for it.I found a previous discussion which describes:
I also found that
sql.js
supports loading a database via:Would you be open to adding
save
andload
functions to your package? Do you have suggestions on the best way for me to implement those?Beta Was this translation helpful? Give feedback.
All reactions