db

module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 9, 2026 License: LGPL-3.0

README

db

GORM database helpers for Go apps: open SQLite or Postgres from one config struct, with the same API and the same errors on both.

CI

Features

  • Pick SQLite or Postgres in your config file, with no code change
  • Use SQLite from many goroutines without "database is locked": writes queue on one connection while reads run in parallel
  • Catch duplicate keys and foreign-key violations the same way on every database
  • Build without CGO, or use the CGO SQLite driver when you need extensions such as sqlite-vec
  • Compile only the database drivers your app uses

Add to your project

go get github.com/go-bumbu/db@latest

The CGO SQLite driver (gormdb/sqlitecgo) needs a C compiler. The pure-Go one (gormdb/sqlite) builds anywhere but writes about 100x slower (benchmark).

import (
	"github.com/go-bumbu/db/gormdb"
	"github.com/go-bumbu/db/gormdb/sqlite"
	"gorm.io/gorm"
)

func openDB() (*gorm.DB, error) {
	// describe the database; usually filled from your config file
	cfg := gormdb.Cfg{
		Driver: gormdb.DriverSqlite,
		Sqlite: gormdb.SqliteCfg{Path: "data/app.db"},
	}
	// open it with the drivers this app supports
	return gormdb.Open(cfg, gormdb.Drivers{Sqlite: sqlite.Driver{}})
}

Release it with gormdb.Close(db) on shutdown.

SQLite's extra rules

The same code runs on SQLite and Postgres as long as it keeps to these, since the SQLite writer is a single connection:

  • Inside db.Transaction or db.Connection, use only tx; a statement on db waits forever.
  • SQL that depends on the connection (last_insert_rowid(), changes(), temp tables) needs a transaction or db.Connection.
  • db.DB() is the writer pool: do not tune it, and release through gormdb.Close.
  • WITH ... queries go to the writer, even read-only ones. Force a statement with db.Clauses(gormdb.Read) or db.Clauses(gormdb.Write).

Test on a temporary file with the same setup, not on :memory::

db := gormdbtest.Sqlite(t, sqlite.Driver{})
Detect duplicates
// constraint errors come back as GORM's sentinels, on every driver
if err := db.Create(&user).Error; errors.Is(err, gorm.ErrDuplicatedKey) {
	return ErrEmailTaken
}
Drivers in your build
  • gormdb/sqlite (glebarez) registers the database/sql driver name sqlite, the same name as modernc.org/sqlite. A binary that links both panics at start-up.
  • Your binary links only the driver packages you import, but your module graph and go.sum list all three (glebarez, mattn and pgx), as they share one module.

API reference and the routing rules for reads and writes: pkg.go.dev/github.com/go-bumbu/db/gormdb.

Development

See DEVELOPMENT.md.

Licensed under the GNU LGPL v3.

Directories

Path Synopsis
Package gormdb opens a GORM connection to the database selected by configuration.
Package gormdb opens a GORM connection to the database selected by configuration.
gormdbtest
Package gormdbtest opens databases for tests.
Package gormdbtest opens databases for tests.
internal/sqliteerr
Package sqliteerr maps SQLite constraint errors to gorm's sentinel errors.
Package sqliteerr maps SQLite constraint errors to gorm's sentinel errors.
internal/sqlitetest
Package sqlitetest is the behaviour suite every SQLite driver adapter must pass; each driver package runs it from its own tests.
Package sqlitetest is the behaviour suite every SQLite driver adapter must pass; each driver package runs it from its own tests.
postgres
Package postgres adapts the gorm.io/driver/postgres driver (pgx) to gormdb.
Package postgres adapts the gorm.io/driver/postgres driver (pgx) to gormdb.
sqlite
Package sqlite adapts the pure-Go glebarez SQLite driver to gormdb, so the binary builds without CGO.
Package sqlite adapts the pure-Go glebarez SQLite driver to gormdb, so the binary builds without CGO.
sqlitecgo
Package sqlitecgo adapts the CGO mattn/go-sqlite3 driver to gormdb.
Package sqlitecgo adapts the CGO mattn/go-sqlite3 driver to gormdb.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL