sql.js icon indicating copy to clipboard operation
sql.js copied to clipboard

"Official" build of SQLite for WebAssembly

Open Daniel15 opened this issue 3 years ago • 7 comments

You may already be aware, but SQLite 3.40.0 was just released this morning, and it adds a beta release of an official WebAssembly build.

https://sqlite.org/wasm/doc/trunk/index.md Changelog: https://sqlite.org/releaselog/3_40_0.html

Your library likely still has some value-added features over using SQLite directly, but it might allow you to simplify this library a little bit?

Daniel15 avatar Nov 16 '22 18:11 Daniel15

Yes, I have been in contact with SQLite maintainers

lovasoa avatar Nov 17 '22 07:11 lovasoa

I am confused (being not a web developer and working on a proof of concept for a feature), what is the recommended way to read a single table SQLite DB file in a browser JS now? This or https://github.com/sqlite/sqlite-wasm?

Gladskih avatar Apr 21 '24 18:04 Gladskih

As a beginner, this library is probably easier to use. sqlite-wasm is more complex but has more features.

lovasoa avatar Apr 21 '24 19:04 lovasoa

So I failed to make "official" package work. It has insane complexity for such trivial task as parse a file. It utilizes "Origin private file system" to import a DB file and requires Web worker for that feature to work because otherwise:

The OPFS sqlite3_vfs cannot run in the main thread because it requires Atomics.wait().

And with sql.js i did just

npm install sql.js

and added into main.js

import initSqlJs from 'sql.js/dist/sql-wasm.js' // I run my app by "npx vite"
import workletURL from "sql.js/dist/sql-wasm.wasm?url";
const SQL = await initSqlJs({ locateFile: (file) => workletURL });
const fetched = await fetch("input.sqlite");
const db = new SQL.Database(new Uint8Array(await fetched.arrayBuffer()));
var queryResults = db.exec(`SELECT MyValue FROM MyTable WHERE MyKey = '42'`);
console.log(queryResults [0].values[0][0]); // and here is the data selected

I am not quite sure yet if I didn't miss some resource disposal and best practices but at least it gives me the data.

Gladskih avatar Apr 22 '24 16:04 Gladskih

. It utilizes "Origin private file system" to import a DB file

@Gladskih For what it's worth, the MDN documentation uses "SQLite database modifications" as an example of a good use case for OPFS: https://developer.mozilla.org/en-US/docs/Web/API/File_System_API/Origin_private_file_system. Regular filesystem access without OPFS is quite a bit slower.

Daniel15 avatar Apr 22 '24 20:04 Daniel15

Yes, I have seen that digging the docs. But there should be the question: "why do we need here a file system at all?" When there are several files, or a lot of data or persistence is required - yes it's most likely reasonable. But it's not the only scenario. And thanks to sql.js for supporting such use case.

Gladskih avatar Apr 22 '24 20:04 Gladskih