gorm

package
v2.19.2 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewORM

func NewORM(dialector gorm.Dialector, gormCfg *gorm.Config, options ...Option) (*gorm.DB, error)

NewORM returns a new gorm DB instance. Create a dialector by calling e.g. https://pkg.go.dev/gorm.io/driver/mysql#New

Example (Standard)
package main

import (
	"context"

	ddGorm "github.com/coopnorge/go-datadog-lib/v2/middleware/gorm"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type User struct{}

func main() {
	ctx := context.Background()

	dsn := "example.com/users"
	gormDB, err := ddGorm.NewORM(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic(err)
	}

	user := &User{}
	tx := gormDB.WithContext(ctx).Select("*").First(user)

	println(tx)
}
Example (WithDriver)
package main

import (
	"context"

	ddDatabase "github.com/coopnorge/go-datadog-lib/v2/middleware/database"
	ddGorm "github.com/coopnorge/go-datadog-lib/v2/middleware/gorm"

	mysqlDriver "github.com/go-sql-driver/mysql"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type User struct{}

func main() {
	ctx := context.Background()

	dsn := "example.com/users"
	db, err := ddDatabase.RegisterDriverAndOpen("mysql", mysqlDriver.MySQLDriver{}, dsn)
	if err != nil {
		panic(err)
	}

	gormDB, err := ddGorm.NewORM(mysql.New(mysql.Config{Conn: db}), &gorm.Config{})
	if err != nil {
		panic(err)
	}

	user := &User{}
	tx := gormDB.WithContext(ctx).Select("*").First(user)

	println(tx)
}

Types

type Option

type Option func(cfg *config)

Option allows for overriding our default-config.

func WithCustomTag

func WithCustomTag(key string, value interface{}) Option

WithCustomTag will attach the value to the span tagged by the key.

func WithServiceName

func WithServiceName(serviceName string) Option

WithServiceName overrides the service-name set in environment-variable "DD_SERVICE".

Jump to

Keyboard shortcuts

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