Documentation
¶
Index ¶
- 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)
- 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 Field
- type FieldDB
- type FieldEnum
- type FieldPair
- type FieldRenderer
- type FieldType
- type GetterArgs
- type GetterProgram
- type Relation
- func (r *Relation) Clone() *Relation
- func (r *Relation) CreateFKFields() (*Field, error)
- 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) ApplyGetters(ctx context.Context, e *entity.Entity, configs ...expr.Config) error
- func (s *Schema) ApplySetters(ctx context.Context, e *entity.Entity, configs ...expr.Config) error
- 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 SetterArgs
- type SetterProgram
- type SystemSchema
- type Undefined
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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)
MergeFields merge second field to the first field.
func NewRelationBackRefError ¶
func NewRelationNodeError ¶
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 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.
Immutable bool `json:"immutable,omitempty"` // immutable field.
Setter string `json:"setter,omitempty"` // setter expression.
Getter string `json:"getter,omitempty"` // getter expression.
// 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.
// SystemField is field created from Go code, not from user schema.
IsSystemField bool `json:"is_system_field,omitempty"` // Is a system field.
// Lock flag to prevent the field from being modified from API.
IsLocked bool `json:"is_locked,omitempty"` // Is a locked field.
// contains filtered or unexported fields
}
Field define the data struct for a field
func CreateUint64Field ¶
func (*Field) Init ¶
Init initializes the field. schemaNames is the current schema that the field belongs to. schemaNames is required for file field.
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 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 GetterArgs ¶ added in v0.6.0
type GetterProgram ¶ added in v0.6.0
type GetterProgram = expr.Program[GetterArgs, any]
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
}
Relation 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) ApplyGetters ¶ added in v0.6.0
ApplyGetters applies the getter programs to the given entity.
func (*Schema) ApplySetters ¶ added in v0.6.0
ApplySetters applies the setter programs to the given entity.
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"`
}
type SchemaDBIndex ¶
type SetterArgs ¶ added in v0.6.0
type SetterProgram ¶ added in v0.6.0
type SetterProgram = expr.Program[SetterArgs, any]