schema

package
v0.18.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: MIT Imports: 14 Imported by: 223

Documentation

Overview

Package schema implements custom Schema and SchemaField datatypes for handling the Collection schema definitions.

Index

Constants

View Source
const (
	FieldValueModifierAdd      string = "+"
	FieldValueModifierSubtract string = "-"
)

field value modifiers

View Source
const (
	FieldNameId                     string = "id"
	FieldNameCreated                string = "created"
	FieldNameUpdated                string = "updated"
	FieldNameCollectionId           string = "collectionId"
	FieldNameCollectionName         string = "collectionName"
	FieldNameExpand                 string = "expand"
	FieldNameUsername               string = "username"
	FieldNameEmail                  string = "email"
	FieldNameEmailVisibility        string = "emailVisibility"
	FieldNameVerified               string = "verified"
	FieldNameTokenKey               string = "tokenKey"
	FieldNamePasswordHash           string = "passwordHash"
	FieldNameLastResetSentAt        string = "lastResetSentAt"
	FieldNameLastVerificationSentAt string = "lastVerificationSentAt"
)

commonly used field names

View Source
const (
	FieldTypeText     string = "text"
	FieldTypeNumber   string = "number"
	FieldTypeBool     string = "bool"
	FieldTypeEmail    string = "email"
	FieldTypeUrl      string = "url"
	FieldTypeEditor   string = "editor"
	FieldTypeDate     string = "date"
	FieldTypeSelect   string = "select"
	FieldTypeJson     string = "json"
	FieldTypeFile     string = "file"
	FieldTypeRelation string = "relation"

	// Deprecated: Will be removed in v0.9+
	FieldTypeUser string = "user"
)

All valid field types

Variables

This section is empty.

Functions

func ArraybleFieldTypes

func ArraybleFieldTypes() []string

ArraybleFieldTypes returns slice with all array value supported field types.

func AuthFieldNames added in v0.8.0

func AuthFieldNames() []string

AuthFieldNames returns the reserved "auth" collection auth field names.

func BaseModelFieldNames added in v0.8.0

func BaseModelFieldNames() []string

BaseModelFieldNames returns the field names that all models have (id, created, updated).

func FieldTypes

func FieldTypes() []string

FieldTypes returns slice with all supported field types.

func FieldValueModifiers added in v0.11.0

func FieldValueModifiers() []string

FieldValueModifiers returns a list with all available field modifier tokens.

func SystemFieldNames added in v0.8.0

func SystemFieldNames() []string

SystemFields returns special internal field names that are usually readonly.

Types

type BoolOptions

type BoolOptions struct {
}

func (BoolOptions) Validate

func (o BoolOptions) Validate() error

type DateOptions

type DateOptions struct {
	Min types.DateTime `form:"min" json:"min"`
	Max types.DateTime `form:"max" json:"max"`
}

func (DateOptions) Validate

func (o DateOptions) Validate() error

type EditorOptions added in v0.12.0

type EditorOptions struct {
	// ConvertUrls is usually used to instruct the editor whether to
	// apply url conversion (eg. stripping the domain name in case the
	// urls are using the same domain as the one where the editor is loaded).
	//
	// (see also https://www.tiny.cloud/docs/tinymce/6/url-handling/#convert_urls)
	ConvertUrls bool `form:"convertUrls" json:"convertUrls"`
}

func (EditorOptions) Validate added in v0.12.0

func (o EditorOptions) Validate() error

type EmailOptions

type EmailOptions struct {
	ExceptDomains []string `form:"exceptDomains" json:"exceptDomains"`
	OnlyDomains   []string `form:"onlyDomains" json:"onlyDomains"`
}

func (EmailOptions) Validate

func (o EmailOptions) Validate() error

type FieldOptions

type FieldOptions interface {
	Validate() error
}

FieldOptions defines common interface methods that every field option struct has.

type FileOptions

type FileOptions struct {
	MaxSelect int      `form:"maxSelect" json:"maxSelect"`
	MaxSize   int      `form:"maxSize" json:"maxSize"` // in bytes
	MimeTypes []string `form:"mimeTypes" json:"mimeTypes"`
	Thumbs    []string `form:"thumbs" json:"thumbs"`
	Protected bool     `form:"protected" json:"protected"`
}

func (FileOptions) IsMultiple added in v0.14.0

func (o FileOptions) IsMultiple() bool

IsMultiple implements MultiValuer interface and checks whether the current field options support multiple values.

func (FileOptions) Validate

func (o FileOptions) Validate() error

type JsonOptions

type JsonOptions struct {
}

func (JsonOptions) Validate

func (o JsonOptions) Validate() error

type MultiValuer added in v0.14.0

type MultiValuer interface {
	IsMultiple() bool
}

MultiValuer defines common interface methods that every multi-valued (eg. with MaxSelect) field option struct has.

type NumberOptions

type NumberOptions struct {
	Min       *float64 `form:"min" json:"min"`
	Max       *float64 `form:"max" json:"max"`
	NoDecimal bool     `form:"noDecimal" json:"noDecimal"`
}

func (NumberOptions) Validate

func (o NumberOptions) Validate() error

type RelationOptions

type RelationOptions struct {
	// CollectionId is the id of the related collection.
	CollectionId string `form:"collectionId" json:"collectionId"`

	// CascadeDelete indicates whether the root model should be deleted
	// in case of delete of all linked relations.
	CascadeDelete bool `form:"cascadeDelete" json:"cascadeDelete"`

	// MinSelect indicates the min number of allowed relation records
	// that could be linked to the main model.
	//
	// If nil no limits are applied.
	MinSelect *int `form:"minSelect" json:"minSelect"`

	// MaxSelect indicates the max number of allowed relation records
	// that could be linked to the main model.
	//
	// If nil no limits are applied.
	MaxSelect *int `form:"maxSelect" json:"maxSelect"`

	// Deprecated: This field is no-op and will be removed in future versions.
	// Instead use the individula SchemaField.Presentable option for each field in the relation collection.
	DisplayFields []string `form:"displayFields" json:"displayFields"`
}

func (RelationOptions) IsMultiple added in v0.14.0

func (o RelationOptions) IsMultiple() bool

IsMultiple implements MultiValuer interface and checks whether the current field options support multiple values.

func (RelationOptions) Validate

func (o RelationOptions) Validate() error

type Schema

type Schema struct {
	// contains filtered or unexported fields
}

Schema defines a dynamic db schema as a slice of `SchemaField`s.

func NewSchema

func NewSchema(fields ...*SchemaField) Schema

NewSchema creates a new Schema instance with the provided fields.

func (*Schema) AddField

func (s *Schema) AddField(newField *SchemaField)

AddField registers the provided newField to the current schema.

If field with `newField.Id` already exist, the existing field is replaced with the new one.

Otherwise the new field is appended to the other schema fields.

func (*Schema) AsMap

func (s *Schema) AsMap() map[string]*SchemaField

AsMap returns a map with all registered schema field. The returned map is indexed with each field name.

func (*Schema) Clone

func (s *Schema) Clone() (*Schema, error)

Clone creates a deep clone of the current schema.

func (*Schema) Fields

func (s *Schema) Fields() []*SchemaField

Fields returns the registered schema fields.

func (*Schema) GetFieldById

func (s *Schema) GetFieldById(id string) *SchemaField

GetFieldById returns a single field by its id.

func (*Schema) GetFieldByName

func (s *Schema) GetFieldByName(name string) *SchemaField

GetFieldByName returns a single field by its name.

func (*Schema) InitFieldsOptions

func (s *Schema) InitFieldsOptions() error

InitFieldsOptions calls `InitOptions()` for all schema fields.

func (Schema) MarshalJSON

func (s Schema) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Schema) RemoveField

func (s *Schema) RemoveField(id string)

RemoveField removes a single schema field by its id.

This method does nothing if field with `id` doesn't exist.

func (*Schema) Scan

func (s *Schema) Scan(value any) error

Scan implements [sql.Scanner] interface to scan the provided value into the current Schema instance.

func (*Schema) UnmarshalJSON

func (s *Schema) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

On success, all schema field options are auto initialized.

func (Schema) Validate

func (s Schema) Validate() error

Validate makes Schema validatable by implementing validation.Validatable interface.

Internally calls each individual field's validator and additionally checks for invalid renamed fields and field name duplications.

func (Schema) Value

func (s Schema) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type SchemaField

type SchemaField struct {
	System   bool   `form:"system" json:"system"`
	Id       string `form:"id" json:"id"`
	Name     string `form:"name" json:"name"`
	Type     string `form:"type" json:"type"`
	Required bool   `form:"required" json:"required"`

	// Presentable indicates whether the field is suitable for
	// visualization purposes (eg. in the Admin UI relation views).
	Presentable bool `form:"presentable" json:"presentable"`

	// Deprecated: This field is no-op and will be removed in future versions.
	// Please use the collection.Indexes field to define a unique constraint.
	Unique bool `form:"unique" json:"unique"`

	Options any `form:"options" json:"options"`
}

SchemaField defines a single schema field structure.

func (*SchemaField) ColDefinition

func (f *SchemaField) ColDefinition() string

ColDefinition returns the field db column type definition as string.

func (*SchemaField) InitOptions

func (f *SchemaField) InitOptions() error

InitOptions initializes the current field options based on its type.

Returns error on unknown field type.

func (SchemaField) MarshalJSON

func (f SchemaField) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*SchemaField) PrepareValue

func (f *SchemaField) PrepareValue(value any) any

PrepareValue returns normalized and properly formatted field value.

func (*SchemaField) PrepareValueWithModifier added in v0.11.0

func (f *SchemaField) PrepareValueWithModifier(baseValue any, modifier string, modifierValue any) any

PrepareValueWithModifier returns normalized and properly formatted field value by "merging" baseValue with the modifierValue based on the specified modifier (+ or -).

func (SchemaField) String

func (f SchemaField) String() string

String serializes and returns the current field as string.

func (*SchemaField) UnmarshalJSON

func (f *SchemaField) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

The schema field options are auto initialized on success.

func (SchemaField) Validate

func (f SchemaField) Validate() error

Validate makes `SchemaField` validatable by implementing validation.Validatable interface.

type SelectOptions

type SelectOptions struct {
	MaxSelect int      `form:"maxSelect" json:"maxSelect"`
	Values    []string `form:"values" json:"values"`
}

func (SelectOptions) IsMultiple added in v0.14.0

func (o SelectOptions) IsMultiple() bool

IsMultiple implements MultiValuer interface and checks whether the current field options support multiple values.

func (SelectOptions) Validate

func (o SelectOptions) Validate() error

type TextOptions

type TextOptions struct {
	Min     *int   `form:"min" json:"min"`
	Max     *int   `form:"max" json:"max"`
	Pattern string `form:"pattern" json:"pattern"`
}

func (TextOptions) Validate

func (o TextOptions) Validate() error

type UrlOptions

type UrlOptions struct {
	ExceptDomains []string `form:"exceptDomains" json:"exceptDomains"`
	OnlyDomains   []string `form:"onlyDomains" json:"onlyDomains"`
}

func (UrlOptions) Validate

func (o UrlOptions) Validate() error

type UserOptions deprecated

type UserOptions struct {
	MaxSelect     int  `form:"maxSelect" json:"maxSelect"`
	CascadeDelete bool `form:"cascadeDelete" json:"cascadeDelete"`
}

Deprecated: Will be removed in v0.9+

func (UserOptions) Validate deprecated

func (o UserOptions) Validate() error

Deprecated: Will be removed in v0.9+

Jump to

Keyboard shortcuts

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