database

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GORM

func GORM() *gorm.DB

GORM returns the underlying GORM client

func RegisterMigration

func RegisterMigration(m *Migration)

RegisterMigration adds a migration to the database

func RegisterMigrations

func RegisterMigrations(ms ...*Migration)

RegisterMigrations adds multiple migrations to the database

Types

type BaseModel

type BaseModel struct {
	ID        uuid.UUID `gorm:"primaryKey;type:uuid;default:uuid_generate_v4()"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt gorm.DeletedAt
}

BaseModel provides common fields for all models Models should embed this struct to get ID, timestamps, and soft delete support

func (*BaseModel) BeforeCreate

func (b *BaseModel) BeforeCreate(tx *gorm.DB) (err error)

BeforeCreate hook generates a UUID if not set

type CRUDStore

type CRUDStore[T any] struct {
	// contains filtered or unexported fields
}

CRUDStore provides generic CRUD operations for any model type

func NewCRUDStore

func NewCRUDStore[T any](client *gorm.DB) *CRUDStore[T]

NewCRUDStore creates a new CRUD store for type T

func (*CRUDStore[T]) Create

func (s *CRUDStore[T]) Create(item T) error

Create inserts a new record

func (*CRUDStore[T]) Delete

func (s *CRUDStore[T]) Delete(id string) error

Delete soft-deletes a record by ID

func (*CRUDStore[T]) Get

func (s *CRUDStore[T]) Get(id string, preloads ...string) (*T, error)

Get retrieves a single record by ID with optional preloads

func (*CRUDStore[T]) List

func (s *CRUDStore[T]) List(preloads ...string) ([]T, error)

List retrieves all records with optional preloads

func (*CRUDStore[T]) Update

func (s *CRUDStore[T]) Update(item T) error

Update saves changes to an existing record

type CRUDStoreInterface

type CRUDStoreInterface[T any] interface {
	List(preloads ...string) ([]T, error)
	Get(id string, preloads ...string) (*T, error)
	Create(item T) error
	Update(item T) error
	Delete(id string) error
}

CRUDStoreInterface defines the interface for CRUD operations

type Database

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

Database provides singleton access to GORM

func Get

func Get() *Database

Get returns the singleton database instance

type Migration

type Migration struct {
	Model interface{}
	Name  string
	Deps  []*Migration
}

Migration represents a database table migration with dependencies

type MigrationBuilder

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

MigrationBuilder provides a fluent interface for building migrations

func NewMigrationBuilder

func NewMigrationBuilder() *MigrationBuilder

NewMigrationBuilder creates a new MigrationBuilder instance

func (*MigrationBuilder) Build

func (b *MigrationBuilder) Build() *Migration

Build constructs the final Migration

func (*MigrationBuilder) Deps

func (b *MigrationBuilder) Deps(deps ...*Migration) *MigrationBuilder

Deps sets the dependencies for this migration

func (*MigrationBuilder) Model

func (b *MigrationBuilder) Model(model interface{}) *MigrationBuilder

Model sets the model struct for this migration

func (*MigrationBuilder) Name

func (b *MigrationBuilder) Name(name string) *MigrationBuilder

Name sets the name of this migration

type Polymorphic

type Polymorphic struct {
	OwnerID   uuid.UUID `gorm:"type:uuid;index"`
	OwnerType string    `gorm:"index"`
}

Polymorphic provides fields for polymorphic relationships See: https://gorm.io/docs/polymorphism.html

Usage example:

type ChildModel struct {
    model.Polymorphic `gorm:"embedded"`
    Name string
}

type ParentModel struct {
    Field ChildModel `gorm:"polymorphic:Owner;polymorphicValue:child_model;"`
}

type Seeder

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

Seeder provides a framework for seeding test data

func NewSeeder

func NewSeeder(db *gorm.DB, batchSize int) *Seeder

NewSeeder creates a new Seeder instance

func (*Seeder) Clear

func (s *Seeder) Clear(model any) error

Clear truncates the table for the given model

func (*Seeder) Seed

func (s *Seeder) Seed(records any) error

Seed inserts a slice of records into the database in batches

func (*Seeder) SeedOne

func (s *Seeder) SeedOne(record any) error

SeedOne inserts a single record into the database

Jump to

Keyboard shortcuts

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