gorm

package
v0.0.0-...-a1dd794 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2020 License: Apache-2.0 Imports: 16 Imported by: 0

README

GORM

The fantastic ORM library for Golang, aims to be developer friendly.

Overview

  • Full-Featured ORM
  • Associations (Has One, Has Many, Belongs To, Many To Many, Polymorphism, Single-table inheritance)
  • Hooks (Before/After Create/Save/Update/Delete/Find)
  • Eager loading with Preload, Joins
  • Transactions, Nested Transactions, Save Point, RollbackTo to Saved Point
  • Context, Prepared Statment Mode, DryRun Mode
  • Batch Insert, FindInBatches, Find To Map
  • SQL Builder, Upsert, Locking, Optimizer/Index/Comment Hints, NamedArg, Search/Update/Create with SQL Expr
  • Composite Primary Key
  • Auto Migrations
  • Logger
  • Extendable, flexible plugin API: Database Resolver (Multiple Databases, Read/Write Splitting) / Prometheus…
  • Every feature comes with tests
  • Developer Friendly

Getting Started

go mod init github.com/my/repo
go get -u github.com/otel-contrib/instrumentation
package main

import (
    "context"
    "log"
    "time"

    "github.com/otel-contrib/instrumentation/gorm.io/gorm"
)

type user struct {
    gorm.Model
    Name string
}

func main() {
    dsn := "root:password@tcp(127.0.0.1:3306)/test?parseTime=true&loc=Asia%2FShanghai"
    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
    if err != nil {
        log.Fatal(err)
    }

    if err := db.AutoMigrate(&user{}); err != nil {
        log.Fatal(err)
    }
}

Documentation

Overview

Package gorm is the fantastic ORM library for Golang, aims to be developer friendly.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrRecordNotFound record not found error
	ErrRecordNotFound = gorm.ErrRecordNotFound
	// ErrInvalidTransaction invalid transaction when you are trying to `Commit` or `Rollback`
	ErrInvalidTransaction = gorm.ErrInvalidTransaction
	// ErrNotImplemented not implemented
	ErrNotImplemented = gorm.ErrNotImplemented
	// ErrMissingWhereClause missing where clause
	ErrMissingWhereClause = gorm.ErrMissingWhereClause
	// ErrUnsupportedRelation unsupported relations
	ErrUnsupportedRelation = gorm.ErrUnsupportedRelation
	// ErrPrimaryKeyRequired primary keys required
	ErrPrimaryKeyRequired = gorm.ErrPrimaryKeyRequired
	// ErrModelValueRequired model value required
	ErrModelValueRequired = gorm.ErrModelValueRequired
	// ErrInvalidData unsupported data
	ErrInvalidData = gorm.ErrInvalidData
	// ErrUnsupportedDriver unsupported driver
	ErrUnsupportedDriver = gorm.ErrUnsupportedDriver
	// ErrRegistered registered
	ErrRegistered = gorm.ErrRegistered
	// ErrInvalidField invalid field
	ErrInvalidField = gorm.ErrInvalidField
	// ErrEmptySlice empty slice found
	ErrEmptySlice = gorm.ErrEmptySlice
	// ErrDryRunModeUnsupported dry run mode unsupported
	ErrDryRunModeUnsupported = gorm.ErrDryRunModeUnsupported
)

Functions

This section is empty.

Types

type Config

type Config = gorm.Config

Config GORM config

type DB

type DB = gorm.DB

DB GORM DB definition

func Open

func Open(dialector Dialector, config *Config, opts ...Option) (*DB, error)

Open initialize db session based on dialector.

type Dialector

type Dialector = gorm.Dialector

Dialector GORM database dialector.

type Model

type Model = gorm.Model

Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt It may be embedded into your model or you may build your own model without it

type User struct {
  gorm.Model
}

type Option

type Option interface {
	Apply(*config)
}

Option applies a configuration to the given config.

func WithMeterProvider

func WithMeterProvider(mp metric.MeterProvider) Option

WithMeterProvider specifies a meter provider to use for creating a meter. If none is specified, the global provider is used.

func WithOperationName

func WithOperationName(name string) Option

WithOperationName specifies a operation name. If none is specified, the default operation name is used

func WithSpanNameFormatter

func WithSpanNameFormatter(f SpanNameFormatter) Option

WithSpanNameFormatter specifies a formatter to used to format span names. If none is specified, the default SpanNameFormatter is used

func WithTracerProvider

func WithTracerProvider(tp trace.TracerProvider) Option

WithTracerProvider specifies a tracer provider to use for creating a tracer. If none is specified, the global provider is used.

type OptionFunc

type OptionFunc func(c *config)

OptionFunc provides a convenience wrapper for simple Options that can be represented as functions.

func (OptionFunc) Apply

func (o OptionFunc) Apply(c *config)

Apply will apply the option to the config.

type Plugin

type Plugin = gorm.Plugin

Plugin GORM plugin interface

func NewOTelPlugin

func NewOTelPlugin(db *DB, opts ...Option) (Plugin, error)

NewOTelPlugin returns plugin that provides OpenTelemetry tracing and metrics to gorm.

type SpanNameFormatter

type SpanNameFormatter func(operation string, db *DB) string

SpanNameFormatter creates a custom span name from the operation and db object.

Jump to

Keyboard shortcuts

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