About
DB is a shared Go database layer used by NetLifeGuru database drivers for querying, execution, transactions, dialect SQL, and result mapping.
DB
DB is a shared database layer for Go applications.
It provides one common API for querying, executing statements, handling transactions, loading SQL models, and mapping database results across supported NetLifeGuru drivers.
The db package is not a standalone database driver.
To connect to a real database, install and use one of the driver packages:
github.com/netlifeguru/db-mysqlgithub.com/netlifeguru/db-postgresgithub.com/netlifeguru/db-scylla
Installing a driver also installs the shared packages:
github.com/netlifeguru/dbgithub.com/netlifeguru/mapper
Features
- Shared Database Layer: Provides common querying and execution APIs for supported drivers
- Driver-Based Usage: Works through concrete drivers such as MySQL, PostgreSQL, and Scylla
- Unified Connection Interface: Uses a common
db.Conninterface across drivers - Typed Query Helpers: Supports typed helpers such as
List,Get,Value, andMaps - Prepared Query Objects: Supports reusable
db.Queryvalues throughRawand query-based helpers - Dialect SQL Support: Selects SQL automatically from
db.DialectSQLbased on the active driver - SQL Model Loading: Loads driver-specific SQL files such as
model.sql,model.psql, andmodel.cql - Result Mapping: Integrates with
github.com/netlifeguru/mapperfor scanning rows into structs and maps - Semantic Exec Helpers: Provides
Insert,Update, andDeleteas readable wrappers aroundExec - Transaction Support: Supports transactions for drivers that provide transaction behavior
- Scylla Batch Support: Supports Scylla-specific batch workflows through the Scylla driver
- Multi-Driver Applications: Supports applications that can run against different database engines
- Context-Aware Operations: Query and execution helpers use
context.Context - Standard Go Friendly: Built around explicit SQL, structs, interfaces, and small helper functions
Requirements
This package requires Go 1.22 or newer.
It is designed for modern Go projects and may use language and standard library features introduced in recent Go versions.
- Go:
1.22or newer - Requires a driver: MySQL, PostgreSQL, or Scylla
- Shared dependencies:
github.com/netlifeguru/mapper - Features used: Generics, context-aware APIs, interfaces, reflection-based result mapping
Quick Example
Install a concrete driver first.
go get github.com/netlifeguru/db-mysqlThen create a connection using the driver package.
conn := mysql.New()
err := conn.CreatePool(db.Config{
Identifier: "default",
Host: "127.0.0.1",
Port: 3306,
Database: "app",
Username: "root",
Password: "secret",
})
if err != nil {
return err
}
defer conn.Close()Use the shared db API with the driver connection.
type User struct {
ID int64 `db:"id"`
Name string `db:"name"`
Email string `db:"email"`
Active bool `db:"active"`
}
users, err := db.List[User](
context.Background(),
conn.Fork(),
`
SELECT * FROM users
ORDER BY id DESC
`,
)
if err != nil {
return err
}The same db.List, db.Get, db.Value, db.Maps, db.Insert, db.Update, and db.Delete APIs can be used with the supported drivers.
Main APIs
| API | Purpose |
|---|---|
db.Conn | Common connection interface implemented by drivers |
db.Config | Shared connection configuration structure |
db.Raw | Create a db.Query from SQL/CQL and arguments |
db.List | Scan multiple rows into []T |
db.Get | Scan zero or one row into T |
db.Value | Read a single scalar value |
db.Maps | Read rows as []map[string]any |
db.Exec | Execute a statement |
db.Insert | Execute an insert statement |
db.Update | Execute an update statement |
db.Delete | Execute a delete statement |
db.Dialect | Build a query from db.DialectSQL for the active driver |
db.LoadModel | Load driver-specific SQL model files |
db.WithConn | Store a connection in context.Context |
db.ConnFromCtx | Read a connection from context.Context |
Driver Packages
The shared db package is used through concrete driver packages.
| Driver | Package | Notes |
|---|---|---|
| MySQL | github.com/netlifeguru/db-mysql | Uses MySQL placeholders and LastInsertId behavior |
| Postgres | github.com/netlifeguru/db-postgres | Uses PostgreSQL placeholders and RETURNING patterns |
| Scylla | github.com/netlifeguru/db-scylla | Uses CQL, consistency settings, query tables, and batches |
Not an ORM
DB is not an ORM.
It does not generate SQL, manage schemas, define models, or hide database-specific behavior.
Instead, it gives you a small shared layer for writing explicit SQL while keeping query execution, result mapping, dialect selection, and driver integration consistent across supported databases.