wasm-sql
Wasmtime host capability for SQL database access from WebAssembly components. Uses sqlx as the underlying database driver.
Note: This library uses experimental async features from wasmtime component model. Required versions: wasmtime 0.41+, wit-bindgen 0.51+
Features
- Connection pooling â efficient connection management via sqlx
- Transactions â full support for begin/commit/rollback with automatic rollback on drop
- Async support â fully async API using wasmtime component model async
Database Support
ð PostgreSQL â â Supported
- Codecs: int16/32/64, float32/64, string, bool, json, uuid, hstore, date, time, timestamp, timestamptz, interval, inet, cidr, macaddr, numeric
ðŠķ SQLite â ð Planned
- Driver ready, codecs pending
ðŽ MySQL â ð Planned
- Driver ready, codecs pending
Missing a codec? Please create an issue with your use case!
Usage
Examples
- examples/host.rs â Host implementation (wasmtime runtime)
- examples/guest-app/ â Guest WASM component example
Host Side (Rust)
Add dependency:
[]
= { = "0.1", = ["postgres"] }
Set up the host:
use Arc;
use ;
use SqlDB;
use PgPoolOptions;
use Linker;
// Your state struct
// Create database pool
let pool = new
.max_connections
.connect
.await?;
let sql_db = new;
// Add wasm-sql imports to linker
let mut linker: = new;
add_to_linker?;
// Create state
let state = HostState ;
See full working example in examples/host.rs.
Guest Side (WASM Component)
See full example in examples/guest-app/.
Add dependency:
[]
= "0.51"
Generate bindings and use:
use ;
use ;
// Execute a parameterized query
async
// Fetch data and decode results using codecs
async
Architecture
âââââââââââââââââââââââ
â WASM Component â
âââââââââââŽââââââââââââ
â WIT imports
âž
âââââââââââââââââââââââ
â wasm-sql (host) â
âââââââââââŽââââââââââââ
â sqlx
âž
âââââââââââââââââââââââ
â Database â
â (PostgreSQL/MySQL/ â
â SQLite) â
âââââââââââââââââââââââ
WIT Worlds
wasm-sql:core/hostâ Core SQL operations (pool, connections, transactions, queries)wasm-sql:postgres/hostâ PostgreSQL-specific type codecs
License
MIT