sqlite3wasm is a database/sql driver for SQLite that needs no
cgo. SQLite is built for WebAssembly with zig and embedded in the
package.
It is made for Go programs that run in a web browser. In a program built with
GOOS=js GOARCH=wasm, the WebAssembly engine of the browser runs SQLite next
to the Go program, at the speed it has anywhere else in that browser. No
WebAssembly runs inside other WebAssembly. The first user is an interactive
tutorial for usql on usql-site.
On every other target, wazero runs the same module inside the Go
program. That build exists so that go test ./... runs on any computer, and
it is not meant for production. For SQLite in a Go program outside a browser,
use mattn/go-sqlite3 or modernc.org/sqlite.
The package also builds with TinyGo, and a TinyGo program is about a third of the size of the same Go program.
go get github.com/xo/sqlite3wasmThe only dependency is wazero, and a browser build does not link it. You need neither zig nor a C compiler. The built module is in the repository.
import (
"database/sql"
_ "github.com/xo/sqlite3wasm"
)
db, err := sql.Open("sqlite3wasm", ":memory:")Every database is in memory. :memory: opens a database for one connection.
Any other name, such as northwind.db, opens a database that every connection
of the same sql.DB shares. The name can start with the scheme
sqlite3wasm:, and the key pragma runs a pragma on each new connection:
sqlite3wasm:northwind.db?pragma=foreign_keys(0)
To give a named database its starting content, such as a file that a page
fetched, call Preload before the database is opened. The first connection
of each sql.DB loads a copy, so a client such as usql opens the name as it
opens any other database:
err := sqlite3wasm.Preload("northwind.db", data)
db, err := sql.Open("sqlite3wasm", "northwind.db")To load a database file into one connection, pass its bytes to
Conn.Deserialize through sql.Conn.Raw:
conn, err := db.Conn(ctx)
err = conn.Raw(func(dc any) error {
return dc.(*sqlite3wasm.Conn).Deserialize(ctx, "main", data)
})Conn.Serialize returns the bytes of a database. Conn.AutoCommit says
whether the connection is outside a transaction.
Placeholders are ?. An error from SQLite is a *sqlite3wasm.Error with the
primary and the extended result code, and errors.Is(err, sqlite3wasm.ErrConstraint) tests the primary code.
docs/INTERFACES.md has the mapping of types.
A context with a deadline stops a long statement, in a browser too. Do not open the first connection from a function that JavaScript calls, such as an event handler, because it waits for the browser to compile the module.
wasm/sqlite3.wasm is SQLite with a small C layer, wasm/xo.c. It has no
file layer and imports no WASI function, only three functions that are built
into JavaScript. So a browser needs no shim and no script of ours to run it.
The driver reaches the module through one of two files, chosen by the js
build tag. machine_wazero.go calls it with wazero. machine_js.go compiles
the embedded module with WebAssembly.compile and calls it with syscall/js.
The rest of the driver is the same for both. One call and one copy move a
whole row. docs/WASM.md has the build, the flags, the imports
and the exports.
go test ./...
cd test && go test ./...The first command runs the tests on wazero. The second runs the same tests in
headless Chrome with chromedp, and runs a small program built by Go
and by TinyGo in Chrome. docs/TESTING.md has the details.
| To do this | Read |
|---|---|
| Build the module, or change a flag, an import or an export | docs/WASM.md |
| Run the tests, on wazero or in a browser | docs/TESTING.md |
| See which interfaces the driver implements and how it maps types | docs/INTERFACES.md |
| Find out why something is the way it is | docs/decisions/README.md |
| Find work that is known and not done | docs/BACKLOG.md |
| See where the work stands | docs/PROGRESS.md |
There are 9 decisions so far.
usqlis a universal command line client for SQL databases. It is the program that the browser tutorial runs.usql-siteis the website ofusql, which will hold the tutorial.dburlparses database URLs forusqland names this driver for the schemesqlite3wasm.dbmetareads the metadata of a database forusql, and reads SQLite for this driver.dbimpholds thexodrivers for databases with an HTTP interface, whose layout this driver follows.odbc,cassandraandoracleare the otherxodrivers.- wazero runs WebAssembly in Go with no cgo.
- zig builds the module.
- chromedp drives Chrome for the browser tests.
- TinyGo builds small Go programs for WebAssembly.
ncruces/go-sqlite3runs SQLite on wazero, and is where to look for a full featured driver of that kind.
Read CONTRIBUTING.md and AGENTS.md. Ask
questions on Discord.