Documentation
¶
Overview ¶
Package resource provides a generic resource type for interacting with a database.
Index ¶
- type AuthorLoader
- type Count
- type Resource
- func (r *Resource[T]) FindByID(ctx context.Context, id int) (Response[T], error)
- func (r *Resource[T]) WithAuthorLoader(authorField, editorField string, loader AuthorLoader) *Resource[T]
- func (r *Resource[T]) WithCount(tableName string, foreignKey string, clause string) *Resource[T]
- func (r *Resource[T]) WithQuery(callback func(query *gorm.DB, tableName string) *gorm.DB) *Resource[T]deprecated
- func (r *Resource[T]) WithScope(callback func(query *gorm.DB, tableName string) *gorm.DB) *Resource[T]
- func (r *Resource[T]) WithoutDeleted(column string) *Resource[T]
- type Response
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AuthorLoader ¶
AuthorLoader is a function that loads authors for a given list of IDs.
type Count ¶
type Count struct {
// contains filtered or unexported fields
}
Count holds information about a count to be performed on a table.
type Resource ¶
type Resource[T any] struct { // contains filtered or unexported fields }
Resource holds a database connection and provides methods for querying and manipulating data.
func (*Resource[T]) WithAuthorLoader ¶
func (r *Resource[T]) WithAuthorLoader(authorField, editorField string, loader AuthorLoader) *Resource[T]
func (*Resource[T]) WithCount ¶
WithCount adds a sub-count to the response Meta. tableName and foreignKey must be hardcoded literals, never user-provided. clause is raw SQL appended as an additional WHERE condition — treat it as a trusted, hardcoded filter (e.g. "active = 1"), never user input.
func (*Resource[T]) WithScope ¶ added in v1.3.3
func (r *Resource[T]) WithScope(callback func(query *gorm.DB, tableName string) *gorm.DB) *Resource[T]
WithScope applies additional query constraints to the resource. The callback receives a fresh *gorm.DB session and the table name. Constraints are additive — calling WithScope multiple times is safe.
Tenant scoping example (use tenancy.RequireTenantScope for security-critical paths):
resource.New[Vehicle](db).
WithScope(func(q *gorm.DB, _ string) *gorm.DB {
return q.Scopes(tenancy.ScopeByTenant(ctx, "dealer_id"))
})
func (*Resource[T]) WithoutDeleted ¶
WithoutDeleted filters soft-deleted records using the given column name. For standard GORM soft-delete (deleted_at IS NULL), pass "deleted_at".
type Response ¶
Response holds the data and metadata for a response.
func Map ¶ added in v1.3.5
Map transforms a Response[TFrom] into a Response[TTo] by applying fn to the Data field. The Meta map is carried over unchanged.
Use this in repository methods that fetch a DB entity and need to return a domain type without duplicating the meta-preservation boilerplate:
resp, err := resource.New[entities.Locale](db).FindByID(ctx, id)
if err != nil { return resource.Response[Locale]{}, err }
return resource.Map(resp, LocaleFromRow), nil