Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddInitializer

func AddInitializer(initializer Initializer)

AddInitializer adds a database connection initializer function. Initializer functions are meant to modify a connection settings at the global scope when it's created.

Initializer functions are called in order, meaning that functions added last can override settings defined by previous ones.

func ClearInitializers

func ClearInitializers()

ClearInitializers remove all database connection initializer functions.

func ClearRegisteredModels

func ClearRegisteredModels()

ClearRegisteredModels unregister all models.

func Close

func Close() error

Close the database connections if they exist.

func Conn

func Conn() *gorm.DB

Conn alias for GetConnection.

func GetConnection

func GetConnection() *gorm.DB

GetConnection returns the global database connection pool. Creates a new connection pool if no connection is available.

The connections will be closed automatically on server shutdown so you don't need to call "Close()" when you're done with the database.

func GetRegisteredModels

func GetRegisteredModels() []interface{}

GetRegisteredModels get the registered models. The returned slice is a copy of the original, so it cannot be modified.

func Migrate

func Migrate()

Migrate migrates all registered models.

func RegisterDialect

func RegisterDialect(name, template string, initializer DialectorInitializer)

RegisterDialect registers a connection string template for the given dialect.

You cannot override a dialect that already exists.

Template format accepts the following placeholders, which will be replaced with the corresponding configuration entries automatically:

- "{username}"
- "{password}"
- "{host}"
- "{port}"
- "{name}"
- "{options}"

Example template for the "mysql" dialect:

{username}:{password}@({host}:{port})/{name}?{options}

func RegisterModel

func RegisterModel(model interface{})

RegisterModel registers a model for auto-migration. When writing a model file, you should always register it in the init() function.

 func init() {
		database.RegisterModel(&MyModel{})
 }

Types

type DialectorInitializer

type DialectorInitializer func(dsn string) gorm.Dialector

DialectorInitializer function initializing a GORM Dialector using the given data source name (DSN).

type Factory

type Factory struct {
	// contains filtered or unexported fields
}

Factory an object used to generate records or seed the database.

func NewFactory

func NewFactory(generator Generator) *Factory

NewFactory create a new Factory. The given generator function will be used to generate records.

func (*Factory) Generate

func (f *Factory) Generate(count int) interface{}

Generate a number of records using the given factory. Returns a slice of the actual type of the generated records, meaning you can type-assert safely.

factory.Generate(5).([]*User)

func (*Factory) Override

func (f *Factory) Override(override interface{}) *Factory

Override set an override model for generated records. Values present in the override model will replace the ones in the generated records. This function expects a struct pointer as parameter. Returns the same instance of `Factory` so this method can be chained.

func (*Factory) Save

func (f *Factory) Save(count int) interface{}

Save generate a number of records using the given factory, insert them in the database and return the inserted records. The returned slice is a slice of the actual type of the generated records, meaning you can type-assert safely.

factory.Save(5).([]*User)

type Generator

type Generator func() interface{}

Generator a generator function generates a single record.

type Initializer

type Initializer func(*gorm.DB)

Initializer is a function meant to modify a connection settings at the global scope when it's created.

Use `db.InstantSet()` and not `db.Set()`, since the latter clones the gorm.DB instance instead of modifying it.

type Paginator

type Paginator struct {
	Records     interface{}
	MaxPage     int64
	Total       int64
	PageSize    int
	CurrentPage int
	// contains filtered or unexported fields
}

Paginator structure containing pagination information and result records. Can be sent to the client directly.

func NewPaginator

func NewPaginator(db *gorm.DB, page, pageSize int, dest interface{}) *Paginator

NewPaginator create a new Paginator.

Given DB transaction can contain clauses already, such as WHERE, if you want to filter results.

articles := []model.Article{}
tx := database.Conn().Where("title LIKE ?", "%"+helper.EscapeLike(search)+"%")
paginator := database.NewPaginator(tx, page, pageSize, &articles)
result := paginator.Find()
if response.HandleDatabaseError(result) {
    response.JSON(http.StatusOK, paginator)
}

func (*Paginator) Find

func (p *Paginator) Find() *gorm.DB

Find requests page information (total records and max page) and executes the transaction. The Paginate struct is updated automatically, as well as the destination slice given in NewPaginate().

Directories

Path Synopsis
dialect