Documentation
¶
Overview ¶
Package gpagorm provides a GORM adapter for the Go Persistence API (GPA)
Package gpagorm provides a GORM adapter for the Go Persistence API (GPA)
Index ¶
- func GetRepository[T any](instanceName ...string) gpa.Repository[T]
- func SupportedDrivers() []string
- type Provider
- func (p *Provider) BeginTx(ctx context.Context, opts *gpa.TxOptions) (interface{}, error)
- func (p *Provider) Close() error
- func (p *Provider) Configure(config gpa.Config) error
- func (p *Provider) DB() interface{}
- func (p *Provider) Health() error
- func (p *Provider) Migrate(models ...interface{}) error
- func (p *Provider) ProviderInfo() gpa.ProviderInfo
- func (p *Provider) RawExec(ctx context.Context, query string, args ...interface{}) (gpa.Result, error)
- func (p *Provider) RawQuery(ctx context.Context, query string, args ...interface{}) (interface{}, error)
- func (p *Provider) SupportedFeatures() []gpa.Feature
- type Repository
- func (r *Repository[T]) Close() error
- func (r *Repository[T]) Count(ctx context.Context, opts ...gpa.QueryOption) (int64, error)
- func (r *Repository[T]) Create(ctx context.Context, entity *T) error
- func (r *Repository[T]) CreateBatch(ctx context.Context, entities []*T) error
- func (r *Repository[T]) CreateIndex(ctx context.Context, fields []string, unique bool) error
- func (r *Repository[T]) CreateTable(ctx context.Context) error
- func (r *Repository[T]) Delete(ctx context.Context, id interface{}) error
- func (r *Repository[T]) DeleteByCondition(ctx context.Context, condition gpa.Condition) error
- func (r *Repository[T]) DropIndex(ctx context.Context, indexName string) error
- func (r *Repository[T]) DropTable(ctx context.Context) error
- func (r *Repository[T]) ExecSQL(ctx context.Context, sql string, args ...interface{}) (gpa.Result, error)
- func (r *Repository[T]) Exists(ctx context.Context, opts ...gpa.QueryOption) (bool, error)
- func (r *Repository[T]) FindAll(ctx context.Context, opts ...gpa.QueryOption) ([]*T, error)
- func (r *Repository[T]) FindByID(ctx context.Context, id interface{}) (*T, error)
- func (r *Repository[T]) FindByIDWithRelations(ctx context.Context, id interface{}, relations []string) (*T, error)
- func (r *Repository[T]) FindBySQL(ctx context.Context, sql string, args []interface{}) ([]*T, error)
- func (r *Repository[T]) FindWithRelations(ctx context.Context, relations []string, opts ...gpa.QueryOption) ([]*T, error)
- func (r *Repository[T]) GetEntityInfo() (*gpa.EntityInfo, error)
- func (r *Repository[T]) GetMigrationStatus(ctx context.Context) (gpa.MigrationStatus, error)
- func (r *Repository[T]) GetTableInfo(ctx context.Context) (gpa.TableInfo, error)
- func (r *Repository[T]) MigrateTable(ctx context.Context) error
- func (r *Repository[T]) Query(ctx context.Context, opts ...gpa.QueryOption) ([]*T, error)
- func (r *Repository[T]) QueryOne(ctx context.Context, opts ...gpa.QueryOption) (*T, error)
- func (r *Repository[T]) RawExec(ctx context.Context, query string, args []interface{}) (gpa.Result, error)
- func (r *Repository[T]) RawQuery(ctx context.Context, query string, args []interface{}) ([]*T, error)
- func (r *Repository[T]) Transaction(ctx context.Context, fn gpa.TransactionFunc[T]) error
- func (r *Repository[T]) Update(ctx context.Context, entity *T) error
- func (r *Repository[T]) UpdatePartial(ctx context.Context, id interface{}, updates map[string]interface{}) error
- type SQLResult
- type Transaction
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetRepository ¶
func GetRepository[T any](instanceName ...string) gpa.Repository[T]
GetRepository returns a type-safe repository for any entity type T If no instanceName provided, uses default instance Usage:
userRepo := gpagorm.GetRepository[User]() // default
userRepo := gpagorm.GetRepository[User]("primary") // named
func SupportedDrivers ¶
func SupportedDrivers() []string
SupportedDrivers returns the list of supported database drivers
Types ¶
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider implements gpa.Provider and gpa.SQLProvider using GORM
func NewProvider ¶
NewProvider creates a new GORM provider instance
func (*Provider) BeginTx ¶ added in v0.1.1
BeginTx starts a transaction with specific isolation level
func (*Provider) DB ¶ added in v0.1.1
func (p *Provider) DB() interface{}
DB returns the underlying database/sql.DB instance
func (*Provider) ProviderInfo ¶
func (p *Provider) ProviderInfo() gpa.ProviderInfo
ProviderInfo returns information about this provider
func (*Provider) RawExec ¶ added in v0.1.1
func (p *Provider) RawExec(ctx context.Context, query string, args ...interface{}) (gpa.Result, error)
RawExec executes raw SQL without returning results
func (*Provider) RawQuery ¶ added in v0.1.1
func (p *Provider) RawQuery(ctx context.Context, query string, args ...interface{}) (interface{}, error)
RawQuery executes raw SQL and returns results
func (*Provider) SupportedFeatures ¶
SupportedFeatures returns the list of supported features
type Repository ¶
type Repository[T any] struct { // contains filtered or unexported fields }
Repository implements type-safe GORM operations using Go generics. Provides compile-time type safety for all CRUD and SQL operations.
func NewRepository ¶
func NewRepository[T any](db *gorm.DB, provider *Provider) *Repository[T]
NewRepository creates a new generic GORM repository for type T. Example: userRepo := NewRepository[User](db, provider)
func (*Repository[T]) Close ¶
func (r *Repository[T]) Close() error
Close closes the repository (no-op for GORM).
func (*Repository[T]) Count ¶
func (r *Repository[T]) Count(ctx context.Context, opts ...gpa.QueryOption) (int64, error)
Count returns the number of entities matching query options.
func (*Repository[T]) Create ¶
func (r *Repository[T]) Create(ctx context.Context, entity *T) error
Create inserts a new entity with compile-time type safety.
func (*Repository[T]) CreateBatch ¶
func (r *Repository[T]) CreateBatch(ctx context.Context, entities []*T) error
CreateBatch inserts multiple entities with compile-time type safety.
func (*Repository[T]) CreateIndex ¶
CreateIndex creates an index on the specified fields.
func (*Repository[T]) CreateTable ¶
func (r *Repository[T]) CreateTable(ctx context.Context) error
CreateTable creates a new table for entity type T.
func (*Repository[T]) Delete ¶
func (r *Repository[T]) Delete(ctx context.Context, id interface{}) error
Delete removes an entity by ID with compile-time type safety.
func (*Repository[T]) DeleteByCondition ¶
DeleteByCondition removes entities matching a condition.
func (*Repository[T]) DropIndex ¶
func (r *Repository[T]) DropIndex(ctx context.Context, indexName string) error
DropIndex removes an index.
func (*Repository[T]) DropTable ¶
func (r *Repository[T]) DropTable(ctx context.Context) error
DropTable drops the table for entity type T.
func (*Repository[T]) ExecSQL ¶
func (r *Repository[T]) ExecSQL(ctx context.Context, sql string, args ...interface{}) (gpa.Result, error)
ExecSQL executes a raw SQL statement.
func (*Repository[T]) Exists ¶
func (r *Repository[T]) Exists(ctx context.Context, opts ...gpa.QueryOption) (bool, error)
Exists checks if any entity matches the query options.
func (*Repository[T]) FindAll ¶
func (r *Repository[T]) FindAll(ctx context.Context, opts ...gpa.QueryOption) ([]*T, error)
FindAll retrieves all entities with compile-time type safety.
func (*Repository[T]) FindByID ¶
func (r *Repository[T]) FindByID(ctx context.Context, id interface{}) (*T, error)
FindByID retrieves a single entity by ID with compile-time type safety.
func (*Repository[T]) FindByIDWithRelations ¶
func (r *Repository[T]) FindByIDWithRelations(ctx context.Context, id interface{}, relations []string) (*T, error)
FindByIDWithRelations retrieves an entity by ID with preloaded relationships.
func (*Repository[T]) FindBySQL ¶
func (r *Repository[T]) FindBySQL(ctx context.Context, sql string, args []interface{}) ([]*T, error)
FindBySQL executes a raw SQL SELECT query with compile-time type safety.
func (*Repository[T]) FindWithRelations ¶
func (r *Repository[T]) FindWithRelations(ctx context.Context, relations []string, opts ...gpa.QueryOption) ([]*T, error)
FindWithRelations retrieves entities with preloaded relationships.
func (*Repository[T]) GetEntityInfo ¶
func (r *Repository[T]) GetEntityInfo() (*gpa.EntityInfo, error)
GetEntityInfo returns metadata about entity type T.
func (*Repository[T]) GetMigrationStatus ¶
func (r *Repository[T]) GetMigrationStatus(ctx context.Context) (gpa.MigrationStatus, error)
GetMigrationStatus returns the current migration status for entity type T.
func (*Repository[T]) GetTableInfo ¶
GetTableInfo returns detailed information about the current table structure.
func (*Repository[T]) MigrateTable ¶
func (r *Repository[T]) MigrateTable(ctx context.Context) error
MigrateTable migrates the table schema for entity type T.
func (*Repository[T]) Query ¶
func (r *Repository[T]) Query(ctx context.Context, opts ...gpa.QueryOption) ([]*T, error)
Query retrieves entities based on query options with compile-time type safety.
func (*Repository[T]) QueryOne ¶
func (r *Repository[T]) QueryOne(ctx context.Context, opts ...gpa.QueryOption) (*T, error)
QueryOne retrieves a single entity based on query options.
func (*Repository[T]) RawExec ¶
func (r *Repository[T]) RawExec(ctx context.Context, query string, args []interface{}) (gpa.Result, error)
RawExec executes a raw SQL statement.
func (*Repository[T]) RawQuery ¶
func (r *Repository[T]) RawQuery(ctx context.Context, query string, args []interface{}) ([]*T, error)
RawQuery executes a raw SQL query with compile-time type safety.
func (*Repository[T]) Transaction ¶
func (r *Repository[T]) Transaction(ctx context.Context, fn gpa.TransactionFunc[T]) error
Transaction executes a function within a transaction with type safety.
func (*Repository[T]) Update ¶
func (r *Repository[T]) Update(ctx context.Context, entity *T) error
Update modifies an existing entity with compile-time type safety.
func (*Repository[T]) UpdatePartial ¶
func (r *Repository[T]) UpdatePartial(ctx context.Context, id interface{}, updates map[string]interface{}) error
UpdatePartial modifies specific fields of an entity.
type SQLResult ¶
type SQLResult struct {
// contains filtered or unexported fields
}
SQLResult implements gpa.Result interface
func (*SQLResult) LastInsertId ¶
LastInsertId returns the last insert ID
func (*SQLResult) RowsAffected ¶
RowsAffected returns the number of rows affected
type Transaction ¶
type Transaction[T any] struct { *Repository[T] }
TransactionG implements gpa.TransactionG using GORM with type safety.
func (*Transaction[T]) Commit ¶
func (t *Transaction[T]) Commit() error
Commit commits the transaction (handled automatically by GORM).
func (*Transaction[T]) Rollback ¶
func (t *Transaction[T]) Rollback() error
Rollback rolls back the transaction (handled automatically by GORM).
func (*Transaction[T]) RollbackToSavepoint ¶
func (t *Transaction[T]) RollbackToSavepoint(name string) error
RollbackToSavepoint rolls back to a previously created savepoint.
func (*Transaction[T]) SetSavepoint ¶
func (t *Transaction[T]) SetSavepoint(name string) error
SetSavepoint creates a savepoint within the transaction.