Documentation
¶
Overview ¶
Package gorm is the GORM-backed nexus.Store[T] adapter referenced by crud_store.go's doc comment. It turns any GORM-mapped struct into a production-ready Store the AsCRUD generator can drive directly.
Usage:
import (
"github.com/paulmanoni/nexus"
nxgorm "github.com/paulmanoni/nexus/storage/gorm"
)
var Module = nexus.Module("notes",
nexus.Provide(NewNotesDB),
nexus.AsCRUD[Note](
func(ctx context.Context, db *DB) (nexus.Store[Note], error) {
return nxgorm.New[Note](db.GetDB())
},
),
)
The adapter handles ID assignment (UUID by default), sort whitelisting, pagination, and translates gorm.ErrRecordNotFound to nexus.ErrCRUDNotFound so AsCRUD's HTTP/GraphQL status mapping works without extra wiring.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
Option tweaks Store construction.
func WithID ¶
WithID overrides the reflected ID accessors. Use when T's primary key field isn't named "ID" or isn't a string — the resolver still needs to surface a string identifier on the wire.
func WithIDGenerator ¶
WithIDGenerator overrides the default uuid.NewString. Useful for deterministic IDs in tests or for ULID/snowflake schemes.
func WithSortable ¶
WithSortable restricts and maps allowed Sort field names. Keys are the names callers send in ?sort=; values are the SQL column names. Without this option the Store accepts any field GORM's schema can resolve (struct field name or db column name).
type Store ¶
type Store[T any] struct { // contains filtered or unexported fields }
Store is a generic GORM-backed nexus.Store[T]. Construct one via New and return it from an AsCRUD resolver. A single Store is safe to share across requests — the underlying *gorm.DB handles its own pooling.
func New ¶
New builds a GORM-backed Store[T]. It parses T's schema once to learn the primary-key column and field-name → db-column mapping.
func (*Store[T]) Find ¶
Find loads one row by primary key. Returns nexus.ErrCRUDNotFound when no row matches so AsCRUD maps it to a 404.
func (*Store[T]) Remove ¶
Remove deletes by primary key. Returns nexus.ErrCRUDNotFound when no row matched so AsCRUD's DELETE handler can return a 404 instead of a silent 204.