Skip to content

kvdb/sqlbase: support GOOS=js/wasm builds with a pluggable SQL driver #10936

Description

@Roasbeef

In this issue, we'd like to run kvdb/sqlbase (the kvdb-over-SQL walletdb backend) in a GOOS=js GOARCH=wasm build, so that walletdb-backed code can persist into a browser SQLite database, e.g. an OPFS-backed SQLite compiled to WASM and exposed through database/sql.

The sqlbase translation layer itself is already driver-agnostic: it speaks plain database/sql and keys off Config.DriverName / Config.Dsn, so in principle it runs on any registered driver, including a browser SQLite one. The blocker is purely how the package is gated and wired, not the logic.

  1. The real sqlbase files (db.go, readwrite_bucket.go, readwrite_cursor.go, readwrite_tx.go, schema.go) are tagged kvdb_postgres || (kvdb_sqlite && !<exotic arches>). Without one of those tags you get no_sql.go, whose Init is a no-op stub, so a plain js/wasm build never compiles the actual bucket/cursor implementation.
  2. The obvious way to flip them on, -tags kvdb_sqlite, also pulls in kvdb/sqlite/db.go, which blank-imports modernc.org/sqlite to register the driver. modernc has no js/wasm target (the arch carve-outs already baked into that build tag are modernc's platform matrix), so that path can't compile for the browser at all.
  3. There's also a native coupling inside the shared layer: sqlbase/db_conn_set.go blank-imports github.com/jackc/pgx/v4/stdlib, which isn't needed when the caller supplies its own driver.

What would help is a way to compile the sqlbase translation layer under js/wasm against a caller-registered driver, decoupled from the modernc registration. A few shapes that would work:

  • Separate the concrete driver registration (the modernc.org/sqlite blank import in kvdb/sqlite) from the sqlbase translation layer, so the latter builds without forcing a native driver into the binary.
  • Add a build path (allow the sqlbase files to compile under js && wasm, or a dedicated tag) where NewSqlBackend works purely off Config.DriverName with no driver force-registered.
  • Move the pgx/v4/stdlib blank import out of the shared db_conn_set.go and behind the postgres-specific tag.

The concrete win: a wasm consumer registers a browser SQLite driver via sql.Register(...), hands its name and DSN to sqlbase.Config, and gets back a working walletdb.DB (buckets, cursors, txns) with no native-driver baggage in the binary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions