#sqlite #sqlite-wasm #javascript

sqlite-wasm-rs

Provide sqlite solution for wasm32-unknown-unknown target

8 releases

0.3.1 Mar 14, 2025
0.3.0 Feb 14, 2025
0.2.4 Feb 11, 2025
0.1.5 Jan 31, 2025
0.1.0 Dec 29, 2024

#77 in WebAssembly

Download history 108/week @ 2024-12-23 175/week @ 2024-12-30 701/week @ 2025-01-06 521/week @ 2025-01-13 452/week @ 2025-01-20 304/week @ 2025-01-27 726/week @ 2025-02-03 1031/week @ 2025-02-10 518/week @ 2025-02-17 287/week @ 2025-02-24 1525/week @ 2025-03-03 1168/week @ 2025-03-10 1753/week @ 2025-03-17

4,753 downloads per month

MIT license

12MB
183K SLoC

C 177K SLoC // 0.3% comments Rust 5.5K SLoC // 0.0% comments 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::export::{self as ffi, install_opfs_sahpool};

async fn open_db() -> anyhow::Result<()> {
    // 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 opfs-sahpool persistent vfs and set as default vfs
    install_opfs_sahpool(None, true).await?;

    // open with opfs-sahpool vfs
    let mut db = std::ptr::null_mut();
    let ret = unsafe {
        ffi::sqlite3_open_v2(
            c"opfs-sahpool.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-vfs: as the default vfs, no additional conditions are required, just use.
  • opfs-sahpool: ported from sqlite-wasm, it provides the best performance persistent storage method.

See https://github.com/Spxg/sqlite-wasm-rs/blob/master/VFS.md

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 issues:

  • 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: https://github.com/Spxg/sqlite-wasm-rs/blob/master/.github/workflows/precompile.yml

Change History: https://github.com/Spxg/sqlite-wasm-rs/commits/master/sqlite-wasm-rs/library

Actions: https://github.com/Spxg/sqlite-wasm-rs/actions?query=event%3Aworkflow_dispatch

  • 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
~241K SLoC