dbrepo

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package dbrepo provides opinionated repository helpers on top of dbx.

It is intentionally table-configured instead of proto-schema-driven: SQL migration files remain the database source of truth, while repositories give AI agents a safe CRUD vocabulary for common resource tables.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTenantRequired      = errors.New("dbrepo: tenant is required")
	ErrOwnerRequired       = errors.New("dbrepo: owner subject is required")
	ErrPatchFieldForbidden = errors.New("dbrepo: patch field is forbidden")
)

Functions

This section is empty.

Types

type Page

type Page[T any] struct {
	Items   []T
	Total   int64
	Page    int
	Size    int
	HasMore bool
}

Page is a typed page result.

type Query

type Query struct {
	Page    int
	Size    int
	Filters map[string]any
	Sort    string
	Desc    bool
}

Query is the safe list contract. Filters and sorts are checked against the ResourceConfig allowlists before reaching the DB.

type ResourceConfig

type ResourceConfig struct {
	Resource string
	Table    string

	IDColumn      string
	TenantColumn  string
	OwnerColumn   string
	DeletedColumn string
	CreatedColumn string
	UpdatedColumn string

	TenantScoped bool
	OwnerScoped  bool
	SoftDelete   bool
	Timestamps   bool

	AllowedFilters     []string
	AllowedSorts       []string
	AllowedPatchFields []string
	BlockedPatchFields []string
	MaxPageSize        int
}

ResourceConfig describes Kernel's safe CRUD conventions for one table.

type ResourceRepository

type ResourceRepository[T any] struct {
	// contains filtered or unexported fields
}

ResourceRepository implements tenant-aware, owner-aware, soft-delete-aware CRUD for a conventional resource table. It deliberately fails closed for tenant or owner scoped tables when ctx is missing the required scope metadata.

func MustResourceRepository

func MustResourceRepository[T any](db dbx.DB, cfg ResourceConfig) *ResourceRepository[T]

func NewResourceRepository

func NewResourceRepository[T any](db dbx.DB, cfg ResourceConfig) (*ResourceRepository[T], error)

func (*ResourceRepository[T]) Create

func (r *ResourceRepository[T]) Create(ctx context.Context, row *T) error

func (*ResourceRepository[T]) Delete

func (r *ResourceRepository[T]) Delete(ctx context.Context, id any) error

func (*ResourceRepository[T]) Get

func (r *ResourceRepository[T]) Get(ctx context.Context, id any) (*T, error)

func (*ResourceRepository[T]) List

func (r *ResourceRepository[T]) List(ctx context.Context, q Query) (*Page[T], error)

func (*ResourceRepository[T]) Patch

func (r *ResourceRepository[T]) Patch(ctx context.Context, id any, patch map[string]any) error

Jump to

Keyboard shortcuts

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