database

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDriverAndDSN

func GetDriverAndDSN(dbType string, enableLog bool, dsn string) (driver string, connString string)

GetDriverAndDSN returns the appropriate driver name and DSN for database/sql based on the database type. This is used for goose migrations which require database/sql instead of GORM.

func New

func New(dbType, dbPsn string, enableLog bool) (*gorm.DB, error)

New creates new database connection to the database server

func NewWithConfig

func NewWithConfig(dialect, dbPsn string, cfg *gorm.Config) (db *gorm.DB, err error)

NewWithConfig creates new database connection using custom gorm config.

func Transaction

func Transaction(db *gorm.DB, fn InTransaction) (err error)

Transaction execute the input func in a transaction

Types

type DB

type DB struct {
	// Model must be set to a specific model instance. e.g: model.User{}
	Model interface{}
	// GDB holds previous DB instance that just executed the query
	GDB *gorm.DB
}

DB represents the client for common usages

func NewDB

func NewDB(model interface{}) *DB

NewDB creates new DB instance

func (*DB) Create

func (cdb *DB) Create(ctx context.Context, db *gorm.DB, input interface{}) error

Create creates a new record on database.

func (*DB) CreateInBatches

func (cdb *DB) CreateInBatches(ctx context.Context, db *gorm.DB, input interface{}, batchSize int) error

CreateInBatches creates batch of new record on database.

func (*DB) Delete

func (cdb *DB) Delete(ctx context.Context, db *gorm.DB, cond ...interface{}) error

Delete deletes record matching given conditions.

func (*DB) DeletePermanently

func (cdb *DB) DeletePermanently(ctx context.Context, db *gorm.DB, cond ...interface{}) error

DeletePermanently deletes record matching given conditions permanently.

func (*DB) Exist

func (cdb *DB) Exist(ctx context.Context, db *gorm.DB, cond ...interface{}) (bool, error)

Exist checks whether there is record matching the given conditions.

func (*DB) List

func (cdb *DB) List(ctx context.Context, db *gorm.DB, output interface{}, lq *ListQueryCondition, count *int64) error

List returns list of records retrievable after filter & pagination if given.

func (*DB) ParseCond

func (cdb *DB) ParseCond(cond ...interface{}) []interface{}

ParseCond returns standard [sqlString, vars] format for query, powered by gowhere package (configurable version)

func (*DB) Update

func (cdb *DB) Update(ctx context.Context, db *gorm.DB, updates interface{}, cond ...interface{}) error

Update updates data of the records matching the given conditions.

func (*DB) View

func (cdb *DB) View(ctx context.Context, db *gorm.DB, output interface{}, cond ...interface{}) error

View returns single record matching the given conditions.

type InTransaction

type InTransaction func(tx *gorm.DB) error

InTransaction defines the transaction wrapper function

type Intf

type Intf interface {
	// Create creates a new record on database.
	// `input` must be a non-nil pointer of the model. e.g: `input := &model.User{}`
	Create(ctx context.Context, db *gorm.DB, input interface{}) error
	// View returns single record matching the given conditions.
	// `output` must be a non-nil pointer of the model. e.g: `output := new(model.User)`
	// Note: RecordNotFound error is returned when there is no record that matches the conditions
	View(ctx context.Context, db *gorm.DB, output interface{}, cond ...interface{}) error
	// List returns list of records retrievable after filter & pagination if given.
	// `output` must be a non-nil pointer of slice of the model. e.g: `data := []*model.User{}; db.List(dbconn, &data, nil, nil)`
	// `lq` can be nil, then no filter & pagination are applied
	// `count` can also be nil, then no extra query is executed to get the total count
	List(ctx context.Context, db *gorm.DB, output interface{}, lq *ListQueryCondition, count *int64) error
	// Update updates data of the records matching the given conditions.
	// `updates` could be a model struct or map[string]interface{}
	// Note: DB.Model must be provided in order to get the correct model/table
	Update(ctx context.Context, db *gorm.DB, updates interface{}, cond ...interface{}) error
	// Delete deletes record matching given conditions.
	// `cond` can be an instance of the model, then primary key will be used as the condition
	Delete(ctx context.Context, db *gorm.DB, cond ...interface{}) error
	// Exist checks whether there is record matching the given conditions.
	Exist(ctx context.Context, db *gorm.DB, cond ...interface{}) (bool, error)
	// CreateInBatches creates batch of new record on database.
	// `input` must be a array non-nil pointer of the model. e.g: `input := []*model.User`
	CreateInBatches(ctx context.Context, db *gorm.DB, input interface{}, batchSize int) error
	// ParseCond returns standard [sqlString, vars] format for query, powered by gowhere package (with default config)
	ParseCond(cond ...interface{}) []interface{}
	// DeletePermanently deletes record matching given conditions permanently.
	// `cond` can be an instance of the model, then primary key will be used as the condition
	DeletePermanently(ctx context.Context, db *gorm.DB, cond ...interface{}) error
}

Intf represents the common db interface

type ListQueryCondition

type ListQueryCondition struct {
	Filter  *gowhere.Plan
	Sort    []string
	Page    int
	PerPage int
}

ListQueryCondition holds data used for db queries

Jump to

Keyboard shortcuts

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