NetLife Guru

Open source Go packages for fast, maintainable web systems. Built with a documentation-first approach.

Product
OverviewGolang packagesNews
Documentation
DocumentationGo LoggerGo RouterGo DB Form
Company
OverviewContactNewsGitHub
Community / Support
Supportinfo@netlife.guru
© 2026 NetLife Guru. All rights reserved.
GitHubinfo@netlife.guru
NetLife GuruNetLife GuruNetLife Guru
NetLife GuruNetLife GuruNetLife Guru
OverviewDocumentationNewsSupportContact

Golang packages

AboutMulti-DriverSQL Files
DB

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-mysql
  • github.com/netlifeguru/db-postgres
  • github.com/netlifeguru/db-scylla

Installing a driver also installs the shared packages:

  • github.com/netlifeguru/db
  • github.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.Conn interface across drivers
  • Typed Query Helpers: Supports typed helpers such as List, Get, Value, and Maps
  • Prepared Query Objects: Supports reusable db.Query values through Raw and query-based helpers
  • Dialect SQL Support: Selects SQL automatically from db.DialectSQL based on the active driver
  • SQL Model Loading: Loads driver-specific SQL files such as model.sql, model.psql, and model.cql
  • Result Mapping: Integrates with github.com/netlifeguru/mapper for scanning rows into structs and maps
  • Semantic Exec Helpers: Provides Insert, Update, and Delete as readable wrappers around Exec
  • 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.22 or 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-mysql

Then 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

APIPurpose
db.ConnCommon connection interface implemented by drivers
db.ConfigShared connection configuration structure
db.RawCreate a db.Query from SQL/CQL and arguments
db.ListScan multiple rows into []T
db.GetScan zero or one row into T
db.ValueRead a single scalar value
db.MapsRead rows as []map[string]any
db.ExecExecute a statement
db.InsertExecute an insert statement
db.UpdateExecute an update statement
db.DeleteExecute a delete statement
db.DialectBuild a query from db.DialectSQL for the active driver
db.LoadModelLoad driver-specific SQL model files
db.WithConnStore a connection in context.Context
db.ConnFromCtxRead a connection from context.Context

Driver Packages

The shared db package is used through concrete driver packages.

DriverPackageNotes
MySQLgithub.com/netlifeguru/db-mysqlUses MySQL placeholders and LastInsertId behavior
Postgresgithub.com/netlifeguru/db-postgresUses PostgreSQL placeholders and RETURNING patterns
Scyllagithub.com/netlifeguru/db-scyllaUses 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.

Edge Cases

Understand how mapper handles null values, missing columns, extra columns, type conversions, pointers, JSON fields, and row count errors.

Multi-Driver

Build applications that can run with multiple database drivers using db.DialectSQL, SQL files, and the shared DB API.

On this page

DBFeaturesRequirementsQuick ExampleMain APIsDriver PackagesNot an ORM