database

package
v0.1.10 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 20 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 InstrumentGorm added in v0.1.4

func InstrumentGorm(db *gorm.DB) error

func InstrumentPgxPoolConfig added in v0.1.4

func InstrumentPgxPoolConfig(cfg *pgxpool.Config) 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 QuoteTablePath added in v0.1.4

func QuoteTablePath(path string) string

func SchemaName added in v0.1.4

func SchemaName(model interface{}) string

SchemaName 获取模型所在 schema。模型未声明 schema 时返回配置中的默认 schema。

func Table

func Table(model interface{}) string

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

func TableName

func TableName(model interface{}) string

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

func TableOf

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

func TablePath added in v0.1.4

func TablePath(model interface{}) string

TablePath 获取模型完整表名(含 schema,不带 SQL 引号)。

func TablePathWithName added in v0.1.4

func TablePathWithName(model any, table string) string

func TableWithName added in v0.1.4

func TableWithName(model any, table string) 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) SchemaName added in v0.1.4

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

func (*Database) Table

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

func (*Database) TableName

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

func (*Database) TablePath added in v0.1.4

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

func (*Database) TablePathWithName added in v0.1.4

func (db *Database) TablePathWithName(model any, table string) string

func (*Database) TableWithName added in v0.1.4

func (db *Database) TableWithName(model any, table string) 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