Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

38 Commits

Folders and files

Repository files navigation

go-wasmsqlite

A minimal WebAssembly SQLite driver for Go's database/sql, running SQLite inside the same Web Worker as the Go WASM binary with OPFS persistence.

This is the slim driver used by godash's web target. It implements a single route — the SQLite OO direct API (sqlite3.oo1.OpfsDb / sqlite3.oo1.DB) — with no nested Worker bridge, no embedded assets, and no migration/dump helpers.

Usage

The driver registers itself under the name wasmsqlite when imported:

import (
	"database/sql"

	_ "github.com/nosuta/go-wasmsqlite"
)

db, err := sql.Open("wasmsqlite", "file=/app.db")

DSN

key default meaning
file /app.db database path in the OPFS VFS
vfs opfs virtual file system to use

Runtime requirements

  • Go 1.24+ with GOOS=js GOARCH=wasm.
  • The page must be cross-origin isolated (COOP/COEP) for OPFS.
  • sqlite3.js (which defines sqlite3InitModule) and sqlite3.wasm must be served next to the worker that runs the WASM binary. godash downloads the official SQLite WASM distribution into web/ automatically.
  • A Content-Security-Policy, if the host sends one, must allow 'unsafe-eval' (or no script-src at all). The driver installs a small JavaScript try/catch wrapper with the Function constructor; see below.

sqlite3InitModule() can run only once per Worker (its Emscripten build consumes a one-shot global init state). The driver therefore initializes the module once and shares it across connections, so database/sql may open — and reopen after a failure — more than one connection in the same Worker.

Error handling

A failed SQLite statement is a normal, expected outcome (a missing table, a constraint violation, a failed ROLLBACK), and the driver always returns it as a Go error — it never panics.

That matters for TinyGo: the SQLite OO API signals failure by throwing a JavaScript exception, and syscall/js turns a thrown value into a Go panic. Recovering from that panic in Go only works on the standard Go toolchain. TinyGo release workers are commonly built with -panic=trap, where recover() never runs and one panic traps the whole wasm module (the worker dies and every in-flight RPC hangs). So the exception is caught on the JavaScript side: the driver creates one helper function with new Function(...) that runs the call inside try/catch and returns { ok, value } or { ok, error }.

Scope

Included: database/sql driver with Exec/Query/Prepare, transactions (BEGIN IMMEDIATE/COMMIT/ROLLBACK), BLOB round-tripping, and OPFS persistence.

Intentionally excluded: golang-migrate integration, asset embedding, VFS detection, and database dump/load. The published module previously carried these (and debug logging); they are removed because the only consumer is the godash web SQLite wrapper, which uses the driver directly.

Tests

go test ./... covers the parts that compile natively. The OPFS behaviour runs in a browser: webtest/ holds a js && wasm test binary, executed with a wasmbrowsertest-style harness that serves sqlite3.js/sqlite3.wasm next to the worker (for example godash's cmd/go_js_wasm_exec):

GOOS=js GOARCH=wasm go test -exec <path-to-go_js_wasm_exec> ./webtest

It covers reopening the database in one Worker, which is what an application does when it retries a failed open.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages