cquill/schema/field

Types

Constraints that can be applied to fields

pub type Constraint {
  NotNull
  Unique
  PrimaryKey
  ForeignKey(
    table: String,
    column: String,
    on_delete: ForeignKeyAction,
  )
  Check(name: String, expression: String)
  MinValue(min: Int)
  MaxValue(max: Int)
  MinLength(min: Int)
  MaxLength(max: Int)
  Pattern(regex: String)
}

Constructors

  • NotNull

    Field cannot be NULL

  • Unique

    Field must have unique values (per-column unique)

  • PrimaryKey

    Field is the primary key (or part of composite key)

  • ForeignKey(
      table: String,
      column: String,
      on_delete: ForeignKeyAction,
    )

    Field references another table

  • Check(name: String, expression: String)

    Custom check constraint

  • MinValue(min: Int)

    Minimum value constraint (for numeric types)

  • MaxValue(max: Int)

    Maximum value constraint (for numeric types)

  • MinLength(min: Int)

    Minimum length constraint (for string types)

  • MaxLength(max: Int)

    Maximum length constraint (for string types)

  • Pattern(regex: String)

    Regex pattern constraint (for string types)

Default value for a field

pub type Default {
  DefaultValue(dynamic.Dynamic)
  DefaultFunction(function_name: String)
  DefaultAutoIncrement
}

Constructors

  • DefaultValue(dynamic.Dynamic)

    Static default value

  • DefaultFunction(function_name: String)

    Database function as default (e.g., “now()”, “gen_random_uuid()”)

  • DefaultAutoIncrement

    Use database’s auto-increment/serial

Complete field definition with all metadata

pub type Field {
  Field(
    name: String,
    field_type: FieldType,
    default: option.Option(Default),
    constraints: List(Constraint),
    comment: option.Option(String),
  )
}

Constructors

  • Field(
      name: String,
      field_type: FieldType,
      default: option.Option(Default),
      constraints: List(Constraint),
      comment: option.Option(String),
    )

    Arguments

    name

    Field name (column name in database)

    field_type

    Field type

    default

    Default value if any

    constraints

    Constraints applied to this field

    comment

    Documentation/comment for this field

Represents the type of a database field. These map to common SQL types across different databases.

pub type FieldType {
  Integer
  BigInteger
  Float
  Decimal(precision: Int, scale: Int)
  String
  Char(length: Int)
  Boolean
  DateTime
  Date
  Time
  Binary
  Uuid
  Json
  Array(FieldType)
  Nullable(FieldType)
  Enum(name: String, values: List(String))
  Custom(type_name: String)
}

Constructors

  • Integer

    Integer type (32-bit signed) SQL: INTEGER, INT, INT4

  • BigInteger

    Big integer type (64-bit signed) SQL: BIGINT, INT8, BIGSERIAL

  • Float

    Floating point number SQL: FLOAT, DOUBLE PRECISION, REAL

  • Decimal(precision: Int, scale: Int)

    Decimal/numeric with precision SQL: DECIMAL, NUMERIC

  • String

    Variable-length string SQL: VARCHAR, TEXT, CHARACTER VARYING

  • Char(length: Int)

    Fixed-length string SQL: CHAR, CHARACTER

  • Boolean

    Boolean value SQL: BOOLEAN, BOOL

  • DateTime

    Timestamp with timezone SQL: TIMESTAMP WITH TIME ZONE, TIMESTAMPTZ

  • Date

    Date only (no time) SQL: DATE

  • Time

    Time only (no date) SQL: TIME, TIME WITHOUT TIME ZONE

  • Binary

    Binary data SQL: BYTEA, BLOB, BINARY

  • Uuid

    UUID/GUID SQL: UUID

  • Json

    JSON data SQL: JSON, JSONB

  • Array(FieldType)

    Array of another type SQL: type[], ARRAY

  • Nullable(FieldType)

    Nullable wrapper - field can be NULL This is separate from constraints to allow type composition

  • Enum(name: String, values: List(String))

    Enum type with allowed values SQL: ENUM (Postgres), CHECK constraint (others)

  • Custom(type_name: String)

    Custom/adapter-specific type For types not covered by the standard set

Actions for foreign key ON DELETE/ON UPDATE

pub type ForeignKeyAction {
  NoAction
  Restrict
  Cascade
  SetNull
  SetDefault
}

Constructors

  • NoAction
  • Restrict
  • Cascade
  • SetNull
  • SetDefault

Values

pub fn array(name: String, element_type: FieldType) -> Field

Create an array field

pub fn auto_increment(field: Field) -> Field

Mark field as auto-increment

pub fn big_integer(name: String) -> Field

Create a big integer field

pub fn binary(name: String) -> Field

Create a binary/bytea field

pub fn boolean(name: String) -> Field

Create a boolean field

pub fn check(
  field: Field,
  name: String,
  expression: String,
) -> Field

Add check constraint

pub fn comment(field: Field, text: String) -> Field

Add a comment/documentation

pub fn date(name: String) -> Field

Create a date-only field

pub fn datetime(name: String) -> Field

Create a datetime/timestamp field

pub fn decimal(name: String, precision: Int, scale: Int) -> Field

Create a decimal field with precision and scale

pub fn default_function(
  field: Field,
  function_name: String,
) -> Field

Add a database function as default

pub fn default_value(
  field: Field,
  value: dynamic.Dynamic,
) -> Field

Add a static default value

pub fn enum_(
  name: String,
  enum_name: String,
  values: List(String),
) -> Field

Create an enum field

pub fn float(name: String) -> Field

Create a float field

pub fn get_base_type(field: Field) -> FieldType

Get the field’s base type (unwrapping Nullable if present)

pub fn get_constraints(
  field: Field,
  predicate: fn(Constraint) -> Bool,
) -> List(Constraint)

Get all constraints of a specific type

pub fn get_name(field: Field) -> String

Get the field name

pub fn get_type(field: Field) -> FieldType

Get the field type

pub fn has_constraint(
  field: Field,
  predicate: fn(Constraint) -> Bool,
) -> Bool

Check if field has a specific constraint

pub fn integer(name: String) -> Field

Create an integer field

pub fn is_auto_increment(field: Field) -> Bool

Check if field has auto-increment

pub fn is_foreign_key_constraint(constraint: Constraint) -> Bool

Check if constraint is ForeignKey

pub fn is_not_null_constraint(constraint: Constraint) -> Bool

Check if constraint is NotNull

pub fn is_nullable(field: Field) -> Bool

Check if field is nullable

pub fn is_numeric_type(field_type: FieldType) -> Bool

Check if a field type is numeric

pub fn is_primary_key(field: Field) -> Bool

Check if field is a primary key

pub fn is_primary_key_constraint(constraint: Constraint) -> Bool

Check if constraint is PrimaryKey

pub fn is_temporal_type(field_type: FieldType) -> Bool

Check if a field type is temporal (date/time related)

pub fn is_text_type(field_type: FieldType) -> Bool

Check if a field type is text-like

pub fn is_unique_constraint(constraint: Constraint) -> Bool

Check if constraint is Unique

pub fn json(name: String) -> Field

Create a JSON field

pub fn max_length(field: Field, max: Int) -> Field

Add maximum length constraint

pub fn max_value(field: Field, max: Int) -> Field

Add maximum value constraint

pub fn min_length(field: Field, min: Int) -> Field

Add minimum length constraint

pub fn min_value(field: Field, min: Int) -> Field

Add minimum value constraint

pub fn new(name: String, field_type: FieldType) -> Field

Create a new field with just name and type

pub fn not_null(field: Field) -> Field

Add NOT NULL constraint

pub fn nullable(field: Field) -> Field

Mark field as nullable

pub fn pattern(field: Field, regex: String) -> Field

Add regex pattern constraint

pub fn primary_key(field: Field) -> Field

Mark as primary key

pub fn references(
  field: Field,
  table: String,
  column: String,
) -> Field

Add foreign key constraint

pub fn references_with_action(
  field: Field,
  table: String,
  column: String,
  on_delete: ForeignKeyAction,
) -> Field

Add foreign key with ON DELETE action

pub fn string(name: String) -> Field

Create a string/text field

pub fn time(name: String) -> Field

Create a time-only field

pub fn type_name(field_type: FieldType) -> String

Get a human-readable name for a field type

pub fn unique(field: Field) -> Field

Add UNIQUE constraint

pub fn uuid(name: String) -> Field

Create a UUID field

pub fn with_default(field: Field, default: Default) -> Field

Add a default value

Search Document