Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Official" build of SQLite for WebAssembly #534

Open
Daniel15 opened this issue Nov 16, 2022 · 6 comments
Open

"Official" build of SQLite for WebAssembly #534

Daniel15 opened this issue Nov 16, 2022 · 6 comments

Comments

@Daniel15
Copy link

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 Daniel15 changed the title "Official" version "Official" build of SQLite for WebAssembly Nov 16, 2022
@lovasoa
Copy link
Member

lovasoa commented Nov 17, 2022

Yes, I have been in contact with SQLite maintainers

@Gladskih
Copy link

Gladskih commented Apr 21, 2024

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?

@lovasoa
Copy link
Member

lovasoa commented Apr 21, 2024

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

@Gladskih
Copy link

Gladskih commented Apr 22, 2024

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.

@Daniel15
Copy link
Author

. 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.

@Gladskih
Copy link

Gladskih commented Apr 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants