schema

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2024 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const FieldCreatedAt = "created_at"
View Source
const FieldDeletedAt = "deleted_at"
View Source
const FieldID = "id"
View Source
const FieldUpdatedAt = "updated_at"

Variables

View Source
var MediaSchema = `` /* 1062-byte string literal not displayed */
View Source
var PermissionSchema = `` /* 823-byte string literal not displayed */
View Source
var RoleSchema = `` /* 1093-byte string literal not displayed */
View Source
var UserSchema = `` /* 1836-byte string literal not displayed */

Functions

func GetSchemasFromDir

func GetSchemasFromDir(dir string) (map[string]*Schema, error)

func NewRelationBackRefError

func NewRelationBackRefError(relation *Relation) error

func NewRelationNodeError

func NewRelationNodeError(schema *Schema, field *Field) error

func UnmarshalJSONValue

func UnmarshalJSONValue(data []byte, valueData []byte, dataType jsonparser.ValueType, offset int) (any, error)

UnmarshalJSONValue converts json bytes to a Go value.

Types

type Builder

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

Builder holds the schema of the database.

func NewBuilderFromDir

func NewBuilderFromDir(dir string) (*Builder, error)

NewBuilderFromDir creates a new schema builder from a directory.

func NewBuilderFromSchemas added in v0.0.5

func NewBuilderFromSchemas(dir string, schemas map[string]*Schema) (*Builder, error)

NewBuilderFromSchemas creates a new schema from a map of schemas.

func (*Builder) AddSchema

func (b *Builder) AddSchema(schema *Schema)

AddSchema adds a new schema

func (*Builder) Clone

func (b *Builder) Clone() *Builder

Clone clones the builder.

func (*Builder) CreateFKs

func (b *Builder) CreateFKs() error

CreateFKs creates all foreign keys for relations

func (*Builder) CreateM2mJunctionSchema

func (b *Builder) CreateM2mJunctionSchema(currentSchema *Schema, r *Relation) (*Schema, bool, error)

func (*Builder) CreateRelations

func (b *Builder) CreateRelations() (err error)

CreateRelations creates all relations between nodes

func (*Builder) Dir

func (b *Builder) Dir(dirs ...string) string

Dir returns the directory of the builder. If dirs is not empty, it will set the dir to the first element of dirs.

func (*Builder) Init

func (b *Builder) Init() (err error)

Init initializes the builder.

func (*Builder) Relation

func (b *Builder) Relation(name string) *Relation

Relation returns a relation by it's name

func (*Builder) Relations

func (b *Builder) Relations() []*Relation

Relations returns all relations

func (*Builder) ReplaceSchema

func (b *Builder) ReplaceSchema(name string, schema *Schema)

ReplaceSchema replaces a schema

func (*Builder) SaveToDir

func (b *Builder) SaveToDir(dir string) error

SaveToDir saves all the schemas to a directory.

func (*Builder) Schema

func (b *Builder) Schema(name string) (*Schema, error)

Schema returns a node by it's name

func (*Builder) SchemaFile

func (b *Builder) SchemaFile(name string) string

SchemaFile returns the json file path of a schema

func (*Builder) Schemas

func (b *Builder) Schemas() []*Schema

Schemas returns all schemas

type Entity

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

Entity represents a single entity.

func NewEntity

func NewEntity(ids ...uint64) *Entity

NewEntity creates a new entity.

func NewEntityFromJSON

func NewEntityFromJSON(jsonData string) (*Entity, error)

NewEntityFromJSON creates a new entity from a JSON string.

func NewEntityFromMap

func NewEntityFromMap(data map[string]any) *Entity

NewEntityFromMap creates a new entity from a map.

func (*Entity) Delete

func (e *Entity) Delete(name string) *Entity

Delete removes a value from the entity.

func (*Entity) Empty

func (e *Entity) Empty() bool

Empty returns true if the entity is empty.

func (*Entity) First

func (e *Entity) First() *orderedmap.Pair[string, any]

First returns the oldest key/value pair in the entity.

func (*Entity) Get

func (e *Entity) Get(name string, defaultValues ...any) any

Get returns a value from the entity.

func (*Entity) GetString

func (e *Entity) GetString(name string, defaultValues ...string) string

GetString returns a string value from the entity.

func (*Entity) GetUint64

func (e *Entity) GetUint64(name string, optional bool) (uint64, error)

GetUint64 returns the foreign key value (uint64) from the entity.

func (*Entity) ID

func (e *Entity) ID() uint64

ID returns the ID of the entity.

func (*Entity) MarshalJSON

func (e *Entity) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Entity) Set

func (e *Entity) Set(name string, value any) *Entity

Set sets a value in the entity.

func (*Entity) SetID

func (e *Entity) SetID(value any) *Entity

SetID sets the ID of the entity. value can be uint64, float64, or a string that can be converted to uint64. if the value is not a valid ID, do nothing. returns the entity.

func (*Entity) ToJSON

func (e *Entity) ToJSON() (string, error)

ToJSON converts the entity to a JSON string.

func (*Entity) ToMap

func (e *Entity) ToMap() map[string]any

ToMap converts the entity to a map.

func (*Entity) UnmarshalJSON

func (e *Entity) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON implements the json.Unmarshaler interface.

type Field

type Field struct {
	Type       FieldType      `json:"type"`
	Name       string         `json:"name"`
	Label      string         `json:"label"`
	IsMultiple bool           `json:"multiple,omitempty"` // Is a multiple field.
	Renderer   *FieldRenderer `json:"renderer,omitempty"` // renderer of the field.
	Size       int64          `json:"size,omitempty"`     // max size parameter for string, blob, etc.
	Unique     bool           `json:"unique,omitempty"`   // column with unique constraint.
	Optional   bool           `json:"optional,omitempty"` // null or not null attribute.
	Default    any            `json:"default,omitempty"`  // default value.
	Enums      []*FieldEnum   `json:"enums,omitempty"`    // enum values.
	Relation   *Relation      `json:"relation,omitempty"` // relation of the field.
	DB         *FieldDB       `json:"db,omitempty"`       // db config for the field.

	// Querier
	Sortable   bool `json:"sortable,omitempty"`   // Has a "sort" option in the tag.
	Filterable bool `json:"filterable,omitempty"` // Has a "filter" option in the tag.

	IsSystemField bool `json:"is_system_field,omitempty"` // Is a system field.
}

Field define the data struct for a field

func CreateUint64Field

func CreateUint64Field(name string) *Field

func (*Field) Clone

func (f *Field) Clone() *Field

Clone returns a copy of the field.

func (*Field) Init

func (f *Field) Init(schemaNames ...string)

Init initializes the field. schemaNames is only required for media field.

func (*Field) IsValidValue

func (f *Field) IsValidValue(value any) bool

IsValidValue returns true if the value is valid for the column

type FieldDB

type FieldDB struct {
	Attr      string `json:"attr,omitempty"`      // extra attributes.
	Collation string `json:"collation,omitempty"` // collation type (utf8mb4_unicode_ci, utf8mb4_general_ci)
	Increment bool   `json:"increment,omitempty"` // auto increment
	Key       string `json:"key,omitempty"`       // key definition (PRI, UNI or MUL).
}

FieldDB define the db config for a field

func (*FieldDB) Clone

func (f *FieldDB) Clone() *FieldDB

type FieldEnum

type FieldEnum struct {
	Value string `json:"value"`
	Label string `json:"label"`
}

FieldEnum define the data struct for an enum field

func (*FieldEnum) Clone

func (f *FieldEnum) Clone() *FieldEnum

type FieldRenderer

type FieldRenderer struct {
	Class    string         `json:"class,omitempty"`    // renderer class name
	Settings map[string]any `json:"settings,omitempty"` // renderer settings.
}

FieldRenderer define the renderer of a field

type FieldType

type FieldType int

FieltType define the data type of a field

const (
	TypeInvalid FieldType = iota
	TypeBool
	TypeTime
	TypeJSON
	TypeUUID
	TypeBytes
	TypeEnum
	TypeString
	TypeText
	TypeInt8
	TypeInt16
	TypeInt32
	TypeInt
	TypeInt64
	TypeUint8
	TypeUint16
	TypeUint32
	TypeUint
	// TypeUintptr
	TypeUint64
	TypeFloat32
	TypeFloat64
	TypeRelation
	TypeMedia
)

func (FieldType) IsRelationType

func (t FieldType) IsRelationType() bool

func (FieldType) MarshalJSON

func (t FieldType) MarshalJSON() ([]byte, error)

MarshalJSON marshal an enum value to the quoted json string value

func (FieldType) String

func (t FieldType) String() string

String returns the string representation of a type.

func (*FieldType) UnmarshalJSON

func (t *FieldType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmashals a quoted json string to the enum value

func (FieldType) Valid

func (t FieldType) Valid() bool

Valid reports if the given type if known type.

type Relation

type Relation struct {
	BackRef    *Relation `json:"-"` // back reference relation
	Name       string    `json:"-"` // relation name: auto generated
	SchemaName string    `json:"-"` // schema name: get from the current schema
	FieldName  string    `json:"-"` // field name: get from the current field

	TargetSchemaName string `json:"schema"`          // target schema name
	TargetFieldName  string `json:"field,omitempty"` // target field name, aka the back reference field name

	Type  RelationType `json:"type"`            // the relation type: o2o, o2m, m2m
	Owner bool         `json:"owner,omitempty"` // the relation owner: true, false
	// FKColumns       []string     `json:"fk_columns"`
	FKColumns       *RelationFKColumns `json:"fk_columns"`
	JunctionTable   string             `json:"junction_table,omitempty"` // junction table name for m2m relation
	Optional        bool               `json:"optional"`
	FKFields        []*Field           `json:"-"`
	RelationSchemas []*Schema          `json:"-"` // for m2m relation
	JunctionSchema  *Schema            `json:"-"` // for m2m relation
}

Reation define the relation structure

func (*Relation) Clone

func (r *Relation) Clone() *Relation

Clone clone the relation

func (*Relation) CreateFKFields

func (r *Relation) CreateFKFields()

CreateFKFields create the foreign key fields

func (*Relation) GetBackRefName

func (r *Relation) GetBackRefName() string

GetBackRefName get the back reference name

func (*Relation) GetFKColumns

func (r *Relation) GetFKColumns() *RelationFKColumns

GetFKColumns return the foreign key columns

func (*Relation) GetTargetFKColumn

func (r *Relation) GetTargetFKColumn() string

GetTargetFKColumn return the FK column name for o2m and o2o relation

func (*Relation) HasFKs

func (r *Relation) HasFKs() bool

HasFKs check if the relation has foreign keys

func (*Relation) Init

func (r *Relation) Init(schema *Schema, relationSchema *Schema, f *Field) *Relation

Init initialize the relation

func (*Relation) IsBidi

func (r *Relation) IsBidi() bool

IsBidi check if the relation is bidirectional

func (*Relation) IsSameType

func (r *Relation) IsSameType() bool

IsSameType check if the relation is same type

type RelationFKColumns

type RelationFKColumns struct {
	CurrentColumn string `json:"current_column"`
	TargetColumn  string `json:"target_column"`
}

type RelationType

type RelationType int

RelationType define the relation type of a field

const (
	RelationInvalid RelationType = iota
	O2O
	O2M
	M2M
)

func (RelationType) IsM2M

func (t RelationType) IsM2M() bool

func (RelationType) IsO2M

func (t RelationType) IsO2M() bool

func (RelationType) IsO2O

func (t RelationType) IsO2O() bool

func (RelationType) MarshalJSON

func (t RelationType) MarshalJSON() ([]byte, error)

MarshalJSON marshal an enum value to the quoted json string value

func (RelationType) String

func (t RelationType) String() string

String returns the string representation of a type.

func (*RelationType) UnmarshalJSON

func (t *RelationType) UnmarshalJSON(b []byte) error

UnmarshalJSON unmashals a quoted json string to the enum value

func (RelationType) Valid

func (t RelationType) Valid() bool

Valid reports if the given type if known type.

type Schema

type Schema struct {
	Name             string    `json:"name"`
	Namespace        string    `json:"namespace"`
	LabelFieldName   string    `json:"label_field"`
	DisableTimestamp bool      `json:"disable_timestamp"`
	Fields           []*Field  `json:"fields"`
	IsSystemSchema   bool      `json:"is_system_schema,omitempty"`
	IsJunctionSchema bool      `json:"is_junction_schema,omitempty"`
	DB               *SchemaDB `json:"db,omitempty"`
	// RelationsFKColumns map[string][]string `json:"-"`
	DBColumns []string `json:"-"`
	// contains filtered or unexported fields
}

Schema holds the node data.

func NewSchemaFromJSON

func NewSchemaFromJSON(jsonData string) (*Schema, error)

NewSchemaFromJSON creates a new node from a json string.

func NewSchemaFromJSONFile

func NewSchemaFromJSONFile(jsonFile string) (*Schema, error)

NewSchemaFromJSONFile creates a new node from a json file.

func (*Schema) Clone

func (s *Schema) Clone() *Schema

Clone returns a copy of the schema.

func (*Schema) Field

func (s *Schema) Field(name string) (*Field, error)

Field return field by it's name

func (*Schema) HasField

func (s *Schema) HasField(fieldName string) bool

HasField checks if the schema has a field.

func (*Schema) Init

func (s *Schema) Init(disableIDColumn bool) error

Init initializes the node.

func (*Schema) SaveToFile

func (s *Schema) SaveToFile(filename string) error

SaveToFile saves the schema to a file.

func (*Schema) Validate

func (s *Schema) Validate() error

Validate inspects the fields of the schema for validation errors.

type SchemaDB

type SchemaDB struct {
	Indexes []*SchemaDBIndex `json:"indexes,omitempty"`
}

type SchemaDBIndex

type SchemaDBIndex struct {
	Name    string   `json:"name,omitempty"`
	Unique  bool     `json:"unique,omitempty"`
	Columns []string `json:"columns,omitempty"`
}

Jump to

Keyboard shortcuts

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