schema

package
v0.0.0-...-49190db Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

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

Index

Constants

View Source
const (
	ReservedFieldNameId      = "id"
	ReservedFieldNameCreated = "created"
	ReservedFieldNameUpdated = "updated"
)

reserved internal field names

View Source
const (
	FieldTypeText     string = "text"
	FieldTypeNumber   string = "number"
	FieldTypeBool     string = "bool"
	FieldTypeEmail    string = "email"
	FieldTypeUrl      string = "url"
	FieldTypeDate     string = "date"
	FieldTypeSelect   string = "select"
	FieldTypeJson     string = "json"
	FieldTypeFile     string = "file"
	FieldTypeRelation string = "relation"
	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 FieldTypes

func FieldTypes() []string

FieldTypes returns slice with all supported field types.

func ReservedFieldNames

func ReservedFieldNames() []string

ReservedFieldNames returns slice with reserved/system field names.

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 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 interfaces that defines common methods that every field options 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"`
}

func (FileOptions) Validate

func (o FileOptions) Validate() error

type JsonOptions

type JsonOptions struct {
}

func (JsonOptions) Validate

func (o JsonOptions) Validate() error

type NumberOptions

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

func (NumberOptions) Validate

func (o NumberOptions) Validate() error

type RelationOptions

type RelationOptions struct {
	MaxSelect     int    `form:"maxSelect" json:"maxSelect"`
	CollectionId  string `form:"collectionId" json:"collectionId"`
	CascadeDelete bool   `form:"cascadeDelete" json:"cascadeDelete"`
}

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"`
	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) 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) 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

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

func (UserOptions) Validate

func (o UserOptions) Validate() error

Jump to

Keyboard shortcuts

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