Skip to content

Spxg/sqlite-wasm-rs

Repository files navigation

Crates.io

wasm32-unknown-unknown bindings to the libsqlite3 library.

Usage

Due to restrictions on the target platform, the emscripten toolchain needs to be installed for compilation.

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

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);
}

About VFS

[dependencies]
sqlite-wasm-vfs = "0.1"

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.

VFS Comparison

MemoryVFS SyncAccessHandlePoolVFS RelaxedIdbVFS
Storage RAM OPFS IndexedDB
Contexts All Dedicated Worker All
Multiple connections
Full durability
Relaxed durability
Multi-database transactions
No COOP/COEP requirements

How to implement a VFS

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

About multithreading

This library is not thread-safe:

Use prebuild libsqlite3.a

We provide the ability to use prebuild libsqlite3.a, cargo provides a links field that can be used to specify which library to link to. With the help of overriding build scripts, you can overriding its configuration in your crate and link sqlite to your prebuild libsqlite3.a.

More see use-prebuild-lib example.

Minimum supported Rust version (MSRV)

The minimal officially supported rustc version is 1.82.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.

About

wasm32-unknown-unknown bindings to the libsqlite3 library.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published