Documentation
¶
Overview ¶
Package gpaorm implements the GPA persistence contracts on top of the Lemmego ORM.
The dependency direction is gpaorm -> orm + gpa. The ORM itself never imports GPA, so the translation lives entirely here.
Index ¶
- Constants
- func GetRepository[T any](provider *Provider) gpa.MigratableRepository[T]
- func GetRepositoryByName[T any](instanceName ...string) gpa.MigratableRepository[T]
- type Provider
- func (p *Provider) BeginTx(ctx context.Context, opts *gpa.TxOptions) (any, error)
- func (p *Provider) Close() error
- func (p *Provider) Configure(config gpa.Config) error
- func (p *Provider) DB() any
- func (p *Provider) Health() error
- func (p *Provider) Migrate(models ...any) error
- func (p *Provider) ORM() *orm.DB
- func (p *Provider) ProviderInfo() gpa.ProviderInfo
- func (p *Provider) RawExec(ctx context.Context, query string, args ...any) (gpa.Result, error)
- func (p *Provider) RawQuery(ctx context.Context, query string, args ...any) (any, error)
- func (p *Provider) Repository[T any]() gpa.MigratableRepository[T]
- 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 any) 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, query string, args ...any) (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 any) (*T, error)
- func (r *Repository[T]) FindByIDWithRelations(ctx context.Context, id any, relations []string) (*T, error)
- func (r *Repository[T]) FindBySQL(ctx context.Context, query string, args []any) ([]*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 []any) (gpa.Result, error)
- func (r *Repository[T]) RawQuery(ctx context.Context, query string, args []any) ([]*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 any, updates map[string]any) error
- type Transaction
Constants ¶
const ProviderName = "LemmegoORM"
ProviderName is how this provider identifies itself in the GPA registry. It must stay distinct from every other provider's name: the registry keys on this string, and GORM already occupies "GORM".
const Version = "0.1.0"
Version reported through ProviderInfo.
Variables ¶
This section is empty.
Functions ¶
func GetRepository ¶
func GetRepository[T any](provider *Provider) gpa.MigratableRepository[T]
GetRepository builds a repository from a provider.
func GetRepositoryByName ¶
func GetRepositoryByName[T any](instanceName ...string) gpa.MigratableRepository[T]
GetRepositoryByName resolves a registered provider and builds a repository from it, mirroring the other GPA providers' entry point.
Types ¶
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider adapts an ORM connection to gpa.Provider and gpa.SQLProvider.
It takes an already-open *orm.DB rather than dialling a database itself, so that gpaorm needs no SQL driver dependencies: the application chooses and imports its own driver, exactly as it does for the migration module.
func (*Provider) Configure ¶
Configure applies connection-pool settings. It cannot re-point an existing connection at a different database; construct a new provider for that.
func (*Provider) DB ¶
DB returns the underlying *sql.DB, as the SQLProvider contract specifies. Use ORM() for the typed handle.
func (*Provider) ORM ¶
ORM returns the underlying ORM handle, for callers that want the richer API instead of the GPA contract.
func (*Provider) ProviderInfo ¶
func (p *Provider) ProviderInfo() gpa.ProviderInfo
func (*Provider) Repository ¶
func (p *Provider) Repository[T any]() gpa.MigratableRepository[T]
Repository builds a typed repository. This is a generic method, legal in Go 1.27 on a concrete type; it is not part of any interface, which generic methods cannot satisfy.
func (*Provider) SupportedFeatures ¶
SupportedFeatures lists only what the adapter actually implements. Locking, subqueries and migration are absent on purpose: the corresponding calls return an unsupported error rather than quietly doing nothing.
type Repository ¶
type Repository[T any] struct { // contains filtered or unexported fields }
Repository implements the GPA repository contracts over the Lemmego ORM.
It is a concrete generic type rather than a generic method on some shared interface: Go 1.27 generic methods cannot satisfy an interface, so this is the only shape that can implement gpa.Repository[T].
func NewRepository ¶
func NewRepository[T any](provider *Provider) *Repository[T]
NewRepository builds a repository bound to a provider's connection.
func TxFor ¶
func TxFor[U any, T any](tx *Transaction[T]) *Repository[U]
TxFor derives a repository for another entity type from an existing transaction, which is how one unit of work spans several models despite gpa.Transaction being parameterised by a single type.
func (*Repository[T]) Close ¶
func (r *Repository[T]) Close() error
Close releases the repository. The connection belongs to the provider and is intentionally left open, since repositories are cheap and shared.
func (*Repository[T]) Count ¶
func (r *Repository[T]) Count(ctx context.Context, opts ...gpa.QueryOption) (int64, error)
func (*Repository[T]) Create ¶
func (r *Repository[T]) Create(ctx context.Context, entity *T) error
Create inserts an entity. The ORM runs the Before/AfterCreate hooks itself, since its hook interfaces are signature-identical to GPA's; only Validate is GPA-specific and therefore run here.
func (*Repository[T]) CreateBatch ¶
func (r *Repository[T]) CreateBatch(ctx context.Context, entities []*T) error
func (*Repository[T]) CreateIndex ¶
func (*Repository[T]) CreateTable ¶
func (r *Repository[T]) CreateTable(ctx context.Context) error
CreateTable is deliberately not implemented. The ORM does not generate DDL from struct definitions; schema is owned by the migration module, which is the framework's single source of truth for it.
func (*Repository[T]) Delete ¶
func (r *Repository[T]) Delete(ctx context.Context, id any) error
Delete removes an entity by primary key. The row is loaded first so the delete hooks see a real entity, and so a soft-deletable model is marked rather than removed.
func (*Repository[T]) DeleteByCondition ¶
func (*Repository[T]) DropIndex ¶
func (r *Repository[T]) DropIndex(ctx context.Context, indexName string) error
func (*Repository[T]) Exists ¶
func (r *Repository[T]) Exists(ctx context.Context, opts ...gpa.QueryOption) (bool, error)
func (*Repository[T]) FindAll ¶
func (r *Repository[T]) FindAll(ctx context.Context, opts ...gpa.QueryOption) ([]*T, error)
func (*Repository[T]) FindByID ¶
func (r *Repository[T]) FindByID(ctx context.Context, id any) (*T, error)
func (*Repository[T]) FindByIDWithRelations ¶
func (*Repository[T]) FindWithRelations ¶
func (r *Repository[T]) FindWithRelations(ctx context.Context, relations []string, opts ...gpa.QueryOption) ([]*T, error)
FindWithRelations eager-loads the named relations. Relation names are the struct field names, the same vocabulary gpa.Preload uses.
func (*Repository[T]) GetEntityInfo ¶
func (r *Repository[T]) GetEntityInfo() (*gpa.EntityInfo, error)
GetEntityInfo exposes the ORM's metadata in GPA's shape, relations included.
func (*Repository[T]) GetMigrationStatus ¶
func (r *Repository[T]) GetMigrationStatus(ctx context.Context) (gpa.MigrationStatus, error)
func (*Repository[T]) GetTableInfo ¶
func (*Repository[T]) MigrateTable ¶
func (r *Repository[T]) MigrateTable(ctx context.Context) error
func (*Repository[T]) Query ¶
func (r *Repository[T]) Query(ctx context.Context, opts ...gpa.QueryOption) ([]*T, error)
func (*Repository[T]) QueryOne ¶
func (r *Repository[T]) QueryOne(ctx context.Context, opts ...gpa.QueryOption) (*T, error)
func (*Repository[T]) Transaction ¶
func (r *Repository[T]) Transaction(ctx context.Context, fn gpa.TransactionFunc[T]) error
Transaction runs fn inside a database transaction, committing when it returns nil and rolling back on error or panic.
func (*Repository[T]) UpdatePartial ¶
UpdatePartial writes only the named fields, addressed by primary key.
type Transaction ¶
type Transaction[T any] struct { *Repository[T] // contains filtered or unexported fields }
Transaction adapts an ORM transaction to gpa.Transaction[T].
GPA models a transaction as a per-entity-type repository, while the ORM's transaction is deliberately entity-agnostic so one unit of work can span several models. The bridge is that many Transaction[T] values can share a single *orm.Tx.
func (*Transaction[T]) Commit ¶
func (t *Transaction[T]) Commit() error
Commit is a no-op: the enclosing Transaction commits when fn returns nil. It exists to satisfy gpa.Transaction.
func (*Transaction[T]) Rollback ¶
func (t *Transaction[T]) Rollback() error
Rollback marks the transaction for rollback. The actual rollback happens when control returns to Transaction, so it stays in one place.
func (*Transaction[T]) RollbackToSavepoint ¶
func (t *Transaction[T]) RollbackToSavepoint(name string) error
func (*Transaction[T]) SetSavepoint ¶
func (t *Transaction[T]) SetSavepoint(name string) error
SetSavepoint and RollbackToSavepoint delegate to the ORM, which owns the savepoint SQL and the identifier validation that has to go with it.