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 — Supported
- Codecs: int64, float64, string, blob, bool, json, uuid, date, time, datetime, datetime-utc
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.5", = ["postgres"] } # or "sqlite"
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 codecswasm-sql:sqlite/host— SQLite-specific type codecs
License
MIT