Skip to content

About

Pure Go database/sql driver for SQLite in the browser using WebAssembly, with no cgo

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

5 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Unit Tests Go Reference Releases Discord Discussion

About

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.

Installing

go get github.com/xo/sqlite3wasm

The 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.

Using

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.

How it works

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.

Testing

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.

Documents

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.

Related projects

  • usql is a universal command line client for SQL databases. It is the program that the browser tutorial runs.
  • usql-site is the website of usql, which will hold the tutorial.
  • dburl parses database URLs for usql and names this driver for the scheme sqlite3wasm.
  • dbmeta reads the metadata of a database for usql, and reads SQLite for this driver.
  • dbimp holds the xo drivers for databases with an HTTP interface, whose layout this driver follows. odbc, cassandra and oracle are the other xo drivers.
  • 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-sqlite3 runs SQLite on wazero, and is where to look for a full featured driver of that kind.

Contributing

Read CONTRIBUTING.md and AGENTS.md. Ask questions on Discord.

About

Pure Go database/sql driver for SQLite in the browser using WebAssembly, with no cgo

Topics

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages