Skip to content

About

wasm32-unknown-unknown bindings to the libsqlite3 library.

Topics

Resources

Stars

97 stars

Watchers

2 watching

Forks

Repository files navigation

Crates.io

wasm32-unknown-unknown bindings to the libsqlite3 library.

Usage

[dependencies]
sqlite-wasm-rs = { version = "0.6", features = ["wasm-bindgen"] }
[dependencies]
# Encryption is supported by SQLite3MultipleCiphers
# See <https://utelle.github.io/SQLite3MultipleCiphers>
sqlite-wasm-rs = { version = "0.6", features = ["wasm-bindgen", "sqlite3mc"] }
[dependencies]
# Encryption is supported by SQLCipher
# See <https://www.zetetic.net/sqlcipher/>
sqlite-wasm-rs = { version = "0.6", features = ["wasm-bindgen", "sqlcipher"] }
use sqlite_wasm_rs as ffi;

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);
    assert_eq!(unsafe { ffi::sqlite3_close(db) }, ffi::SQLITE_OK);
}

About VFS

[dependencies]
sqlite-wasm-vfs = { version = "0.3", features = ["sahpool"] }

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.

How to implement a VFS

Here is an example showing how to implement a simple in-memory VFS, see implement-a-vfs example.

cargo run -p rsqlite-vfs --example implement-a-vfs

About multithreading

Multithreading is not supported, SQLite is compiled with -DSQLITE_THREADSAFE=0.

For shared-memory builds, set CFLAGS_wasm32_unknown_unknown="-matomics -mbulk-memory".

Use without wasm-bindgen

No features are enabled by default, provide your own host functions. See JS Host or C Host example.

Use custom SQLite sources

The encryption backends use sources from sqlite3mc-src or sqlcipher-src.

Set SQLITE_WASM_RS_SOURCE_DIR to use custom sources:

  • SQLite: a directory containing sqlite3.c and sqlite3.h.
  • sqlite3mc: a directory containing sqlite3mc_amalgamation.c and sqlite3mc_amalgamation.h.
  • sqlcipher: a source tree matching the sqlcipher-src crate's sqlcipher directory, including LibTomCrypt.
SQLITE_WASM_RS_SOURCE_DIR=/path/to/sqlite cargo build --target wasm32-unknown-unknown --features bindgen

Minimum supported Rust version (MSRV)

The minimal officially supported rustc version is 1.81.0.

Extensions

Extension About
sqlite-vec A vector search SQLite extension that runs anywhere!

Contributions are welcome!

Related Project

  • 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.
  • SQLite3MultipleCiphers: SQLite3 encryption extension with support for multiple ciphers.

Friends

  • moli - Best browser for AI Agent, written in pure Rust.

About

wasm32-unknown-unknown bindings to the libsqlite3 library.

Topics

Resources

Stars

97 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages