database

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 23, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const KeyDatabase runtime.Key = "database"

Variables

View Source
var (
	ErrNotInitialized = errors.New("database: not initialized")

	Default *Database
	DB      *gorm.DB
	PGXPool *pgxpool.Pool
)

Functions

func Bind

func Bind(app *runtime.App, db *Database) error

func Close

func Close()

func Gorm

func Gorm(ctxs ...context.Context) *gorm.DB

func GormFrom

func GormFrom(app *runtime.App) *gorm.DB

func Init

func Init(cfg Config, mode string) error

func InitWithConfig

func InitWithConfig(cfg Config, mode string) error

func PGX

func PGX(ctxs ...context.Context) *pgxpool.Pool

func PGXFrom

func PGXFrom(app *runtime.App) *pgxpool.Pool

func PGXTransaction

func PGXTransaction(ctx context.Context, fn func(tx pgx.Tx) error) error

func Paginate

func Paginate(page, pageSize int) func(db *gorm.DB) *gorm.DB

func Table

func Table(model interface{}) string

Table 获取模型的 SQL 安全表名,可直接拼接到原生 SQL 的标识符位置。

func TableName

func TableName(model interface{}) string

TableName 获取模型的完整表名(含 GORM TablePrefix,不带 SQL 引号)。

func TableOf

func TableOf[T any](db *Database) string

func Transaction

func Transaction(ctx context.Context, fn func(tx *gorm.DB) error) error

Types

type Config

type Config struct {
	Driver          string `mapstructure:"driver" yaml:"driver"`
	DSN             string `mapstructure:"dsn" yaml:"dsn"`
	TablePrefix     string `mapstructure:"table_prefix" yaml:"table_prefix"`
	Schema          string `mapstructure:"schema" yaml:"schema"`
	MaxOpenConns    int    `mapstructure:"max_open_conns" yaml:"max_open_conns"`
	MaxIdleConns    int    `mapstructure:"max_idle_conns" yaml:"max_idle_conns"`
	ConnMaxLifetime int    `mapstructure:"conn_max_lifetime" yaml:"conn_max_lifetime"`
	LogLevel        string `mapstructure:"log_level" yaml:"log_level"`
}

type Database

type Database struct {
	Gorm   *gorm.DB
	PGX    *pgxpool.Pool
	Config Config
}

func From

func From(app *runtime.App) *Database

func New

func New(ctx context.Context, cfg Config, mode string) (*Database, error)

func (*Database) Close

func (db *Database) Close() error

func (*Database) Table

func (db *Database) Table(model any) string

func (*Database) TableName

func (db *Database) TableName(model any) string

type Page

type Page struct {
	Page     int
	PageSize int
}

func (Page) Limit

func (p Page) Limit() int

func (Page) Offset

func (p Page) Offset() int

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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