goyave: github.com/System-Glitch/goyave/database Index | Files

package database

import "github.com/System-Glitch/goyave/database"


Package Files

database.go factory.go validation.go

func AddInitializer Uses

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 Uses

func ClearInitializers()

ClearInitializers remove all database connection initializer functions.

func ClearRegisteredModels Uses

func ClearRegisteredModels()

ClearRegisteredModels unregister all models.

func Close Uses

func Close() error

Close the database connections if they exist.

func Conn Uses

func Conn() *gorm.DB

Conn alias for GetConnection.

func GetConnection Uses

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 Uses

func GetRegisteredModels() []interface{}

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

func Migrate Uses

func Migrate()

Migrate migrates all registered models.

func RegisterDialect Uses

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:


func RegisterModel Uses

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() {

type DialectorInitializer Uses

type DialectorInitializer func(dsn string) gorm.Dialector

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

type Factory Uses

type Factory struct {
    // contains filtered or unexported fields

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

func NewFactory Uses

func NewFactory(generator Generator) *Factory

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

func (*Factory) Generate Uses

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.


func (*Factory) Override Uses

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 Uses

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.


type Generator Uses

type Generator func() interface{}

Generator a generator function generates a single record.

type Initializer Uses

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.

Package database imports 11 packages (graph). Updated 2020-10-24. Refresh now. Tools for package owners.