Documentation
¶
Index ¶
- Constants
- Variables
- func BuildPostgresConnectionString(config PostgresConfig) string
- func Close(c context.Context) error
- func CreateTestContext(c context.Context, modelsToMigration ...interface{}) context.Context
- func DB(ctx context.Context) *gorm.DB
- func FileWithLineNum() string
- func FromContext(ctx context.Context) *gorm.DB
- func GlobalConnection() *gorm.DB
- func MigrationDown(db *gorm.DB, args []string) error
- func MigrationGenerate(db *gorm.DB, args []string) error
- func MigrationInit(db *gorm.DB, args []string) error
- func MigrationPerform(db *gorm.DB, args []string) error
- func MigrationSeed(db *gorm.DB, args []string) error
- func MigrationVersion(db *gorm.DB, args []string) error
- func NewDB(c context.Context) *gorm.DB
- func NewInMemoryConnection(fileName string, modelToMigrate ...interface{}) *gorm.DB
- func NewPostgresConnection(config PostgresConfig) (*gorm.DB, error)
- func NewSQLiteConnection(config SQLiteConfig, modelToMigrate ...interface{}) (*gorm.DB, error)
- func ToContext(parent context.Context, db *gorm.DB) context.Context
- type AfterFunc
- type Config
- type ConfigFunc
- type CustomDialector
- func (dialect *CustomDialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{})
- func (dialect *CustomDialector) DataTypeOf(field *schema.Field) string
- func (dialect *CustomDialector) DefaultValueOf(field *schema.Field) clause.Expression
- func (dialect *CustomDialector) Explain(sql string, vars ...interface{}) string
- func (dialect *CustomDialector) Initialize(db *gorm.DB) error
- func (dialect *CustomDialector) Migrator(db *gorm.DB) gorm.Migrator
- func (dialect *CustomDialector) Name() string
- func (dialect *CustomDialector) QuoteTo(writer clause.Writer, s string)
- type Driver
- type Logger
- func (l Logger) Error(ctx context.Context, msg string, data ...interface{})
- func (l Logger) Info(ctx context.Context, msg string, data ...interface{})
- func (l *Logger) LogMode(level logger.LogLevel) logger.Interface
- func (l Logger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error)
- func (l Logger) Warn(ctx context.Context, msg string, data ...interface{})
- func (l Logger) WithSourceFields() *golly.Entry
- type Model
- type ModelUUID
- type OrmPlugin
- func (p *OrmPlugin[T]) After(handlers ...AfterFunc) *OrmPlugin[T]
- func (p *OrmPlugin[T]) AfterDeinitialize(app *golly.Application) error
- func (p *OrmPlugin[T]) Commands() []*cobra.Command
- func (p *OrmPlugin[T]) Deinitialize(app *golly.Application) error
- func (p *OrmPlugin[T]) Initialize(app *golly.Application) error
- func (*OrmPlugin[T]) Name() string
- type PostgresConfig
- type SQLiteConfig
- type SchemaMigration
Constants ¶
const (
DefaultConnection = "default"
)
Variables ¶
var (
ErrorDatabaseNotDefined = errors.New("database is required but not defined")
)
Functions ¶
func BuildPostgresConnectionString ¶
func BuildPostgresConnectionString(config PostgresConfig) string
func CreateTestContext ¶
func FileWithLineNum ¶
func FileWithLineNum() string
FileWithLineNum returns the file name and line number of the most recent caller outside specific libraries or test files, optimized for production use.
func GlobalConnection ¶
func MigrationDown ¶
MigrationDown reverts a specific migration by version number.
func MigrationGenerate ¶
MigrationGenerate creates new migration files with up and down SQL templates.
func MigrationInit ¶
MigrationInit initializes the migration folder and creates the schema migrations table.
func MigrationPerform ¶
MigrationPerform applies all missing migrations to bring the database up to date.
func MigrationSeed ¶
MigrationSeed runs a seed script located in the seed.go file.
func MigrationVersion ¶
MigrationVersion prints the current database version and lists any missing migrations.
func NewInMemoryConnection ¶
this is used for testing makes things easier. NewInMemoryConnection creates a new database connection and migrates any passed in model
func NewPostgresConnection ¶
func NewPostgresConnection(config PostgresConfig) (*gorm.DB, error)
func NewSQLiteConnection ¶
func NewSQLiteConnection(config SQLiteConfig, modelToMigrate ...interface{}) (*gorm.DB, error)
this is used for testing makes things easier. NewInMemoryConnection creates a new database connection and migrates any passed in model
Types ¶
type ConfigFunc ¶
type ConfigFunc[T any] func(a *golly.Application) (T, error)
type CustomDialector ¶
CustomDialector wraps the stdlib DB and dynamically generates IAM tokens per connection.
func (*CustomDialector) BindVarTo ¶
func (dialect *CustomDialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{})
func (*CustomDialector) DataTypeOf ¶
func (dialect *CustomDialector) DataTypeOf(field *schema.Field) string
func (*CustomDialector) DefaultValueOf ¶
func (dialect *CustomDialector) DefaultValueOf(field *schema.Field) clause.Expression
func (*CustomDialector) Explain ¶
func (dialect *CustomDialector) Explain(sql string, vars ...interface{}) string
func (*CustomDialector) Initialize ¶
func (dialect *CustomDialector) Initialize(db *gorm.DB) error
func (*CustomDialector) Migrator ¶
func (dialect *CustomDialector) Migrator(db *gorm.DB) gorm.Migrator
func (*CustomDialector) Name ¶
func (dialect *CustomDialector) Name() string
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func (Logger) WithSourceFields ¶
type Model ¶
type Model struct {
ID uint `json:"id" gorm:"primaryKey" faker:"-"`
CreatedAt time.Time `json:"createdAt,omitempty" faker:"-"`
UpdatedAt time.Time `json:"updatedAt,omitempty" faker:"-"`
DeletedAt gorm.DeletedAt `json:"deletedAt,omitempty" faker:"-"`
}
Model default model struct (Can add additional functionality here)
type ModelUUID ¶
type ModelUUID struct {
ID uuid.UUID `gorm:"type:uuid;" json:"id" fake:"-"`
CreatedAt time.Time `json:"createdAt" faker:"-"`
UpdatedAt time.Time `json:"updatedAt" faker:"-"`
DeletedAt gorm.DeletedAt `json:"deletedAt,omitempty" faker:"-"`
}
ModelUUID is a UUID version of model
func NewModelUUID ¶
func NewModelUUID() ModelUUID
func TestModelUUID ¶
func TestModelUUID() ModelUUID
type OrmPlugin ¶
type OrmPlugin[T any] struct { UseGormMigrations bool Database T // Generic configuration, specific to the database driver // contains filtered or unexported fields }
OrmConfig provides a generic configuration wrapper for the ORM plugin.
func NewOrmPlugin ¶
func NewOrmPlugin[T any](config ConfigFunc[T]) *OrmPlugin[T]
NewOrmPlugin creates a new instance of OrmPlugin with default values.
func (*OrmPlugin[T]) After ¶
After allows you to hook on to after the connection is estabilished to enable any gorm specific stuff you want like CTEs etc
func (*OrmPlugin[T]) AfterDeinitialize ¶
func (p *OrmPlugin[T]) AfterDeinitialize(app *golly.Application) error
AfterDeinitialize closes the global database connection.
func (*OrmPlugin[T]) Deinitialize ¶
func (p *OrmPlugin[T]) Deinitialize(app *golly.Application) error
func (*OrmPlugin[T]) Initialize ¶
func (p *OrmPlugin[T]) Initialize(app *golly.Application) error
Initialize sets up the database connection and middleware.
type PostgresConfig ¶
type PostgresConfig struct {
ConnectionString string
ApplicationName string
Host string
Port int
User string
Password string
Database string
SSL bool
URL string // Optional: Use a full database URL if provided.
Logger bool // Disable logger if false.
MaxIdleConns int
MaxOpenConns int
ConnMaxLifetime time.Duration
MaxIdleTime time.Duration
ConnectionTimeout time.Duration
AuthToken string
AuthTokenFnc func() (string, error)
BeforeConnectFnc func(ctx context.Context, config *pgx.ConnConfig) error
}
PostgresConfig defines the configuration required to connect to a PostgreSQL database.