resource

package
v1.3.15 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package resource provides a generic resource type for interacting with a database.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AuthorLoader

type AuthorLoader func(db *gorm.DB, ids []int) ([]any, error)

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 New

func New[T any](db *gorm.DB) *Resource[T]

New creates a new Resource for the given model type.

func (*Resource[T]) FindByID

func (r *Resource[T]) FindByID(ctx context.Context, id int) (Response[T], error)

func (*Resource[T]) WithAuthorLoader

func (r *Resource[T]) WithAuthorLoader(authorField, editorField string, loader AuthorLoader) *Resource[T]

func (*Resource[T]) WithCount

func (r *Resource[T]) WithCount(tableName string, foreignKey string, clause string) *Resource[T]

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]) WithQuery deprecated

func (r *Resource[T]) WithQuery(callback func(query *gorm.DB, tableName string) *gorm.DB) *Resource[T]

WithQuery is an alias for WithScope kept for backwards compatibility.

Deprecated: use WithScope instead.

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

func (r *Resource[T]) WithoutDeleted(column string) *Resource[T]

WithoutDeleted filters soft-deleted records using the given column name. For standard GORM soft-delete (deleted_at IS NULL), pass "deleted_at".

type Response

type Response[T any] struct {
	Data T              `json:"data"`
	Meta map[string]any `json:"meta"`
}

Response holds the data and metadata for a response.

func Map added in v1.3.5

func Map[TFrom, TTo any](r Response[TFrom], fn func(TFrom) TTo) Response[TTo]

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

Jump to

Keyboard shortcuts

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