sqlc WASM plugin ? generate type-safe MoonBit database layer from SQL queries
| Path | Audience | What you need |
|---|---|---|
| A — mooncakes.io | Use generated types.mbt / queries.mbt in your app | MoonBit + moon add (no plugin repo clone) |
| B — Plugin repo | Build WASM plugin, run examples, contribute | MoonBit + sqlc + this repository |
| Item | Value |
|---|---|
| Package | Mairzzcllo/moonbit_sqlc_plugin |
| Version | 0.1.8 |
| Import path | Mairzzcllo/moonbit_sqlc_plugin/runtime |
| Docs | https://mooncakes.io/docs/Mairzzcllo/moonbit_sqlc_plugin |
| Target | wasm-gc (do not use native) |
moon update
moon add Mairzzcllo/moonbit_sqlc_plugin@0.1.8
moon check --target wasm-gc{
"deps": {
"Mairzzcllo/moonbit_sqlc_plugin": "0.1.8"
}
}import {
"Mairzzcllo/moonbit_sqlc_plugin/runtime" @runtime,
}moon check --target wasm-gc
moon test --target wasm-gcmoon add pulls from mooncakes.io and does not require login. Only moon publish needs credentials.
mkdir myapp && cd myapp{
"name": "your_org/myapp",
"version": "0.1.0",
"preferred-target": "wasm-gc",
"supported-targets": "+wasm+wasm-gc"
}import {
"Mairzzcllo/moonbit_sqlc_plugin/runtime" @runtime,
}moon update
moon add Mairzzcllo/moonbit_sqlc_plugin@0.1.8
# copy types.mbt + queries.mbt here
moon check --target wasm-gcmoon add Mairzzcllo/moonbit_sqlc_plugin@0.1.8 # upgrade
moon remove Mairzzcllo/moonbit_sqlc_plugin # remove.\scripts\setup-mooncakes.ps1 -Version 0.1.8bash scripts/setup-mooncakes.sh --version 0.1.8git clone https://github.com/Mairzzcllo/MoonBit-sqlc-WASM-plugin.git
cd MoonBit-sqlc-WASM-plugin
moon check
moon testmoon build --target wasm --release| Mode | Path |
|---|---|
| release (recommended) | _build/wasm/release/build/plugin/plugin.wasm |
| debug | _build/wasm/debug/build/plugin/plugin.wasm |
chmod +x scripts/run-example.sh scripts/setup-mooncakes.sh
bash scripts/run-example.sh
bash scripts/run-example.sh --full --release --skip-build.\scripts\run-example.ps1
.\scripts\run-example.ps1 -Full -Release -SkipBuild
$OutputEncoding = [Console]::OutputEncoding = [Text.Encoding]::UTF8version: "2"
plugins:
- name: moonbit
wasm:
url: file://./_build/wasm/release/build/plugin/plugin.wasm
sha256: ""
sql:
- engine: postgresql
schema: schema.sql
queries: query.sql
codegen:
- out: gen
plugin: moonbit
options:
package_name: myappsqlc generateNote: Do not commit platform-specific sha256 hashes. Use scripts/sync-sqlc-sha256.ps1 after building locally.
fn example(db: @runtime.DB) {
match query_get_user(db, 42L) {
Ok(user) => println(user.name)
Err(@runtime.DBError::NoRows) => println("not found")
Err(e) => println("error: \{e}")
}
}| Option | Default | Description |
|---|---|---|
| package_name | "main" | Legacy option (parsed, not emitted since 0.1.7) |
| emit_sql_as_comment | true | Embed SQL above each function |
| emit_json_tags | false | Emit @json.tag(...) |
| emit_empty_slices | false | Return [] for empty :many results |
| emit_exact_table_names | false | Singularize table names (users → User) |
| emit_methods_with_db_argument | false | sqlc compat; always emits standalone query_* fns |
| Symptom | Fix |
|---|---|
| moonbit_simd.h missing | Use --target wasm-gc, not native |
| moon add package not found | Run moon update first to refresh registry index |
| Generated code missing DB / Row | Add runtime import in moon.pkg |
| WASM path not found | Use _build/wasm/..., not target/ |
| sha256 mismatch | Do not commit local hashes; run scripts/sync-sqlc-sha256.ps1 |
| Windows garbled output | $OutputEncoding = [Console]::OutputEncoding = [Text.Encoding]::UTF8 |
| sqlc cannot find plugin | Build WASM first; verify file:// path matches build mode |
plugin/ # WASM plugin (codegen + WASI I/O)
runtime/ # Generated-code runtime (published on mooncakes)
examples/users/ # Reproducible example
tests/ # golden + integration tests
docs/ # API reference and quickstart
scripts/ # run-example / setup-mooncakes / sync-sqlc-sha256moon check --deny-warn
moon test --deny-warn # 937 inline tests
moon build --target wasm --releasetests/integration/wasm/validate_plugin.ps1 -TestSqlc -Release -SkipBuild
tests/integration/e2e/run_e2e.ps1 -SkipBuild -Releasesqlc (protobuf) → wasi_io → codec → adapter → ir
→ type_codegen / query_codegen → ast → emitter → types.mbt + queries.mbt| Resource | Link |
|---|---|
| Quickstart | docs/quickstart.md · 中文 · 日本語 |
| Runtime API | docs/runtime-api.md · 中文 · 日本語 |
| Examples | examples/README.md · 中文 · 日本語 |
| mooncakes | https://mooncakes.io/docs/Mairzzcllo/moonbit_sqlc_plugin |
| GitHub | https://github.com/Mairzzcllo/MoonBit-sqlc-WASM-plugin |
Third-party notices are in NOTICE. WASM I/O protocol reference: sqlc-gen-greeter (MIT).
sqlc WASM plugin ? generate type-safe MoonBit database layer from SQL queries