Creating Fields

Fields define the structure of your database columns. Every field is created using model.CreateField() and configured through a fluent, chainable API.


model.CreateField()
                    

A field definition typically consists of:

  • A type (VARCHAR, INT, JSON, etc.)
  • Optional constraints (NOT NULL, UNIQUE)
  • Optional defaults
  • Optional indexes

Field Types

String & Text Types


AsChar(n)
AsVarchar(n)
AsTinyText()
AsText()
AsMediumText()
AsLongText()
                    

Numeric Types


AsTinyInt()
AsSmallInt()
AsMediumInt()
AsInt()
AsBigInt()
AsFloat()
AsDouble()
AsDecimal(precision)
                    

Boolean & UUID


AsBool()
AsUUID()
                    

Date & Time


AsDate()
AsTime()
AsTimestamp()
AsYear()
                    

JSON & Structured


AsJSON()
AsEnum(values...)
AsSet(values...)
                    

Binary & Blob


AsBlob()
AsTinyBlob()
AsMediumBlob()
AsLongBlob()
                    

Geometry Types


AsGeometry()
AsPoint()
AsLineString()
AsPolygon()
                    

Field Modifiers

Modifiers change how a field behaves in the database. They can be chained in any order.


NotNull()
IsPrimary()
IsUnique()
IsIndex()
                    

By default, fields are nullable unless NotNull() is explicitly specified.

Default Values

Default values are applied at the database level. ModelsHandler supports static and dynamic defaults.


Default("guest")
DefaultNull()
DefaultNow()
                    

DefaultNow() maps to CURRENT_TIMESTAMP for supported column types.

Indexes & Keys

Indexes and keys are declared directly on fields. Schema synchronization ensures they stay in sync.


IsPrimary()   // PRIMARY KEY
IsUnique()    // UNIQUE INDEX
IsIndex()     // NORMAL INDEX
                    

Examples


Email: model.CreateField().
    AsVarchar(255).
    NotNull().
    IsUnique()

CreatedAt: model.CreateField().
    AsTimestamp().
    DefaultNow().
    NotNull()

Metadata: model.CreateField().
    AsJSON()

Status: model.CreateField().
    AsEnum("active", "inactive").
    Default("'active'")
                    

Next Steps