database

package
v0.0.0-...-4c964c4 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package database provides persistent data storage using a postgres or sqlite3 database.

Index

Constants

View Source
const (
	// sqlForUpdate is the sql statement used for locking rows returned by select queries.
	SQLForUpdate = "FOR UPDATE"
)

Variables

View Source
var Builder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)

Builder is a global instance of the sql builder. we are able to hardcode to postgres since sqlite3 is compatible with postgres.

Functions

func Connect

func Connect(ctx context.Context, driver string, datasource string) (*sqlx.DB, error)

Connect to a database and verify with a ping.

func ConnectAndMigrate

func ConnectAndMigrate(
	ctx context.Context,
	driver string,
	datasource string,
	migrators ...Migrator,
) (*sqlx.DB, error)

ConnectAndMigrate creates the database handle and migrates the database.

func ConnectGorm

func ConnectGorm(ctx context.Context, driver string, datasource string, opts ...GormConfigOption) (*gorm.DB, error)

func EncodeToJSONString

func EncodeToJSONString(v any) string

func GormLimit

func GormLimit(size int) int

func GormOffset

func GormOffset(page, size int) int

func Limit

func Limit(size int) uint64

limit returns the page size to a sql limit.

func Must

func Must(db *sqlx.DB, err error) *sqlx.DB

Must is a helper function that wraps a call to Connect and panics if the error is non-nil.

func Offset

func Offset(page, size int) uint64

offset converts the page to a sql offset.

func ProcessGormSQLErrorf

func ProcessGormSQLErrorf(ctx context.Context, err error, format string, args ...interface{}) error

func ProcessSQLErrorf

func ProcessSQLErrorf(ctx context.Context, err error, format string, args ...interface{}) error

Logs the error and message, returns either the provided message. Always logs the full message with error as warning.

Types

type Config

type Config struct {
	Driver     string
	Datasource string
	Host       string
	Port       int
	Username   string
	Password   string
	DBName     string
	Options    string
}

Config specifies the config for the database package.

type GormConfigLogger

type GormConfigLogger struct {
	Level logger.LogLevel
}

func (GormConfigLogger) Apply

func (opt GormConfigLogger) Apply(c *gorm.Config)

type GormConfigOption

type GormConfigOption interface {
	Apply(c *gorm.Config)
}

type Migrator

type Migrator func(ctx context.Context, dbx *sqlx.DB) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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