Documentation
¶
Index ¶
- Constants
- func BindEntity[T any](e *Entity) (result T, err error)
- func ErrFieldNotFound(schemaName, fieldName string) error
- func ErrInvalidFieldValue(fieldName string, value any, errs ...error) error
- func GetSchemasFromDir(dir string, systemSchemaTypes ...any) (map[string]*Schema, error)
- func MergeFields(f1, f2 *Field)
- func NewRelationBackRefError(relation *Relation) error
- func NewRelationNodeError(schema *Schema, field *Field) error
- func StringToFieldValue[T any](field *Field, strValue string) (T, error)
- func UnmarshalJSONValue(data []byte, valueData []byte, dataType jsonparser.ValueType, offset int) (any, error)
- type Builder
- func (b *Builder) AddSchema(schema *Schema)
- func (b *Builder) Clone() *Builder
- func (b *Builder) CreateFKs() error
- func (b *Builder) CreateM2mJunctionSchema(currentSchema *Schema, r *Relation) (*Schema, bool, error)
- func (b *Builder) CreateRelations() (err error)
- func (b *Builder) Dir(dirs ...string) string
- func (b *Builder) Init() (err error)
- func (b *Builder) Relation(name string) *Relation
- func (b *Builder) Relations() []*Relation
- func (b *Builder) ReplaceSchema(name string, schema *Schema)
- func (b *Builder) SaveToDir(dir string) error
- func (b *Builder) Schema(name string) (*Schema, error)
- func (b *Builder) SchemaFile(name string) string
- func (b *Builder) Schemas() []*Schema
- type CustomizableSchema
- type Entity
- func (e *Entity) Delete(name string) *Entity
- func (e *Entity) Empty() bool
- func (e *Entity) First() *orderedmap.Pair[string, any]
- func (e *Entity) Get(name string, defaultValues ...any) any
- func (e *Entity) GetString(name string, defaultValues ...string) string
- func (e *Entity) GetUint64(name string, optional bool) (uint64, error)
- func (e *Entity) ID() uint64
- func (e *Entity) Keys() []string
- func (e *Entity) MarshalJSON() ([]byte, error)
- func (e *Entity) Name(names ...string) string
- func (e *Entity) Set(name string, value any) *Entity
- func (e *Entity) SetID(value any) *Entity
- func (e *Entity) String() string
- func (e *Entity) ToJSON() (string, error)
- func (e *Entity) ToMap() map[string]any
- func (e *Entity) UnmarshalJSON(data []byte) (err error)
- type Field
- type FieldDB
- type FieldEnum
- type FieldPair
- type FieldRenderer
- type FieldType
- type Relation
- func (r *Relation) Clone() *Relation
- func (r *Relation) CreateFKFields() *Field
- func (r *Relation) GetBackRefName() string
- func (r *Relation) GetFKColumns() *RelationFKColumns
- func (r *Relation) GetTargetFKColumn() string
- func (r *Relation) HasFKs() bool
- func (r *Relation) Init(schema *Schema, relationSchema *Schema, f *Field) *Relation
- func (r *Relation) IsBidi() bool
- func (r *Relation) IsSameType() bool
- type RelationFKColumns
- type RelationType
- type Schema
- func (s *Schema) Clone() *Schema
- func (s *Schema) CreateField(sf reflect.StructField) (*Field, error)
- func (s *Schema) CreateFields() error
- func (s *Schema) Customize() (_ *Schema, err error)
- func (s *Schema) ExtendFieldByTag(sf reflect.StructField, field *Field) error
- func (s *Schema) Field(name string) *Field
- func (s *Schema) HasField(fieldName string) bool
- func (s *Schema) Init(disableIDColumn bool) error
- func (s *Schema) SaveToFile(filename string) error
- func (s *Schema) Validate() error
- type SchemaDB
- type SchemaDBIndex
- type SystemSchema
Constants ¶
const FieldCreatedAt = "created_at"
const FieldDeletedAt = "deleted_at"
const FieldID = "id"
const FieldUpdatedAt = "updated_at"
Variables ¶
This section is empty.
Functions ¶
func BindEntity ¶ added in v0.1.0
BindEntity binds the fields of the given Entity to a target struct value.
func ErrFieldNotFound ¶ added in v0.1.0
func ErrInvalidFieldValue ¶ added in v0.1.0
func GetSchemasFromDir ¶
func MergeFields ¶ added in v0.1.0
func MergeFields(f1, f2 *Field)
Merge Fields merge second field to the first field.
func NewRelationBackRefError ¶
func NewRelationNodeError ¶
func StringToFieldValue ¶ added in v0.1.0
StringToFieldValue converts a string value to a specific type based on the provided field.
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 ¶
NewBuilderFromDir creates a new schema builder from a directory.
func NewBuilderFromSchemas ¶ added in v0.0.5
func NewBuilderFromSchemas(dir string, schemas map[string]*Schema, systemSchemaTypes ...any) (*Builder, error)
NewBuilderFromSchemas creates a new schema from a map of schemas.
func (*Builder) CreateM2mJunctionSchema ¶
func (*Builder) CreateRelations ¶
CreateRelations creates all relations between nodes
func (*Builder) Dir ¶
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) ReplaceSchema ¶
ReplaceSchema replaces a schema
func (*Builder) SchemaFile ¶
SchemaFile returns the json file path of a schema
type CustomizableSchema ¶ added in v0.1.0
type CustomizableSchema interface {
Schema() *Schema
}
CustomizableSchema allows the struct to customize the schema.
Method `Schema` should return the customized schema. It supports the following types of customization: - Customize schema information: - Name - Namespace - LabelFieldName - DisableTimestamp - IsJunctionSchema - DB - Customize all field information, except `IsSystemField`: - Adding `Fields` ([]*Field) property to the returned schema to customize the fields. - Each field item contains data to customize schema field (partially or fully). - Matching will be done based on the field name. - Matched field item will be used to customize the corresponding schema field. - Non-matched field item will be ignored.
type Entity ¶
type Entity struct {
// contains filtered or unexported fields
}
Entity represents a single entity.
func NamedEntity ¶ added in v0.1.0
NamedEntity creates a new entity with the given model.
func NewEntityFromJSON ¶
NewEntityFromJSON creates a new entity from a JSON string.
func NewEntityFromMap ¶
NewEntityFromMap creates a new entity from a map.
func (*Entity) First ¶
func (e *Entity) First() *orderedmap.Pair[string, any]
First returns the oldest key/value pair in the entity.
func (*Entity) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface.
func (*Entity) Name ¶ added in v0.1.0
Name returns the model name of the entity. If names is provided, it sets the model name to the first name in the list.
func (*Entity) SetID ¶
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) UnmarshalJSON ¶
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.
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.
// 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.
Renderer *FieldRenderer `json:"renderer,omitempty"` // renderer of the field.
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.
IsSystemField bool `json:"is_system_field,omitempty"` // Is a system field.
}
Field define the data struct for a field
func CreateUint64Field ¶
func (*Field) IsValidValue ¶
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
type FieldPair ¶ added in v0.1.0
While converting struct to schema, we need to convert the struct fields to schema fields.
We will need to know which struct field maps to which schema field. This is done by making pairs of struct field and schema field.
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
func (*FieldRenderer) Clone ¶ added in v0.1.0
func (fr *FieldRenderer) Clone() *FieldRenderer
type FieldType ¶
type FieldType int
FieldType define the data type of a field
func FieldTypeFromReflectType ¶ added in v0.1.0
func FieldTypeFromString ¶ added in v0.1.0
func (FieldType) IsRelationType ¶
func (FieldType) MarshalJSON ¶
MarshalJSON marshal an enum value to the quoted json string value
func (FieldType) StructType ¶ added in v0.1.0
StructType returns the reflect.Type of the field type
func (*FieldType) UnmarshalJSON ¶
UnmarshalJSON unmashals a quoted json string to the enum value
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 *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) CreateFKFields ¶
CreateFKFields create the foreign key fields
func (*Relation) GetBackRefName ¶
GetBackRefName get the back reference name
func (*Relation) GetFKColumns ¶
func (r *Relation) GetFKColumns() *RelationFKColumns
GetFKColumns return the foreign key columns
func (*Relation) GetTargetFKColumn ¶
GetTargetFKColumn return the FK column name for o2m and o2o relation
func (*Relation) IsSameType ¶
IsSameType check if the relation is same type
type RelationFKColumns ¶
type RelationType ¶
type RelationType int
RelationType define the relation type of a field
const ( RelationInvalid RelationType = iota O2O O2M M2M )
func RelationTypeFromString ¶ added in v0.1.0
func RelationTypeFromString(s string) RelationType
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 {
*SystemSchema `json:"-"`
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"`
// contains filtered or unexported fields
}
Schema holds the node data.
func CreateSchema ¶ added in v0.1.0
CreateSchema creates a schema from the given struct.
Schema information:
- Will be created from the struct type: name, namespace.
- Customize through CustomizableSchema interface: Supports all schema properties/fields.
- Customize through specific field "_ any `\"fs:name=schema_name, name\"`": Does not support customizing fields.
Fields information:
- The struct fields will be converted to schema fields.
- The struct field tags will be used to customize the schema fields.
- The struct field conversion will follow the following rules:
- If the field is not exported, it will be ignored.
- If the field is a primitive/time type, it will be mapped arcorrdingly reflectTypesToFieldType.
- If the field is a struct or slice of struct, it must has a field tag define it as a relation.
- If the field is a complex type of primitives, it must has a field tag to define the type as json.
- Enums field must be string with struct tag: fs.enums="[{'value': 'v1', 'label': 'L1'}, {'value': 'v2', 'label': 'L2'}]".
func NewSchemaFromJSON ¶
NewSchemaFromJSON creates a new node from a json string.
func NewSchemaFromJSONFile ¶
NewSchemaFromJSONFile creates a new node from a json file.
func (*Schema) CreateField ¶ added in v0.1.0
func (s *Schema) CreateField(sf reflect.StructField) (*Field, error)
func (*Schema) CreateFields ¶ added in v0.1.0
func (*Schema) ExtendFieldByTag ¶ added in v0.1.0
func (s *Schema) ExtendFieldByTag(sf reflect.StructField, field *Field) error
ExtendFieldByTag extends the given field by parsing the struct field tag and updating the field properties accordingly.
Common properties format:
- E.g: `fs:"type=string,name=custom_name,label=Custom Label,size=10,multiple,unique,optional,sortable,filterable,default=10"`
- Supported field properties:
- type: Tag fs="type=string".
- name: Use json tag to customize the field name, e.g. `json:"custom_name"`.
- label: Tag fs="label=Custom Label".
- size: Tag fs="size=10".
- multiple: Tag fs="multiple".
- unique: Tag fs="unique".
- optional: Tag fs="optional".
- sortable: Tag fs="sortable".
- filterable: Tag fs="filterable".
- default: Tag fs="default=10", if field is time, use RFC3339 format.
Complex properties format:
- E.g: `fs.enums="[{'value': 'v1', 'label': 'L1'}, {'value': 'v2', 'label': 'L2'}]"`
- E.g: `fs.relation="{'type': 'o2m', 'schema': 'post', 'field': 'categories', 'owner': true}"`
- enums: Only for string fields. Tag fs.enums=hjson -> []*FieldEnum
- relation: Tag fs.relation=hjson -> *Relation
- renderer: Tag fs.renderer=hjson -> *Field.Renderer
- db: Tag fs.db=hjson -> *FieldDB
To extend other field properties, you can implement the CustomizableSchema interface.
func (*Schema) SaveToFile ¶
SaveToFile saves the schema to a file.
type SchemaDB ¶
type SchemaDB struct {
Indexes []*SchemaDBIndex `json:"indexes,omitempty"`
}