db

package
v0.0.0-...-3e848aa Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	io.Closer

	// Create creates record.
	Create(obj any) error
	// Update updates record by given conditions.
	Update(obj any, where ...any) error
	// Updates update attributes, values can be map/struct(select all fields include empty value fields).
	Updates(obj, values any, where ...any) error
	// UpdateSingle updates record with single column value.
	UpdateSingle(obj any, col string, value any, where ...any) error
	// Delete deletes record by given conditions.
	Delete(obj any, where ...any) error
	// Get gets record that match given conditions.
	Get(out any, where ...any) error
	// Find finds records that match given conditions.
	Find(out any, where ...any) error
	// FindForPaging finds records that match given conditions with offset and limit.
	FindForPaging(out any, offset, limit int, order string, query string, where ...any) error
	// Count returns how many records for a model based on given query conditions.
	Count(model any, query string, where ...any) (count int64, err error)
	// Exist checks record if exist by given conditions.
	Exist(out any, where ...any) (bool, error)
	// Transaction does some db operators in transaction.
	Transaction(fc func(tx DB) error) error
	// ExecRaw executes raw sql.
	ExecRaw(out any, query string, args ...any) error
	// RawDB returns raw gorm db.
	RawDB() *gorm.DB
	// Migrator returns data migrator.
	Migrator() gorm.Migrator
}

DB represents a db wrapper based on gorm. Support: sqlit/mysql/postgres.

func NewDB

func NewDB(cfg *config.Database) (DB, error)

NewDB creates a DB instance.

type Migration

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

Migration represents migration information include table and init records.

func NewMigration

func NewMigration(table any) *Migration

NewMigration creates a data migration.

func (*Migration) AddInitRecord

func (m *Migration) AddInitRecord(value any, where ...any)

AddInitRecord adds init record.

type Migrator

type Migrator interface {
	// AddMigration adds migration data.
	AddMigration(migration *Migration)
	// Run runs data migration include table and data.
	Run() error
}

Migrator represents data migrator interface.

func NewMigrator

func NewMigrator(db DB) Migrator

NewMigrator creates a Migrator instance.

Jump to

Keyboard shortcuts

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