dbx

package module
v0.0.0-...-a82d359 Latest Latest
Warning

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

Go to latest
Published: Oct 7, 2025 License: MIT Imports: 16 Imported by: 0

README

dbx

A Go package that provides robust database management functionality with connection pooling, caching, and SQLite support using the Bun ORM.

Features

  • SQLite database support with WAL mode
  • Connection pooling and lifecycle management
  • Database connection caching with automatic cleanup
  • Migration support
  • Built with Bun ORM

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrDBFileNotFound = errors.New("db file not found")

Functions

func CreateDB

func CreateDB(dsn string, opts ...CreateOptFn) (err error)

CreateDB creates a new database specified by the dsn and runs migrations. Provides the following options:

  • CreateWithDriverName(driverName DriverName) - specify the database driver (default: DriverSQLite)
  • CreateWithDbFolder(folder string) - specify the folder to create the SQLite database file in (default: "./data")
  • CreateWithSource(fs embed.FS) - specify the embedded filesystem containing migration files
  • CreateWithSrcFolder(folder string) - specify the folder within the embedded filesystem containing migration files

For SQLite, if the database file already exists, it will not be overwritten. For other databases, ensure that the user has the necessary permissions to create a new database.

func DbFilePath

func DbFilePath(name, dbFolder string) (string, error)

DbFilePath converts a name into a full path to the db including the file extension

func MigrateDB

func MigrateDB(dsn string, opts ...CreateOptFn) (err error)

MigrateDB runs migrations on the db

func OpenDB

func OpenDB(dsn string, opts ...OpenOptFn) (*bun.DB, error)

OpenDB opens a new database connection. for sqlite, dsn should be a file name (without extension)

func TableExists

func TableExists(ctx context.Context, db *bun.DB, tableName string) (bool, error)

TableExists checks if a table exists in the database

Types

type Cache

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

func NewCache

func NewCache() *Cache

func (*Cache) Cleanup

func (c *Cache) Cleanup()

func (*Cache) Close

func (c *Cache) Close() error

func (*Cache) Get

func (c *Cache) Get(name string) (db *bun.DB, err error)

func (*Cache) GetOrOpen

func (c *Cache) GetOrOpen(name string, openOptions ...OpenOptFn) (db *bun.DB, err error)

func (*Cache) Has

func (c *Cache) Has(name string) *bun.DB

func (*Cache) Set

func (c *Cache) Set(name string, db *bun.DB) bool

type CreateOptFn

type CreateOptFn func(options *CreateOptions)

func CreateWithDbFolder

func CreateWithDbFolder(nme string) CreateOptFn

func CreateWithDriverName

func CreateWithDriverName(dn DriverName) CreateOptFn

func CreateWithSource

func CreateWithSource(fs embed.FS) CreateOptFn

func CreateWithSrcFolder

func CreateWithSrcFolder(n string) CreateOptFn

type CreateOptions

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

type DriverName

type DriverName string
const (
	DriverSQLite   DriverName = "sqlite"
	DriverPostgres DriverName = "postgres"
	DriverPgx      DriverName = "pgx"
	DriverMySQL    DriverName = "mysql"
	DriverMSSQL    DriverName = "mssql"
)

type ListOptions

type ListOptions struct {
	Where string
	Args  []any
	Limit int
}

type OpenOptFn

type OpenOptFn func(options *Options)

func WithConnMaxLifetime

func WithConnMaxLifetime(d time.Duration) OpenOptFn

func WithDbFolder

func WithDbFolder(nme string) OpenOptFn

func WithDriverName

func WithDriverName(dn DriverName) OpenOptFn

func WithMaxIdleConns

func WithMaxIdleConns(n int) OpenOptFn

func WithMaxOpenConns

func WithMaxOpenConns(n int) OpenOptFn

type Options

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

type Transact

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

func NewTransact

func NewTransact(db *bun.DB) (tx *Transact)

func (*Transact) Commit

func (t *Transact) Commit() error

func (*Transact) Db

func (t *Transact) Db() (db bun.IDB)

func (*Transact) Rollback

func (t *Transact) Rollback() error

func (*Transact) Start

func (t *Transact) Start(ctx context.Context, opt *sql.TxOptions) error

func (*Transact) Transaction

func (t *Transact) Transaction(ctx context.Context, opt *sql.TxOptions, fn TransactFunc) (err error)

type TransactFunc

type TransactFunc func(ctx context.Context) error

Jump to

Keyboard shortcuts

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