schema

package
v0.25.3 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package schema defines OpenVaultDB schema modes and record validation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NativePostgresCollectionID added in v0.21.0

func NativePostgresCollectionID(schemaName, relationName string) (string, error)

NativePostgresCollectionID encodes the exact UTF-8 schema and relation name as a length-prefixed tuple. The ID is logical; SQL code must use the two original identifiers separately so the PostgreSQL driver can quote them.

func ParseNativePostgresCollectionID added in v0.21.0

func ParseNativePostgresCollectionID(id string) (schemaName, relationName string, err error)

ParseNativePostgresCollectionID decodes an ID produced by NativePostgresCollectionID. Non-canonical or malformed encodings are refused.

func ValidateRecord

func ValidateRecord(mode Mode, collection string, col *Collection, data map[string]any) error

ValidateRecord validates record data against the collection schema per the database schema mode. col may be nil when no schema is declared for the collection: that is an error in strict mode and a pass otherwise.

Types

type Collection

type Collection struct {
	Fields     map[string]Field `yaml:"fields" json:"fields"`
	References []Reference      `yaml:"references,omitempty" json:"references,omitempty"`
	// Source is present on collections dynamically discovered from a native
	// PostgreSQL read-only mount. Name remains the logical, stable collection ID.
	Source *NativeCollectionSource `yaml:"source,omitempty" json:"source,omitempty"`
}

Collection declares the schema of one collection.

type Decimal added in v0.14.2

type Decimal struct {
	Precision int    `yaml:"precision" json:"precision"`
	Scale     int    `yaml:"scale" json:"scale"`
	Unbounded bool   `yaml:"unbounded,omitempty" json:"unbounded,omitempty"`
	Storage   string `yaml:"storage" json:"storage"`
}

Decimal describes an exact decimal value transported and stored as text. Unbounded means the source does not declare a maximum precision or scale; Precision and Scale are then both zero rather than guessed.

type Field

type Field struct {
	Type     FieldType `yaml:"type" json:"type"`
	Required bool      `yaml:"required,omitempty" json:"required,omitempty"`
	Decimal  *Decimal  `yaml:"decimal,omitempty" json:"decimal,omitempty"`
	// NativeType preserves the source engine type for discovered read-only
	// schemas whose portable FieldType is necessarily broader.
	NativeType string `yaml:"native_type,omitempty" json:"nativeType,omitempty"`
	// PrimaryKey and Nullable are descriptive source metadata. They do not
	// create record-key semantics or authorize writes.
	PrimaryKey bool `yaml:"primary_key,omitempty" json:"primaryKey,omitempty"`
	Nullable   bool `yaml:"nullable,omitempty" json:"nullable,omitempty"`
}

Field declares a single field of a collection schema.

func (Field) Validate added in v0.14.2

func (f Field) Validate() error

type FieldType

type FieldType string

FieldType is the declared type of a field in a collection schema.

const (
	TypeString  FieldType = "string"
	TypeNumber  FieldType = "number"
	TypeDecimal FieldType = "decimal"
	TypeInteger FieldType = "integer"
	TypeBoolean FieldType = "boolean"
	TypeObject  FieldType = "object"
	TypeArray   FieldType = "array"
	TypeAny     FieldType = "any"
)

func (FieldType) Validate

func (t FieldType) Validate() error

Validate returns an error if t is not a known field type.

type Mode

type Mode string

Mode is the schema mode of a logical database.

Schemaless means no required pre-declared schema. It does not mean no schema information: partial and schemaless writes are observed into the inferred schema catalogue (see pkg/inferred).

const (
	// ModeStrict requires a declared schema for every collection before writes;
	// writes are validated and unknown fields are rejected.
	ModeStrict Mode = "strict"
	// ModePartial validates declared fields; undeclared fields are allowed.
	ModePartial Mode = "partial"
	// ModeSchemaless requires no declared schema before writes.
	ModeSchemaless Mode = "schemaless"
)

func Modes

func Modes() []Mode

Modes lists all valid schema modes.

func (Mode) Validate

func (m Mode) Validate() error

Validate returns an error if m is not a known schema mode.

type NativeCollectionSource added in v0.21.0

type NativeCollectionSource struct {
	Schema string `yaml:"schema" json:"schema"`
	Name   string `yaml:"name" json:"name"`
}

NativeCollectionSource names a physical source relation without changing the logical collection ID used in OVDB routes and authorization.

type Reference added in v0.11.0

type Reference struct {
	Field        string   `yaml:"field" json:"field"`
	Fields       []string `yaml:"fields,omitempty" json:"fields,omitempty"`
	Collection   string   `yaml:"collection" json:"collection"`
	TargetField  string   `yaml:"target_field" json:"targetField"`
	TargetFields []string `yaml:"target_fields,omitempty" json:"targetFields,omitempty"`
}

Reference describes a declared field relationship to another collection. It is metadata for discovery and does not enforce referential integrity.

type Schemas

type Schemas struct {
	Collections map[string]Collection `yaml:"collections" json:"collections"`
}

Schemas holds declared collection schemas of a database.

func (*Schemas) Collection

func (s *Schemas) Collection(name string) *Collection

Collection returns the declared schema for the named collection, or nil.

func (*Schemas) Validate

func (s *Schemas) Validate() error

Validate checks declared schemas for structural correctness.

type ValidationError

type ValidationError struct {
	Collection string
	Field      string
	Message    string
}

ValidationError describes a schema-mode validation failure for a write.

func (*ValidationError) Error

func (e *ValidationError) Error() string

Jump to

Keyboard shortcuts

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