gorm

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package gorm provides GORM helpers for Postgres. Package gorm 提供 Postgres 的 GORM 辅助函数。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildDSN

func BuildDSN(cfg Config) (string, error)

BuildDSN returns the Postgres DSN for cfg. BuildDSN 返回 cfg 的 Postgres DSN。

func Connect

func Connect(ctx context.Context, cfg Config) (*gorm.DB, error)

Connect opens and verifies a Postgres-backed GORM DB. Call Connect(ctx, Config{...}). Connect 打开并验证基于 Postgres 的 GORM DB。 调用 Connect(ctx, Config{...})。

Example / 示例:

db, _ := gorm.Connect(ctx, gorm.Config{Host: "localhost", Port: 5432, User: "app", Database: "db"})

func NewLogger

func NewLogger(opts LogOptions) logger.Interface

NewLogger returns a GORM logger backed by slog. Call NewLogger(LogOptions{Logger: base, Level: slog.LevelInfo}). NewLogger 返回基于 slog 的 GORM logger。 调用 NewLogger(LogOptions{Logger: base, Level: slog.LevelInfo})。

func Ping

func Ping(ctx context.Context, db *gorm.DB) error

Ping checks connectivity through db.DB(). Ping 通过 db.DB() 检查连通性。

Types

type Config

type Config struct {
	Host                 string
	Port                 int
	User                 string
	Password             string
	Database             string
	SSLMode              string
	MaxOpenConns         int
	MaxIdleConns         int
	ConnMaxLifetime      time.Duration
	PreferSimpleProtocol bool
	Logger               logger.Interface
}

Config configures Connect. Config 配置 Connect。

type LogOptions

type LogOptions struct {
	Logger               *slog.Logger
	Level                slog.Level
	Disabled             bool
	IgnoreRecordNotFound bool
	// IncludeQueryParams logs SQL query parameter values.
	// Keep it false for production logs.
	// IncludeQueryParams 记录 SQL 查询参数值。
	// 生产日志保持 false。
	IncludeQueryParams bool
}

LogOptions configures NewLogger. LogOptions 配置 NewLogger。

Jump to

Keyboard shortcuts

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