Documentation
¶
Index ¶
- Constants
- Variables
- func Actor(ctx context.Context) string
- func Changes[T Object](ctx context.Context, db Storage, consumer string, after time.Time, ...) async.Awaiter
- func Collect[T Object](seq iter.Seq[T], where func(T) bool) []T
- func Count[T Object](ctx context.Context, db Storage, q Query) (int, error)
- func Create[T Object](ctx context.Context, db Storage, constructor func(obj T) error, ...) (T, error)
- func Delete[T Object](ctx context.Context, db Storage, urn URN) (T, error)
- func Fetch[T Object](ctx context.Context, db Storage, urn URN) (T, error)
- func Insert[T Object](ctx context.Context, db Storage, v T) (T, error)
- func IsConflict(err error) bool
- func IsInvalidTransition(err error) bool
- func IsLockLost(err error) bool
- func IsNotFound(err error) bool
- func New[T Object](tenant, namespace string, funcs ...func(obj T) error) (T, error)
- func Next[T Object](ctx context.Context, db Storage) (uint32, error)
- func Overwrite[T Object](ctx context.Context, db Storage, v T) (T, error)
- func Patch[T Object](ctx context.Context, db Storage, urn URN, patch func(T) error) (T, error)
- func ReadFile(path string, data []byte, v any) error
- func Search[T Object](ctx context.Context, db Storage, q Query) (iter.Seq[T], error)
- func Select[T Object, P any](seq iter.Seq[T], where func(T) (P, bool)) []P
- func ToJSON(v Object) ([]byte, error)
- func UnmarshalYAML(data []byte, v any) error
- func Update[T Object](ctx context.Context, db Storage, v T) (T, error)
- func Upsert[T Object](ctx context.Context, db Storage, v T, patch func(T) error) (T, error)
- func WithActor(ctx context.Context, actor string) context.Context
- type Blob
- type Change
- type Compression
- type Embed
- type Files
- type Indexer
- type Kind
- type Link
- type LinkKind
- type Linker
- type Memory
- type Meta
- type Object
- func FromJSON(c Registry, data []byte) (Object, error)
- func FromYAML(c Registry, data []byte) (Object, error)
- func NewByType(typ reflect.Type, tenant, namespace string) (Object, error)
- func ReadJSON(c Registry, reader io.Reader) (Object, error)
- func ReadYAML(c Registry, reader io.Reader) (Object, error)
- type Options
- type Path
- type Query
- type Registry
- type Storage
- type Store
- func (s *Store) Close() error
- func (s *Store) Delete(ctx context.Context, urn URN) (Object, error)
- func (s *Store) Fetch(ctx context.Context, urn URN) (Object, error)
- func (s *Store) Insert(ctx context.Context, object Object) (Object, error)
- func (s *Store) Recover(ctx context.Context) error
- func (s *Store) Search(ctx context.Context, kind Kind, query Query) (iter.Seq[Object], error)
- func (s *Store) Start(ctx context.Context, deleteResource func(context.Context, URN) error)
- func (s *Store) Update(ctx context.Context, object Object) (Object, error)
- func (s *Store) Upload(ctx context.Context, scope URN, contentType string, data []byte) (*Blob, error)
- type Target
- type Type
- type URN
Constants ¶
const ( UnknownActor = "unknown" SystemActor = "system" )
const MaxSize = 64 << 20
MaxSize is the largest uncompressed Blob payload.
Variables ¶
var ( ErrNotFound = errors.New("storage: document was not found") ErrConflict = errors.New("storage: write conflict") ErrInvalidTransition = errors.New("storage: invalid state transition") ErrDeleting = errors.New("storage: content is deleting") ErrInvalid = errors.New("storage: invalid input") ErrLockLost = errors.New("storage: lock ownership lost") )
var DefaultActions = []string{
"create", "read", "update", "delete", "search", "count", "run", "fail",
}
DefaultActions lists standard permission actions used when Options.Actions is empty.
var (
ErrKindNotFound = errors.New("resource: kind not found")
)
Functions ¶
func Changes ¶ added in v0.9.1
func Changes[T Object](ctx context.Context, db Storage, consumer string, after time.Time, handle func(context.Context, []Change) error) async.Awaiter
Changes starts bounded, durable changes for T with the named persistent consumer. Wait observes its terminal error. The callback is retried until it succeeds or ctx is canceled.
func Collect ¶ added in v0.9.1
Collect drains a search iterator, keeping items for which where returns true. A nil where keeps every item. Use this before running nested queries on SQLite, which only allows one open cursor at a time.
func Create ¶ added in v0.9.1
func Create[T Object](ctx context.Context, db Storage, constructor func(obj T) error, tenant, namespace string) (T, error)
Create creates a new resource and inserts it into the storage.
func Delete ¶ added in v0.9.1
Delete deletes a resource from the storage and returns the deleted object.
func IsConflict ¶ added in v0.9.1
IsConflict returns true if the specified error is a conflict error.
func IsInvalidTransition ¶ added in v0.9.1
IsInvalidTransition checks whether the error is a state transition error.
func IsLockLost ¶ added in v0.9.1
IsLockLost returns true if the specified error reports lost lock ownership.
func IsNotFound ¶ added in v0.9.1
IsNotFound returns true if the specified error is a not found error.
func Next ¶ added in v0.9.1
Next advances the sequence named by T's resource kind and returns its new value.
func Patch ¶ added in v0.9.1
Patch fetches a resource, applies patch, and retries the update with a fresh version when a concurrent change wins, up to ten attempts. The patch callback may therefore run up to ten times and must only mutate the supplied object; it must be side-effect-free and safe to repeat.
func ReadFile ¶ added in v0.9.1
ReadFile decodes a file into v using its extension (.json, .yaml, .yml). When data is nil the file contents are read from path.
func Select ¶ added in v0.9.1
Select drains a search iterator, projecting items for which where returns true.
func ToJSON ¶ added in v0.9.1
ToJSON encodes a resource for storage. Fields use their JSON name unless a store tag overrides it.
func UnmarshalYAML ¶ added in v0.9.1
UnmarshalYAML decodes YAML into v using json struct tags.
Types ¶
type Blob ¶ added in v0.9.1
type Blob struct {
Meta `kind:"blob" json:",inline"`
ContentType string `json:"contentType" form:"ro"`
Size int64 `json:"size" form:"ro"`
ObjectKey string `json:"-" store:"objectKey" form:"-"`
SHA256 string `json:"-" store:"sha256" form:"-"`
StoredSize int64 `json:"-" store:"storedSize" form:"-"`
Compression Compression `json:"-" store:"compression" form:"-"`
// contains filtered or unexported fields
}
Blob is immutable binary content stored outside the resource database. Storage details are retained only in the persisted representation.
type Change ¶ added in v0.9.1
Change describes a durable create, update, or delete mutation. It is valid only for the duration of the Changes callback and must not be retained or modified by the consumer.
type Compression ¶ added in v0.9.1
type Compression string
Compression describes the persisted encoding of a Blob.
const ( CompressionRaw Compression = "raw" CompressionZstd Compression = "zstd" )
type Embed ¶ added in v0.9.1
Embedded represents a generic embedded document for unmarshaling
func (Embed) MarshalJSON ¶ added in v0.9.1
MarshalJSON marshals the JSON from the embedded document
func (*Embed) UnmarshalJSON ¶ added in v0.9.1
UnmarshalJSON unmarshals the JSON into the embedded document
type Files ¶ added in v0.9.1
type Files interface {
fs.FS
Write(context.Context, string, []byte) (string, error)
Delete(context.Context, string) error
}
Files is the object filesystem used for Blob content.
type Indexer ¶ added in v0.9.1
type Indexer interface {
Index() string
}
Indexer represents a resource that provides an index.
type Kind ¶ added in v0.9.1
type Kind string
Kind represents a resource Kind (e.g. "Document", "Sprite")
const ( // KindBlob identifies binary resources. KindBlob Kind = "blob" )
type Link ¶ added in v0.9.1
type Link struct {
Source URN `json:"source"`
Target URN `json:"target"`
Path Path `json:"path"`
Kind LinkKind `json:"kind"`
}
Link is a derived link index entry.
func Links ¶ added in v0.10.0
Links returns links declared by tags or by the object's Linker method.
type LinkKind ¶ added in v0.9.1
type LinkKind uint8
LinkKind distinguishes exclusive ownership from an ordinary dependency.
type Memory ¶ added in v0.9.1
type Memory struct {
// contains filtered or unexported fields
}
Memory is a zero-value in-memory object filesystem for tests.
type Meta ¶ added in v0.9.1
type Meta struct {
ID string `json:"id" form:"-"` // Globally unique identifier (e.g. "9m4e2mr0ui3e8a215n4g")
Kind Kind `json:"kind" form:"-"` // Meta kind (e.g. "deployment")
Tenant string `json:"tenant" form:"-"` // Tenant slug (e.g. "acme")
Namespace string `json:"namespace" form:"-"` // Namespace of the object (e.g. "default")
State string `json:"state,omitempty" form:"-"` // State is the current state of the resource
CreatedBy string `json:"createdBy,omitempty" form:"-"` // CreatedBy is the user who created the resource
CreatedAt int64 `json:"createdAt,omitempty" form:"-"` // CreatedAt is the time when the resource was created
UpdatedBy string `json:"updatedBy,omitempty" form:"-"` // UpdatedBy is the user who last updated the resource
UpdatedAt int64 `json:"updatedAt,omitempty" form:"-"` // UpdatedAt is the time when the resource was last updated
ExpiresAt int64 `json:"expiresAt,omitempty" form:"-"` // ExpiresAt is when the resource becomes eligible for deletion
}
Meta represents a metadata of the object.
type Object ¶
type Object interface {
URN() URN // URN returns the uniform identifier of the object
Status() string // Status returns the current state
Created() (string, time.Time) // Created returns createdBy and createdAt information
Updated() (string, time.Time) // Updated returns updatedBy and updatedAt information
}
Object represents an object in the system.
type Options ¶ added in v0.9.1
type Options struct {
Icon string `json:"icon,omitempty"` // Icon name from https://lucide.dev/icons
Title string `json:"title,omitempty"` // Title of the document (e.g. Person)
Plural string `json:"plural,omitempty"` // Plural name of the document (e.g. People)
Sort string `json:"sort,omitempty"` // Sort field
Search bool `json:"search,omitempty"` // Enable a materialized full-text index where supported
States state.Machine `json:"-"` // Optional lifecycle state machine
Actions []string `json:"actions,omitempty"` // Allowed permission actions for this kind
Workflows []string `json:"workflows,omitempty"` // Built-in workflows to run after saves
}
Options represents the options for a document
type Path ¶ added in v0.9.1
type Path string
Path represents a rendering path for a particular field.
func (Path) Index ¶ added in v0.9.1
Index retrieves the index of the path, if it's a slice. Otherwise, returns -1.
type Query ¶ added in v0.9.1
type Query struct {
Tenant string // Tenant limits results to one tenant.
IDs []string // IDs limits results to the listed resource IDs.
Namespaces []string // Namespaces limits results to the listed namespaces.
States []string // States is a list of states to filter by
Indexes []string // Indexes is a list of indexes to filter by
Filters map[string][]string // Filters is a map of filters to apply
Match string // Match is the full-text search query
SortBy []string // Sort is the set of fields to order by
Offset int // Offset is the number of records to skip
Limit int // Limit is the maximum number of records to return
CreatedBefore time.Time // CreatedBefore filters records created before this time
UpdatedBefore time.Time // UpdatedBefore filters records updated before this time
UpdatedAfter time.Time // UpdatedAfter filters records updated after this time
// Selection unions exact namespaces and their IDs, intersecting all other filters.
// A nil map is unrestricted; an empty map matches nothing. A nil ID slice
// selects the whole namespace; an empty slice selects nothing. No wildcards
// are interpreted. The caller owns the map and slices and must not modify
// them during Search or Count. Tenant and kind retain their existing scope.
Selection map[string][]string
}
Query represents a query to filter records.
func ParseQuery ¶ added in v0.9.1
ParseQuery parses a string query into a Query struct. The query format is structured as a semicolon-separated list of key-value pairs. Example query: "namespace=company;state=active;filter=age:30;match={Name}" - The query is limited to `company` namespace. - Only records with an `active` state will be considered. - A filter is applied to only include records where `age` is `30`. - It matches records containing the person's name from the placeholder `{Name}`.
**namespace**: Specifies the namespaces to filter by. Multiple namespaces can be separated by commas. Example: `namespace=company,person`
**state**: Indicates the states to filter by. Multiple states can be separated by commas. Example: `state=active,inactive`
**filter**: Defines filters to apply. Each filter is specified as `field:value` for equality checks, or just `field` (without colon) for existence checks (non-nil and non-zero). Multiple filters can be separated by commas. Example: `filter=age:30,income:1000` (equality checks) Example: `filter=email` (existence check - matches records where email is set and non-empty)
**match**: A full-text search query. This can include any search terms. Example: `match=software engineer`
type Storage ¶ added in v0.9.1
type Storage interface {
io.Closer
Registry() Registry
Lock(ctx context.Context, name string) (context.Context, context.CancelFunc, error)
Insert(ctx context.Context, v Object) (Object, error)
Update(ctx context.Context, v Object) (Object, error)
Delete(ctx context.Context, urn URN) (Object, error)
Fetch(ctx context.Context, urn URN) (Object, error)
Link(ctx context.Context, source URN) error
Links(ctx context.Context, target URN) ([]Link, error)
Search(ctx context.Context, kind Kind, query Query) (iter.Seq[Object], error)
Count(ctx context.Context, kind Kind, query Query) (int, error)
Changes(ctx context.Context, consumer string, kind Kind, after time.Time, handle func(context.Context, []Change) error) error
Upload(ctx context.Context, scope URN, contentType string, data []byte) (*Blob, error)
Next(ctx context.Context, name string) (uint32, error)
}
Storage represents a storage layer for records.
type Store ¶ added in v0.9.1
type Store struct {
Storage
// contains filtered or unexported fields
}
Store joins resource storage with its file backend.
func (*Store) Delete ¶ added in v0.9.1
Delete removes ordinary resources directly. Deleting a Blob first makes it unreadable, then removes its file and metadata. File failures leave a retryable deleting resource.
func (*Store) Fetch ¶ added in v0.9.1
Fetch retrieves a resource and binds its file backend when needed.
func (*Store) Insert ¶ added in v0.9.1
Insert stores a resource and binds its file backend when needed.
func (*Store) Recover ¶ added in v0.9.1
Recover removes files and metadata for Blobs left in the deleting state.
func (*Store) Search ¶ added in v0.9.1
Search retrieves resources and binds their file backend when needed.
type Target ¶ added in v0.9.1
type Target string
Target identifies a resource and an optional reference.
func ParseTarget ¶ added in v0.9.1
ParseTarget parses a target in the form <URN> or <URN>@<ref>.
func (Target) IsDraft ¶ added in v0.9.1
IsDraft reports whether the target selects the draft version.
func (Target) IsLatest ¶ added in v0.9.1
IsLatest reports whether the target selects the latest version.
func (Target) MarshalJSON ¶ added in v0.9.1
MarshalJSON marshals a valid target as a JSON string.
func (*Target) UnmarshalJSON ¶ added in v0.9.1
UnmarshalJSON validates and unmarshals a target from a JSON string.
type Type ¶ added in v0.9.1
type Type struct {
Kind Kind // Kind of the resource
Type reflect.Type // Type of the resource
SearchPaths []string // JSON paths excluded from full-text search by search:"-".
Options // Options of the resource
// contains filtered or unexported fields
}
Type represents a registration of a resource kind.
func MustRegister ¶ added in v0.9.1
MustRegister registers a resource kind into the specified registry, panicking on error.
type URN ¶ added in v0.9.1
type URN struct {
Tenant string `json:"-" uri:"tenant" binding:"required"` // Tenant slug (e.g. "acme")
Namespace string `json:"-" uri:"namespace" binding:"required"` // Namespace name (e.g. "default")
Kind Kind `json:"-" uri:"kind" binding:"required"` // Object kind (e.g. "namespace")
ID string `json:"-" uri:"id"` // Globally unique identifier
}
URN represents a uniform resource name for accessing resources. Format: urn:tenant:namespace:kind:id
func (URN) MarshalJSON ¶ added in v0.9.1
MarshalJSON marshals the URN to JSON.
func (*URN) UnmarshalJSON ¶ added in v0.9.1
UnmarshalJSON unmarshals the JSON into a URN.