database

package
v2.12.0 Latest Latest
Warning

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

Go to latest
Published: Aug 7, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MigrationsTableName      = "liquor_migrations"
	MigrationsLocksTableName = "liquor_migrations_locks"
)

Variables

View Source
var Module = fx.Module("liquor-adapter-database", fx.Provide(
	NewConnection,
	NewProvider,
))

Functions

func Init

func Init(ctx context.Context, db *bun.DB) error

Types

type ConnectionOutput added in v2.5.0

type ConnectionOutput struct {
	fx.Out
	DB  *bun.DB
	ODM liquordb.ODM
}

func NewConnection

func NewConnection(cfg *config.Config, logger *zap.Logger) ConnectionOutput

NewConnection creates a new database connection using the Bun ORM. It supports multiple database drivers (SQLite, MySQL, PostgreSQL) based on configuration.

Parameters:

  • cfg: configuration object containing database settings
  • logger: zap logger instance for error reporting

Returns:

  • *bun.DB: configured database connection instance

The function will fatal log if connection fails. Supported drivers:

  • SQLite
  • MySQL
  • PostgreSQL

type Migration

type Migration struct {
	ID         int64 `bun:",pk,type:serial"`
	Name       string
	GroupID    int64
	MigratedAt time.Time `bun:",notnull,nullzero,default:current_timestamp"`
}

type Provider

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

func NewProvider

func NewProvider(db *bun.DB) *Provider

func (*Provider) CreateIndexIfNotExists

func (p *Provider) CreateIndexIfNotExists(n any, name string, colums ...string) error

CreateIndexIfNotExists creates a new database index for the given model if it doesn't already exist. It uses the Bun ORM's schema creation capabilities to generate the index.

Parameters:

  • n: any struct that represents a database model
  • name: the name of the index to be created
  • colums: variadic list of column names to be included in the index

Returns:

  • error: returns nil if successful, otherwise returns the error that occurred

Example:

type User struct {
    ID    int64  `bun:"id,pk,autoincrement"`
    Email string `bun:"email,notnull"`
}

err := provider.CreateIndexIfNotExists(&User{}, "idx_user_email", "email")

func (*Provider) CreateTableIfNotExists

func (p *Provider) CreateTableIfNotExists(n any) error

CreateTableIfNotExists creates a new database table for the given model if it doesn't already exist. It uses the Bun ORM's schema creation capabilities to generate the table structure.

Parameters:

  • n: any struct that represents a database model

Returns:

  • error: returns nil if successful, otherwise returns the error that occurred

Example:

type User struct {
    ID   int64  `bun:"id,pk,autoincrement"`
    Name string `bun:"name,notnull"`
}

err := provider.CreateTableIfNotExists(&User{})

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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