Documentation
¶
Index ¶
- func BuildJoinCondition(relation *Relation, currentTable, relatedTable string, ...) (string, error)
- func GetJunctionTableName(modelA, modelB string) string
- func IsArrayFieldType(fieldType FieldType) bool
- func ModelNameToTableName(modelName string) string
- func ValidateRelation(relation *Relation, currentModel, relatedModel *Schema) error
- type Field
- type FieldBuilder
- func (fb *FieldBuilder) AutoIncrement() *FieldBuilder
- func (fb *FieldBuilder) Bool() *FieldBuilder
- func (fb *FieldBuilder) Build() Field
- func (fb *FieldBuilder) DateTime() *FieldBuilder
- func (fb *FieldBuilder) DbType(dbType string) *FieldBuilder
- func (fb *FieldBuilder) Default(value any) *FieldBuilder
- func (fb *FieldBuilder) Float() *FieldBuilder
- func (fb *FieldBuilder) Index() *FieldBuilder
- func (fb *FieldBuilder) Int() *FieldBuilder
- func (fb *FieldBuilder) Int64() *FieldBuilder
- func (fb *FieldBuilder) JSON() *FieldBuilder
- func (fb *FieldBuilder) Map(columnName string) *FieldBuilder
- func (fb *FieldBuilder) Nullable() *FieldBuilder
- func (fb *FieldBuilder) PrimaryKey() *FieldBuilder
- func (fb *FieldBuilder) String() *FieldBuilder
- func (fb *FieldBuilder) Unique() *FieldBuilder
- type FieldType
- type Index
- type Relation
- type RelationMetadata
- type RelationType
- type Schema
- func (s *Schema) AddField(field Field) *Schema
- func (s *Schema) AddIndex(index Index) *Schema
- func (s *Schema) AddRelation(name string, relation Relation) *Schema
- func (s *Schema) GetColumnNameByFieldName(fieldName string) (string, error)
- func (s *Schema) GetField(name string) (*Field, error)
- func (s *Schema) GetFieldByColumnName(columnName string) (*Field, error)
- func (s *Schema) GetFieldByName(name string) *Field
- func (s *Schema) GetFieldNameByColumnName(columnName string) (string, error)
- func (s *Schema) GetPrimaryKey() (*Field, error)
- func (s *Schema) GetRelation(relationName string) (Relation, error)
- func (s *Schema) GetRelationByFieldName(fieldName string) (*Relation, error)
- func (s *Schema) GetRelationsToModel(modelName string) []Relation
- func (s *Schema) GetTableName() string
- func (s *Schema) HasRelation(relationName string) bool
- func (s *Schema) MapColumnDataToSchema(data map[string]any) (map[string]any, error)
- func (s *Schema) MapColumnNamesToFields(columnNames []string) ([]string, error)
- func (s *Schema) MapFieldNamesToColumns(fieldNames []string) ([]string, error)
- func (s *Schema) MapSchemaDataToColumns(data map[string]any) (map[string]any, error)
- func (s *Schema) Validate() error
- func (s *Schema) ValidateRelations(schemas map[string]*Schema) error
- func (s *Schema) WithCompositeKey(fields []string) *Schema
- func (s *Schema) WithTableName(name string) *Schema
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildJoinCondition ¶
func BuildJoinCondition(relation *Relation, currentTable, relatedTable string, currentSchema, relatedSchema *Schema) (string, error)
BuildJoinCondition builds the SQL join condition for a relation
func GetJunctionTableName ¶
GetJunctionTableName generates a junction table name for many-to-many relations
func IsArrayFieldType ¶
IsArrayFieldType checks if a field type represents an array
func ModelNameToTableName ¶
ModelNameToTableName converts model name to default table name (pluralized, snake_case)
func ValidateRelation ¶
ValidateRelation validates a relation definition
Types ¶
type Field ¶
type Field struct {
Name string
Type FieldType
PrimaryKey bool
AutoIncrement bool
Nullable bool
Unique bool
Default any
Index bool
DbType string // Database-specific type (e.g., "@db.VarChar(255)", "@db.Money")
DbAttributes []string // Additional database attributes
Map string // Column name mapping (@map("column_name"))
}
func (Field) GetColumnName ¶
GetColumnName returns the actual database column name for this field
type FieldBuilder ¶
type FieldBuilder struct {
// contains filtered or unexported fields
}
func NewField ¶
func NewField(name string) *FieldBuilder
func (*FieldBuilder) AutoIncrement ¶
func (fb *FieldBuilder) AutoIncrement() *FieldBuilder
func (*FieldBuilder) Bool ¶
func (fb *FieldBuilder) Bool() *FieldBuilder
func (*FieldBuilder) Build ¶
func (fb *FieldBuilder) Build() Field
func (*FieldBuilder) DateTime ¶
func (fb *FieldBuilder) DateTime() *FieldBuilder
func (*FieldBuilder) DbType ¶
func (fb *FieldBuilder) DbType(dbType string) *FieldBuilder
func (*FieldBuilder) Default ¶
func (fb *FieldBuilder) Default(value any) *FieldBuilder
func (*FieldBuilder) Float ¶
func (fb *FieldBuilder) Float() *FieldBuilder
func (*FieldBuilder) Index ¶
func (fb *FieldBuilder) Index() *FieldBuilder
func (*FieldBuilder) Int ¶
func (fb *FieldBuilder) Int() *FieldBuilder
func (*FieldBuilder) Int64 ¶
func (fb *FieldBuilder) Int64() *FieldBuilder
func (*FieldBuilder) JSON ¶
func (fb *FieldBuilder) JSON() *FieldBuilder
func (*FieldBuilder) Map ¶
func (fb *FieldBuilder) Map(columnName string) *FieldBuilder
func (*FieldBuilder) Nullable ¶
func (fb *FieldBuilder) Nullable() *FieldBuilder
func (*FieldBuilder) PrimaryKey ¶
func (fb *FieldBuilder) PrimaryKey() *FieldBuilder
func (*FieldBuilder) String ¶
func (fb *FieldBuilder) String() *FieldBuilder
func (*FieldBuilder) Unique ¶
func (fb *FieldBuilder) Unique() *FieldBuilder
type FieldType ¶
type FieldType string
const ( FieldTypeString FieldType = "string" FieldTypeInt FieldType = "int" FieldTypeInt64 FieldType = "int64" FieldTypeFloat FieldType = "float" FieldTypeBool FieldType = "bool" FieldTypeDateTime FieldType = "datetime" FieldTypeJSON FieldType = "json" FieldTypeDecimal FieldType = "decimal" // Array types FieldTypeStringArray FieldType = "string[]" FieldTypeIntArray FieldType = "int[]" FieldTypeInt64Array FieldType = "int64[]" FieldTypeFloatArray FieldType = "float[]" FieldTypeBoolArray FieldType = "bool[]" FieldTypeDecimalArray FieldType = "decimal[]" FieldTypeDateTimeArray FieldType = "datetime[]" // MongoDB specific types FieldTypeObjectId FieldType = "objectid" FieldTypeBinary FieldType = "binary" FieldTypeDecimal128 FieldType = "decimal128" FieldTypeTimestamp FieldType = "timestamp" FieldTypeDocument FieldType = "document" // Embedded document FieldTypeArray FieldType = "array" // Generic array )
func FieldTypeFromGo ¶
type RelationMetadata ¶
type RelationMetadata struct {
// For many-to-many relations
ThroughTable string // Junction table name
ThroughFields []string // Fields in junction table
// Inverse relation
InverseField string // Field name in the related model
InverseRelation *Relation
// Query options
OnDelete string // CASCADE, RESTRICT, SET NULL
OnUpdate string // CASCADE, RESTRICT, SET NULL
}
RelationMetadata contains additional metadata for relations
type RelationType ¶
type RelationType string
const ( RelationOneToOne RelationType = "oneToOne" RelationOneToMany RelationType = "oneToMany" RelationManyToOne RelationType = "manyToOne" RelationManyToMany RelationType = "manyToMany" )
type Schema ¶
type Schema struct {
Name string
TableName string
Fields []Field
Relations map[string]Relation
Indexes []Index
CompositeKey []string // Fields that form the composite primary key
}
func (*Schema) GetColumnNameByFieldName ¶
GetColumnNameByFieldName returns the database column name for a given schema field name
func (*Schema) GetFieldByColumnName ¶
GetFieldByColumnName returns a field by its database column name
func (*Schema) GetFieldByName ¶
GetFieldByName returns a field by name (returns nil if not found)
func (*Schema) GetFieldNameByColumnName ¶
GetFieldNameByColumnName returns the schema field name for a given column name
func (*Schema) GetPrimaryKey ¶
func (*Schema) GetRelation ¶
GetRelation returns a relation by name
func (*Schema) GetRelationByFieldName ¶
GetRelationByFieldName finds a relation that uses the specified field
func (*Schema) GetRelationsToModel ¶
GetRelationsToModel returns all relations that point to the specified model
func (*Schema) GetTableName ¶
GetTableName returns the database table name (same as TableName field, but method for consistency)
func (*Schema) HasRelation ¶
HasRelation checks if a relation exists
func (*Schema) MapColumnDataToSchema ¶
MapColumnDataToSchema converts data with database column names to data with schema field names
func (*Schema) MapColumnNamesToFields ¶
MapColumnNamesToFields converts a slice of database column names to schema field names
func (*Schema) MapFieldNamesToColumns ¶
MapFieldNamesToColumns converts a slice of schema field names to database column names
func (*Schema) MapSchemaDataToColumns ¶
MapSchemaDataToColumns converts data with schema field names to data with database column names
func (*Schema) ValidateRelations ¶
ValidateRelations validates all relations in the schema