I'm getting more interested in SQLite as a database, keeping everything in memory to the process itself. When do we actually need to have a multi-process solution for a website? How much can we really do within a single process?
Both of these use wasm to ship the actually sqlite binary code and run in in browser!
We are going to use
npm to download the packages only. We need to get
sql-wasm.wasm and simply serve them up in the browser.
Test CLI Script
Lets look at how to create and populate a simple database on the command line.
Which we can test with:
Not wildly exciting data but there there you go.
- Load up the
sql-wasm.jsfile, which provides
locateFileis used to figure out where
- Fetch our database that we genereated previously, called
- Create a new database using the
Uint8Arrayof the file loaded.
- Do some awesome HTML DOM manipulation to create the table.
- Run the query, and populate the table.
In the directory with the html, wasm, and sqlite files, run:
And you should get:
Lets look at using a different method, using deno instead of node.
asdf, but you can follow the official instructions.
And then check to see what you have installed:
deno 1.8.3 (release, x86_64-unknown-linux-gnu) v8 9.0.257.3 typescript 4.2.2
I need this to make my emacs integration work, but it could be optional for you.
deps.ts to centralize your version dependancies:
Test cli script
Lets create a
test.ts script which creates a database and popualtes it:
And then we can run this with:
Test web script webworkers
Here we are going to write a
web.tsx file that will function as a
WebWorker. Our database will run in a different thread and the main
Then we need to bundle it up:
This creates a
web.js file that contains our webworker as well as the
sqlite binary in wasm.
This code is simpler that the previous code. We load up
web.js as a
modulepreload, and then inside of our script tag we create the new webwork, and send it a message. Once it gets the message it creates the in-memory database, then
But that's not the same!
I don't know how to load in the database file with the deno bundle based solution, so at this moment while it works I don't know how to solve that issue. Still the approach is cleaner.
Right now it looks like
sql.js will do what I want. I'm surprised at
how fast web assembly is.