better-sqlite3 icon indicating copy to clipboard operation
better-sqlite3 copied to clipboard

[Request] WebAssambly build

Open panstromek opened this issue 6 years ago • 10 comments

I am not sure how much is this possible, but I would like to use this library compiled do to WASM. I know there are some projects that already compile SQLite to WASM or ASM.js (most notably SQL.js), but honestly I like your API the most, and some of these are not mature enough.

Main reason is that I use this on multiple different devices and I have often problem with compiling it. I cannot always cross compile so I need to build the package on the target device during deployment, which often fails for some obscure reasons and behaves differently on each device.

WASM is supported in Node without compilation during installation. This would allow me to bundle everything and deploy without compiling anything.

Moreover, I need to use the database in a browser, and this would allow me to reuse code without creating some adapters, because mentioned libs have have slightly different APIs.

I am really not sure if that is even possible here, though ;) thx for great lib anyway ;)

panstromek avatar Feb 22 '19 07:02 panstromek

It's an interesting request. I can see the benefits, but I'm not familiar enough with WebAssembly to charge forward right now.

JoshuaWise avatar Feb 27 '19 15:02 JoshuaWise

+1 for wasm

lygstate avatar Feb 27 '19 15:02 lygstate

I actually compiled SQLite to webassembly for one of my projects, it works great except for Windows. The file directory/structure provided by Node doesn't allow the underlying SQLite functions to mount or read a database, I wasn't able to find a way around this and abandoned SQLite for WASM. It does work just fine in the browser (for memory databases) and MacOS/Linux.

A working build script and files are here if anyone wants to work with it: https://github.com/ClickSimply/snap-db/tree/688bcd86b434f357bb73f702d65c8964a5978a29/src-wasm

only-cliches avatar Aug 25 '19 03:08 only-cliches

Error while building better-sqlite3 with node-gyp on Android (termux). I can post more error information later. But building node-sqlite3 on Android is OK.

So WebAssembly is also useful here.

s20208413 avatar Mar 02 '20 14:03 s20208413

Webassembly version: https://github.com/wireapp/websql/pull/84#issuecomment-593173030

lygstate avatar Mar 07 '20 10:03 lygstate

Hey, seems like the technology is gradually getting more mature for this. I'd imagine using a WASI build of sqlite3 (example here) along with the built in WASI implementation in Node 16 could be a way to make this happen.

Other WASM versions of sqlite, such as websql or sql.js, don't have a simple cross platform way of interacting with the file system. This is the big benefit of WASI. It gives a standard sandboxed system interface for stuff like file system access.

Implementing a WASI/Node option could mitigate issues like #694 and make downstream cross platform distribution much more sustainable.

micahscopes avatar Sep 15 '21 05:09 micahscopes

Just chiming in here, I love this library and want to use it in the browser for a wasm-based sqlite3 deployment.

It seems the Sqlite project is taking on Wasm as a supported platform target.

I haven't dug in too deep yet, but just trying to build better-sqlite3 for a browser using esbuild's current instructions, I get a handful of errors (23x in better-sqlite v7.6.2).

These are essentially in 2 categories:

Node libraries like: path, util and fs

✘ [ERROR] Could not resolve "path"

    node_modules/file-uri-to-path/index.js:6:18:
      6 │ var sep = require('path').sep || '/';
        ╵                   ~~~~~~

Destructuring assignments

✘ [ERROR] Transforming destructuring to the configured target environment ("chrome58", "edge16", "firefox57", "safari11") is not supported yet

    node_modules/better-sqlite3/lib/methods/serialize.js:2:6:
      2 │ const { cppdb } = require('../util');

For the second issue (destructuring), I was able to update my build to target edge18 instead of edge16 and this resolved that set and left me with only 8 errors of the first variety.

I'm going to take a look and see if there are some simple things that could get us running in the browser.

lukebayes avatar Nov 16 '22 03:11 lukebayes

It's more complicated than I hoped and I've got to get some sleep tonight. I'll try to make some time to take another look in the next week or two.

Any thoughts or tips would be appreciated.

Thanks again for a great library!

lukebayes avatar Nov 16 '22 04:11 lukebayes

wasm build of sqlite3 has gone official…having this api in the browser would be most excellent

bump!

cellvia avatar Feb 15 '23 06:02 cellvia

Looks like @tndrle has implemented exactly this, with some small limitations: https://github.com/tndrle/node-sqlite3-wasm

flaviut avatar Sep 25 '23 22:09 flaviut