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 |
|
#77 in WebAssembly
4,753 downloads per month
12MB
183K
SLoC
Contains (static library, 1.5MB) library/libsqlite3linked.a, (static library, 1MB) library/libsqlite3.a
SQLite Wasm Rust
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:
JsValue
is not cross-threaded, see https://github.com/rustwasm/wasm-bindgen/pull/955 for details.- sqlite is compiled with
-DSQLITE_THREADSAFE=0
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
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.
Dependencies
~10–18MB
~241K SLoC