gorm

package
v0.26.2 Latest Latest
Warning

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

Go to latest
Published: May 5, 2026 License: MIT Imports: 10 Imported by: 0

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

type Option[T any] func(*Store[T])

Option tweaks Store construction.

func WithID

func WithID[T any](get func(*T) string, set func(*T, string)) Option[T]

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

func WithIDGenerator[T any](fn func() string) Option[T]

WithIDGenerator overrides the default uuid.NewString. Useful for deterministic IDs in tests or for ULID/snowflake schemes.

func WithSortable

func WithSortable[T any](mapping map[string]string) Option[T]

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

func New[T any](db *gormpkg.DB, opts ...Option[T]) (*Store[T], error)

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

func (s *Store[T]) Find(ctx context.Context, id string) (*T, error)

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

func (s *Store[T]) Remove(ctx context.Context, id string) error

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.

func (*Store[T]) Save

func (s *Store[T]) Save(ctx context.Context, item *T) error

Save upserts by primary key. An empty ID is treated as a create — the Store assigns a fresh ID via newID and writes it back through setID so the caller sees it on the response.

func (*Store[T]) Search

func (s *Store[T]) Search(ctx context.Context, opts nexus.ListOptions) ([]T, int, error)

Search runs a paginated query. Limit is clamped by AsCRUD before this call, but we re-clamp defensively so direct callers don't need to.

Jump to

Keyboard shortcuts

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