Documentation
¶
Index ¶
- Variables
- func New(opts ...Option) (*gorm.DB, error)
- func NewLogger(opts ...GormLoggerOption) *gormLogger
- func WithOmitVariablesFromTrace(ctx context.Context) context.Context
- type Config
- type DBModel
- type DataConf
- type DataSourceManager
- type GormLoggerOption
- type GormOpenTelemetryPlugin
- type Option
- type TraceOption
- type Transaction
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var (
ErrDriverNotFound = errors.New("gorm: driver not found")
)
Functions ¶
func New ¶
Example ¶
package main
import (
"context"
"fmt"
"time"
// "gorm.io/driver/sqlite" // with cgo sqlite3
"github.com/glebarez/sqlite" // without cgo sqlite3
"github.com/go-kratos/kratos/v2/log"
"gorm.io/gorm"
"github.com/omalloc/contrib/kratos/orm"
"github.com/omalloc/contrib/kratos/orm/crud"
"github.com/omalloc/contrib/protobuf"
)
type User struct {
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement;"`
Name string `json:"name" gorm:"column:name;"`
orm.DBModel
}
type userRepo struct {
crud.CRUD[User]
db *gorm.DB
}
func NewUserRepo(db *gorm.DB) *userRepo {
return &userRepo{
CRUD: crud.New[User](db),
db: db,
}
}
func main() {
// file stored = file:test.db?cache=shared&mode=memory&charset=utf8mb4&parseTime=true&loc=Local
// memory stored
db, err := orm.New(
orm.WithDriver(sqlite.Open(":memory:?cache=shared&mode=memory&charset=utf8mb4&parseTime=true&loc=Local")),
orm.WithTracingOpts(orm.WithDatabaseName("test")),
orm.WithLogger(
orm.WIthSlowThreshold(time.Second*2),
orm.WithSkipCallerLookup(true),
orm.WithSkipErrRecordNotFound(true),
orm.WithLogHelper(log.NewFilter(log.GetLogger(), log.FilterLevel(log.LevelInfo))),
),
)
if err != nil {
panic(err)
}
// 创建测试表
_ = db.Session(&gorm.Session{SkipHooks: true}).AutoMigrate(&User{})
repo := NewUserRepo(db)
if err := repo.Create(context.Background(), &User{Name: "test1"}); err != nil {
println(err.Error())
}
if err := repo.Create(context.Background(), &User{Name: "test1"}); err != nil {
println(err.Error())
}
// 自动分页
pagination := protobuf.PageWrap(nil)
users, err := repo.SelectList(context.Background(), pagination)
if err != nil {
println(err.Error())
}
if len(users) != int(pagination.Resp().Total) {
println("data total size not equal, got %d want %d", len(users), pagination.Resp().Total)
}
fmt.Println()
for _, user := range users {
fmt.Printf("%d--%s\n", user.ID, user.Name)
}
}
Output: 1--test1 2--test1
func NewLogger ¶
func NewLogger(opts ...GormLoggerOption) *gormLogger
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config a gorm custom config
type DataSourceManager ¶ added in v1.2.0
type GormLoggerOption ¶
type GormLoggerOption func(*gormLogger)
func WIthSlowThreshold ¶
func WIthSlowThreshold(threshold time.Duration) GormLoggerOption
func WithDebug ¶
func WithDebug() GormLoggerOption
func WithLogHelper ¶
func WithLogHelper(klog log.Logger) GormLoggerOption
WithLogHelper set gorm-logger and log filters..
func WithSkipCallerLookup ¶
func WithSkipCallerLookup(skip bool) GormLoggerOption
func WithSkipErrRecordNotFound ¶
func WithSkipErrRecordNotFound(skip bool) GormLoggerOption
type GormOpenTelemetryPlugin ¶
type GormOpenTelemetryPlugin struct {
// contains filtered or unexported fields
}
func NewTracer ¶
func NewTracer(opts ...TraceOption) *GormOpenTelemetryPlugin
func (*GormOpenTelemetryPlugin) Initialize ¶
func (op *GormOpenTelemetryPlugin) Initialize(db *gorm.DB) error
func (*GormOpenTelemetryPlugin) Name ¶
func (op *GormOpenTelemetryPlugin) Name() string
type Option ¶
type Option func(*Config)
func WithLogger ¶
func WithLogger(opts ...GormLoggerOption) Option
WithLogger set gorm-logger and has debug logger writer..
func WithTracingOpts ¶
func WithTracingOpts(opts ...TraceOption) Option
WithTracingOpts set gorm-tracing some opts. used for opentelemetry.
type TraceOption ¶
type TraceOption interface {
// contains filtered or unexported methods
}
TraceOption allows for managing options for the tracing plugin.
func WithAlwaysOmitVariables ¶
func WithAlwaysOmitVariables() TraceOption
WithAlwaysOmitVariables will omit variables from the span attributes
func WithDatabaseName ¶
func WithDatabaseName(dbName string) TraceOption
WithDatabaseName specified the database name to be used in span names
since its not possible to extract this information from gorm
func WithTracerProvider ¶
func WithTracerProvider(provider trace.TracerProvider) TraceOption
WithTracerProvider sets the tracer provider to use for opentelemetry.
If none is specified, the global provider is used.
type Transaction ¶ added in v1.0.9
type Transaction interface {
Transaction(context.Context, func(ctx context.Context) error) error
WithContext(context.Context) *gorm.DB
}
func NewTransactionManager ¶ added in v1.0.9
func NewTransactionManager(dsm DataSourceManager) Transaction
Source Files
¶
Click to show internal directories.
Click to hide internal directories.