Help us shape the future of Go! Take the 2026 Go Developer Survey

schema

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

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

func GetJunctionTableName(modelA, modelB string) string

GetJunctionTableName generates a junction table name for many-to-many relations

func IsArrayFieldType

func IsArrayFieldType(fieldType FieldType) bool

IsArrayFieldType checks if a field type represents an array

func ModelNameToTableName

func ModelNameToTableName(modelName string) string

ModelNameToTableName converts model name to default table name (pluralized, snake_case)

func ValidateRelation

func ValidateRelation(relation *Relation, currentModel, relatedModel *Schema) error

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

func (f Field) GetColumnName() string

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

func FieldTypeFromGo(t reflect.Type) FieldType

type Index

type Index struct {
	Name   string
	Fields []string
	Unique bool
}

type Relation

type Relation struct {
	Type       RelationType
	Model      string
	ForeignKey string
	References string
	OnDelete   string
	OnUpdate   string
}

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 New

func New(name string) *Schema

func (*Schema) AddField

func (s *Schema) AddField(field Field) *Schema

func (*Schema) AddIndex

func (s *Schema) AddIndex(index Index) *Schema

func (*Schema) AddRelation

func (s *Schema) AddRelation(name string, relation Relation) *Schema

func (*Schema) GetColumnNameByFieldName

func (s *Schema) GetColumnNameByFieldName(fieldName string) (string, error)

GetColumnNameByFieldName returns the database column name for a given schema field name

func (*Schema) GetField

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

func (*Schema) GetFieldByColumnName

func (s *Schema) GetFieldByColumnName(columnName string) (*Field, error)

GetFieldByColumnName returns a field by its database column name

func (*Schema) GetFieldByName

func (s *Schema) GetFieldByName(name string) *Field

GetFieldByName returns a field by name (returns nil if not found)

func (*Schema) GetFieldNameByColumnName

func (s *Schema) GetFieldNameByColumnName(columnName string) (string, error)

GetFieldNameByColumnName returns the schema field name for a given column name

func (*Schema) GetPrimaryKey

func (s *Schema) GetPrimaryKey() (*Field, error)

func (*Schema) GetRelation

func (s *Schema) GetRelation(relationName string) (Relation, error)

GetRelation returns a relation by name

func (*Schema) GetRelationByFieldName

func (s *Schema) GetRelationByFieldName(fieldName string) (*Relation, error)

GetRelationByFieldName finds a relation that uses the specified field

func (*Schema) GetRelationsToModel

func (s *Schema) GetRelationsToModel(modelName string) []Relation

GetRelationsToModel returns all relations that point to the specified model

func (*Schema) GetTableName

func (s *Schema) GetTableName() string

GetTableName returns the database table name (same as TableName field, but method for consistency)

func (*Schema) HasRelation

func (s *Schema) HasRelation(relationName string) bool

HasRelation checks if a relation exists

func (*Schema) MapColumnDataToSchema

func (s *Schema) MapColumnDataToSchema(data map[string]any) (map[string]any, error)

MapColumnDataToSchema converts data with database column names to data with schema field names

func (*Schema) MapColumnNamesToFields

func (s *Schema) MapColumnNamesToFields(columnNames []string) ([]string, error)

MapColumnNamesToFields converts a slice of database column names to schema field names

func (*Schema) MapFieldNamesToColumns

func (s *Schema) MapFieldNamesToColumns(fieldNames []string) ([]string, error)

MapFieldNamesToColumns converts a slice of schema field names to database column names

func (*Schema) MapSchemaDataToColumns

func (s *Schema) MapSchemaDataToColumns(data map[string]any) (map[string]any, error)

MapSchemaDataToColumns converts data with schema field names to data with database column names

func (*Schema) Validate

func (s *Schema) Validate() error

func (*Schema) ValidateRelations

func (s *Schema) ValidateRelations(schemas map[string]*Schema) error

ValidateRelations validates all relations in the schema

func (*Schema) WithCompositeKey

func (s *Schema) WithCompositeKey(fields []string) *Schema

func (*Schema) WithTableName

func (s *Schema) WithTableName(name string) *Schema

Jump to

Keyboard shortcuts

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