|
| 1 | +# SQLx on `wasm32-wasip2` |
| 2 | + |
| 3 | +These examples show SQLx running inside a [WebAssembly Component] on the |
| 4 | +`wasm32-wasip2` target, using a `sqlx::Pool` to connect to a database over the |
| 5 | +host network via [`wasi:sockets`]. They run in any WASIP2 runtime; the |
| 6 | +commands below use [Wasmtime]. |
| 7 | + |
| 8 | +| Example | Backend | |
| 9 | +| ------------------------ | -------- | |
| 10 | +| [`postgres`](./postgres) | Postgres | |
| 11 | +| [`mysql`](./mysql) | MySQL | |
| 12 | + |
| 13 | +## How it works |
| 14 | + |
| 15 | +The examples use the standard **`runtime-tokio`** feature with async I/O. |
| 16 | +Tokio's `net` driver works on `wasm32-wasip2` (via `mio`'s WASI support). |
| 17 | + |
| 18 | +Two target-specific requirements: |
| 19 | + |
| 20 | +- **`--cfg tokio_unstable`**: Tokio's `net` support on `wasm32` is gated behind |
| 21 | + this cfg. It's set for the `wasm32-wasip2` target in |
| 22 | + [`.cargo/config.toml`](./.cargo/config.toml), so a plain |
| 23 | + `cargo build --target wasm32-wasip2` works (no `RUSTFLAGS` needed). |
| 24 | +- **Current-thread runtime**: WASIP2 does not have threads, so each example uses |
| 25 | + `#[tokio::main(flavor = "current_thread")]`. |
| 26 | + |
| 27 | +Each example opens a `Pool`, creates a table, inserts rows in a transaction, then |
| 28 | +reads them back and aggregates — a small round-trip exercising DDL, bind |
| 29 | +parameters, `fetch_all`, and `begin`/`commit`. `LISTEN`/`NOTIFY` and TLS |
| 30 | +(`tls-rustls-*`) also work on this target; see the |
| 31 | +[top-level README](../../README.md#webassembly-wasm32-wasip2). |
| 32 | + |
| 33 | +## Prerequisites |
| 34 | + |
| 35 | +```sh |
| 36 | +rustup target add wasm32-wasip2 |
| 37 | +# Wasmtime: https://wasmtime.dev/ |
| 38 | +``` |
| 39 | + |
| 40 | +## Run |
| 41 | + |
| 42 | +### Postgres |
| 43 | + |
| 44 | +```sh |
| 45 | +# A database to connect to: |
| 46 | +docker run -d --name sqlx-wasip2-pg \ |
| 47 | + -e POSTGRES_PASSWORD=password -e POSTGRES_DB=sqlx \ |
| 48 | + -p 5432:5432 postgres:17 |
| 49 | + |
| 50 | +cd postgres |
| 51 | +cargo build --target wasm32-wasip2 --release |
| 52 | +wasmtime run -S inherit-network \ |
| 53 | + --env DATABASE_URL="postgres://postgres:password@127.0.0.1:5432/sqlx" \ |
| 54 | + target/wasm32-wasip2/release/sqlx-wasip2-postgres.wasm |
| 55 | +``` |
| 56 | + |
| 57 | +Expected output: |
| 58 | + |
| 59 | +```text |
| 60 | +alice: 5 |
| 61 | +carol: 4 |
| 62 | +bob: 3 |
| 63 | +total votes: 12 |
| 64 | +connected via pool to: PostgreSQL 17.x ... |
| 65 | +``` |
| 66 | + |
| 67 | +### MySQL |
| 68 | + |
| 69 | +```sh |
| 70 | +docker run -d --name sqlx-wasip2-mysql \ |
| 71 | + -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=sqlx \ |
| 72 | + -p 3306:3306 mysql:8 |
| 73 | + |
| 74 | +cd mysql |
| 75 | +cargo build --target wasm32-wasip2 --release |
| 76 | +wasmtime run -S inherit-network \ |
| 77 | + --env DATABASE_URL="mysql://root:password@127.0.0.1:3306/sqlx" \ |
| 78 | + target/wasm32-wasip2/release/sqlx-wasip2-mysql.wasm |
| 79 | +``` |
| 80 | + |
| 81 | +Expected output: |
| 82 | + |
| 83 | +```text |
| 84 | +alice: 5 |
| 85 | +carol: 4 |
| 86 | +bob: 3 |
| 87 | +total votes: 12 |
| 88 | +connected via pool to: 8.x.x |
| 89 | +``` |
| 90 | + |
| 91 | +> `wasmtime run -S inherit-network` grants the component access to the host |
| 92 | +> network; it is required for outbound TCP. These examples connect by IP literal |
| 93 | +> (`127.0.0.1`), so that flag alone is sufficient. Connecting by **hostname** |
| 94 | +> additionally needs `--allow-ip-name-lookup` to permit WASI DNS resolution. |
| 95 | +
|
| 96 | +[WebAssembly Component]: https://component-model.bytecodealliance.org/ |
| 97 | +[`wasi:sockets`]: https://github.com/WebAssembly/wasi-sockets |
| 98 | +[Wasmtime]: https://wasmtime.dev/ |
0 commit comments