SQLite Forum

Just another SQLite-in-the-browser library
Login

Just another SQLite-in-the-browser library

(1) By Evan (evan-brass) on 2023-05-22 18:26:12 [source]

Hello, I thought I'd toss my hat in the ring for doing SQLite in the browser through WASM. Unlike the other options I've seen (wa-sqlite, sql.js, and the official WASM project), I used wasi to compile SQLite instead of emscripten. With the right sqlite_cfg.h and enough optimization none of the WASI imports end up in the finished .wasm file.

It supports in-memory and origin private file system databases, but also has a file-picker VFS so that you can edit SQLite databases on your local computer from a browser tab (no locking support). There's also an HTTP vfs so that you can access (read-only) SQLite databases on any static host that supports range queries (inspired by https://phiresky.github.io/blog/2021/hosting-sqlite-databases-on-github-pages/).

Anyway, it's not production ready (nor likely will it ever be), but I though it might contain some interesting patterns that other people could use in their endeavors.

NPM: https://www.npmjs.com/package/sql.mjs

Demo page: https://blog.evan-brass.net/sql.mjs/

The sqlite3.async.wasm file is only 1.5mb (530kb if you gzip it) and that includes R-trees, math functions, JSON, and FTS5 which doesn't seem too bad: https://github.com/evan-brass/sql.mjs/releases/tag/v0.10.0

(2.1) By Stephan Beal (stephan) on 2023-05-23 19:22:17 edited from 2.0 in reply to 1 [link] [source]

I used wasi to compile SQLite instead of emscripten

This is great, thank you for letting us know. You can compile the canonical source tree with the --wasi-sdk=DIR (Edit:) --with-wasi-sdk=DIR flag to build a "server-side" version using the WASI SDK. We don't currently do anything "official" with that but we have it on good authority that the VMWare folks use it.

Demo page: https://blog.evan-brass.net/sql.mjs/

For the near-term future i am limited to a phone for internet activity but will get this added to the "related works" section of our site once i'm properly back online. (Edit: done 2023-05-23.)

The sqlite3.async.wasm file is only 1.5mb (530kb if you gzip it) and that includes R-trees, math functions, JSON, and FTS5 which doesn't seem too bad:

You can probably get it smaller if you like, but that may well depend on compilation and linking flags. We can get the canonical release down to about 800kb uncompressed, but also without rtree (i'll need to check whether we include the math functions, once i finally get access to power outlets) and getting that level of compression might be an emscripten quirk. Note that JSON is always enabled by default now - that was not always the case.

(3) By Stephan Beal (stephan) on 2023-05-23 19:24:00 in reply to 1 [link] [source]

I used wasi to compile SQLite instead of emscripten.

A "related works" entry for that has been added to the wasm page. It currently points to this forum thread, since it provides a description of your work. If you'd prefer that it point somewhere else, just post the link here.