#sqlite-wasm #sqlite #javascript

sqlite-wasm-rs

Provide sqlite solution for wasm32-unknown-unknown target

9 releases

new 0.3.2 Apr 4, 2025
0.3.1 Mar 14, 2025
0.3.0 Feb 14, 2025
0.2.4 Feb 11, 2025
0.1.0 Dec 29, 2024

#59 in WebAssembly

Download history 165/week @ 2024-12-27 323/week @ 2025-01-03 694/week @ 2025-01-10 707/week @ 2025-01-17 197/week @ 2025-01-24 356/week @ 2025-01-31 1156/week @ 2025-02-07 643/week @ 2025-02-14 383/week @ 2025-02-21 1279/week @ 2025-02-28 1177/week @ 2025-03-07 1863/week @ 2025-03-14 1840/week @ 2025-03-21 1747/week @ 2025-03-28

6,915 downloads per month

MIT license

12MB
184K SLoC

C 177K SLoC // 0.3% comments Rust 6.5K SLoC // 0.0% comments JavaScript 10 SLoC Shell 6 SLoC

Contains (static library, 1.5MB) library/libsqlite3linked.a, (static library, 1MB) library/libsqlite3.a

SQLite Wasm Rust

Crates.io

Provide sqlite solution for wasm32-unknown-unknown target.

Usage

[dependencies]
# Using `bundled` default feature causes us to automatically compile
# and link in an up to date, requires the emscripten toolchain
sqlite-wasm-rs = "0.3"
[dependencies]
# If you don't have the emscripten toolchain, you can use the `precompiled` feature
sqlite-wasm-rs = { version = "0.3", default-features = false, features = ["precompiled"] }
use sqlite_wasm_rs::{self as ffi, relaxed_idb_vfs::install as install_idb_vfs};

async fn open_db() {
    // open with memory vfs
    let mut db = std::ptr::null_mut();
    let ret = unsafe {
        ffi::sqlite3_open_v2(
            c"mem.db".as_ptr().cast(),
            &mut db as *mut _,
            ffi::SQLITE_OPEN_READWRITE | ffi::SQLITE_OPEN_CREATE,
            std::ptr::null()
        )
    };
    assert_eq!(ffi::SQLITE_OK, ret);

    // install relaxed-idb persistent vfs and set as default vfs
    install_idb_vfs(None, true).await.unwrap();

    // open with relaxed-idb vfs
    let mut db = std::ptr::null_mut();
    let ret = unsafe {
        ffi::sqlite3_open_v2(
            c"relaxed-idb.db".as_ptr().cast(),
            &mut db as *mut _,
            ffi::SQLITE_OPEN_READWRITE | ffi::SQLITE_OPEN_CREATE,
            std::ptr::null()
        )
    };
    assert_eq!(ffi::SQLITE_OK, ret);
}

About VFS

The following vfs have been implemented:

  • memory: as the default vfs, no additional conditions are required, store the database in memory.
  • sahpool: ported from sqlite-wasm, store the database in opfs.
  • relaxed-idb: store the database in blocks in indexed db.

Go to here to check it out.

About multithreading

This library is not thread-safe:

Use external libc

As mentioned above, sqlite is now directly linked to emscripten's libc, but we provide the ability to customize libc, cargo provides a links field that can be used to specify which library to link to.

We created a new sqlite-wasm-libc library with no implementation and only a links = "libc" configuration, and then with the help of overriding build scripts, you can overriding its configuration in your crate and link sqlite to your custom libc.

More see custom-libc example.

Why provide precompiled library

Since wasm32-unknown-unknown does not have libc, emscripten is used here for compilation, otherwise we need to copy a bunch of c headers required for sqlite3 compilation, which is a bit of a hack for me. If sqlite3 is compiled at compile time, the emscripten toolchain is required, and we cannot assume that all users have it installed. (Believe me, because rust mainly supports the wasm32-unknown-unknown target, most people do not have the emscripten toolchain). Considering that wasm is cross-platform, vendor compilation products are acceptable.

About security:

  • You can specify the bundled feature to compile sqlite locally, which requires the emscripten toolchain.
  • Currently all precompiled products are compiled and committed through Github Actions, which can be tracked, downloaded and compared.

Precompile Workflow | Change History | Actions

  • diesel: A safe, extensible ORM and Query Builder for Rust.
  • rusqlite: Ergonomic bindings to SQLite for Rust.
  • sqlite-wasm: SQLite Wasm conveniently wrapped as an ES Module.
  • sqlite-web-rs: A SQLite WebAssembly backend for Diesel.
  • wa-sqlite: WebAssembly SQLite with support for browser storage extensions.

Dependencies

~10–18MB
~247K SLoC