WebAssembly functions for your SQLite-compatible database
libSQL, Turso's open-contribution fork of SQLite, adds user-defined functions written in WebAssembly and registered directly from SQL with a CREATE FUNCTION ... LANGUAGE wasm statement, instead of only through SQLite's C API. WebAssembly was chosen for its sandboxing, portability, performance and wide use as a compile target from languages such as Rust and Zig. A walkthrough compiles Rust encrypt/decrypt functions with the libsql_bindgen crate or its playground and loads them as binary blobs in the libSQL shell. Functions cannot touch system resources such as time, randomness or the network, and their signatures must map to INTEGER, REAL, TEXT and BLOB types.
Related posts
- How We Built the Turso Shell in the Browser
Turso mediumAdeptTurso's SQLite-compatible database can run entirely in the browser via WebAssembly, persisting data to OPFS with no backend. The walkthrough builds a SQL shell with Vite, React and TypeScript. It covers importing from the @tursodatabase/database-wasm/vite export to work around a Vite dev-server WASM issue, and setting COOP/COEP headers so SharedArrayBuffer is available. It then builds a query executor that maps prepared-statement results into a table, and upgrades the textarea to a CodeMirror editor with SQL highlighting, autocomplete and a Mod-Enter keybinding.
- Introducing Turso in the Browser
Turso mediumAdeptTurso, a from-scratch SQLite rewrite, now runs in the browser via WebAssembly as the npm package @tursodatabase/database-wasm, with a better-sqlite3-like API whose methods are all async because of OPFS. Query compute runs on the main UI thread, since benchmarks showed offloading to a worker cost too much in communication overhead. A Web Worker handles filesystem I/O through the synchronous OPFS interface and shares buffers with the main thread via SharedArrayBuffer, which requires COOP/COEP headers and locks a database to one tab. The code is compiled to wasm32-wasip1-threads with napi-rs v3, and there are dedicated Vite and Turbopack exports plus a CDN build that inlines the WASM as base64 at the cost of a larger bundle.
- Introducing Limbo: A complete rewrite of SQLite in Rust
Turso easyNoviceLimbo is Turso's from-scratch rewrite of SQLite in Rust that aims for full language and file-format compatibility, memory safety and a modern architecture. The team chose a rewrite over the libSQL fork because invasive changes such as natural vector-index syntax and async I/O were hard to make in C without SQLite's proprietary test suite. Deterministic Simulation Testing is built in from the start, and Antithesis is used for system-level fault simulation, which has already caught io_uring partial-write bugs. Early results include a fully asynchronous sqlite3_step, a WASM-first build that works with Drizzle, fewer tunables, and a point query about 20% faster than tuned SQLite (506ns vs 620ns).
- Introducing Fast, Native Encryption in Turso Database
Turso mediumAdeptTurso Database, a Rust rewrite of SQLite, now supports native encryption at rest using AEAD ciphers (AEGIS-256 and AES-GCM). Each page is encrypted individually with a fresh nonce, and the nonce and auth tag are stored in SQLite's per-page reserved space. Database and WAL files are encrypted, while the 100-byte header is not. Keys are supplied per connection via URI and kept only in memory. Benchmarks with AEGIS-256 show about 6% read and 14% write latency overhead on larger databases and under 3% on total runtime, with key derivation, rekeying and encrypting existing databases still planned.
- Postgres Foreign Data Wrappers with Wasm
Supabase mediumAdeptSupabase Wrappers, a Rust framework for building Postgres Foreign Data Wrappers, now supports WebAssembly FDWs that are downloaded from URLs like GitHub or S3, checksum-verified, cached and run in a sandboxed Wasm runtime inside the Postgres extension. The host extension provides the runtime, an HTTP interface and utilities, while each guest FDW fetches data from HTTP JSON APIs. TCP-based databases and local files are not yet supported. Snowflake and Paddle Wasm FDWs ship built in, a SQL walkthrough shows creating a Paddle server and foreign table, and wrappers from untrusted sources should never be installed.