db

package
v0.0.0-...-a4f4c6b Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: GPL-3.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DB *gorm.DB

Functions

func Close

func Close()

Close the DB connection and set the DB variable to nil.

func DropDB

func DropDB(dbName string, config Config) (err error)

DropDB deletes the DB with the given name if it exists.

func Open

func Open(config Config) error

Open and initialise the DB global variable and run AutoMigrate for all the registered models.

func RW

func RW(permissionID int) (db *gorm.DB)

RW returns a copy of DB with the appropriate gorm.Session and hooks applied so that the read/write permissions of the permission of the given ID will be reflected in any queries run using the returned gorm.DB. This is achieved by setting the DryRun flag in the gorm.Session/gorm.DB appropriately.

func RegisterEnum

func RegisterEnum(enum Enum)

RegisterEnum will add the given enum to the enums global variable. The enums in this variable will be created at startup by createEnums.

func RegisterExtension

func RegisterExtension(extension string)

RegisterExtension will add the given extension name to the extensions global variable. The extensions in this variable will be created at startup by createExtensions.

func RegisterModel

func RegisterModel(model any)

RegisterModel will find the schema of the given model, wrap this information in a DBModel and add it to the models global mapping that will be passed to AutoMigrate when the DB connection is opened.

func UpdateComputedFieldsForModels

func UpdateComputedFieldsForModels(modelNames []string, pks []any) (err error)

UpdateComputedFieldsForModels will update the computed fields in all rows of the given model names.

func Upsert

func Upsert(value Upsertable) (created bool, err error)

Upsert will upsert the given Upsertable value in the current DB.

Types

type ComputedFieldsModel

type ComputedFieldsModel interface {
	// UpdateComputedFields updates the computed fields for the instance of the ComputedFieldsModel.
	UpdateComputedFields(tx *gorm.DB) (err error)
	// Empty returns a pointer to an empty instance of the ComputedFieldsModel that can be used as the output for
	// gorm.DB.ScanRows for instance.
	Empty() any
	// Order returns the SQL expression that will be used for ordering the entire result set. This is used in
	// UpdateComputedFieldsForModels.
	Order() string
}

ComputedFieldsModel represents a model that has computed fields that should be updated each time an instance of that model is created or updated. This does not cover fields that should only be computed solely on create or update.

type Config

type Config interface {
	DBHost() string
	DBUser() string
	DBPassword() string
	DBName() string
	TestDBName() string
	DBPort() int
	DBSSLMode() string
	DBTimezone() string
	DBPostgresDBName() string
	DBDefaultRWAccess() (config RWConfig)
	DBRWAccessConfigForID(id int) (config RWConfig)
	DBRWAccessForID(id int) (read bool, write bool)
	DBPhaseReadAccess(id int) bool
	DBPhaseWriteAccess(id int) bool
}

type DBModel

type DBModel struct {
	Schema *schema.Schema
	Model  any
}

func GetModel

func GetModel(model any) *DBModel

GetModel will return the DBModel from the models mapping that matches the type name of the given model.

func GetModelByName

func GetModelByName(name string) *DBModel

GetModelByName will return the DBModel from the models mapping that matches the given name. Remember that names should not be prefixed with their package name.

func (*DBModel) ColumnDBNames

func (dbm *DBModel) ColumnDBNames() []string

ColumnDBNames gets all the column names of the DBModel that are not the empty string.

func (*DBModel) ColumnDBNamesExcluding

func (dbm *DBModel) ColumnDBNamesExcluding(names ...string) []string

ColumnDBNamesExcluding gets all the column names of the DBModel that are not the empty string excluding the names given.

type Enum

type Enum interface {
	fmt.Stringer
	driver.Valuer
	// Type returns the name of the type that should be created in the DB.
	Type() string
	// Values returns the possible values of the enum.
	Values() []string
}

Enum represents an enum type that should be created on migration

type RWConfig

type RWConfig interface {
	ID() int
	R() bool
	W() bool
}

type Upsertable

type Upsertable interface {
	OnConflict() clause.OnConflict
	OnCreateOmit() []string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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