cquill/codegen/type_mapping

Types

Extended column metadata including Gleam type and generation info

pub type ColumnMeta {
  ColumnMeta(
    gleam_type: GleamType,
    is_auto_generated: Bool,
    postgres_type: String,
    udt_name: String,
    column_name: String,
    is_nullable: Bool,
  )
}

Constructors

  • ColumnMeta(
      gleam_type: GleamType,
      is_auto_generated: Bool,
      postgres_type: String,
      udt_name: String,
      column_name: String,
      is_nullable: Bool,
    )

    Arguments

    gleam_type

    The mapped Gleam type

    is_auto_generated

    Whether this column is auto-generated (serial, has default, etc.) Auto-generated columns are excluded from insert types

    postgres_type

    The original PostgreSQL data type

    udt_name

    The underlying type name (useful for enums)

    column_name

    Column name

    is_nullable

    Whether the column is nullable

Represents a Gleam type that a PostgreSQL column maps to

pub type GleamType {
  GleamInt
  GleamFloat
  GleamString
  GleamBool
  GleamBitArray
  GleamTime
  GleamDate
  GleamTimeOfDay
  GleamDecimal
  GleamUuid
  GleamJson
  GleamList(element_type: GleamType)
  GleamOption(inner_type: GleamType)
  GleamCustom(module: String, type_name: String)
}

Constructors

  • GleamInt

    Gleam Int (for integer, smallint, bigint, serial, bigserial)

  • GleamFloat

    Gleam Float (for real, double precision)

  • GleamString

    Gleam String (for varchar, char, text)

  • GleamBool

    Gleam Bool (for boolean)

  • GleamBitArray

    Gleam BitArray (for bytea)

  • GleamTime

    Time type (for timestamp with/without time zone)

  • GleamDate

    Date type (for date)

  • GleamTimeOfDay

    Time of day type (for time with/without time zone)

  • GleamDecimal

    Decimal type (for numeric, decimal) - external package

  • GleamUuid

    UUID type - typically a newtype wrapper around String

  • GleamJson

    JSON type (for json, jsonb) - opaque type

  • GleamList(element_type: GleamType)

    List type for PostgreSQL arrays

  • GleamOption(inner_type: GleamType)

    Option type for nullable columns

  • GleamCustom(module: String, type_name: String)

    Custom type (for user-defined enums or types)

Errors that can occur during type mapping

pub type TypeMappingError {
  UnknownType(postgres_type: String, udt_name: String)
  UnsupportedArray(element_type: String)
}

Constructors

  • UnknownType(postgres_type: String, udt_name: String)

    Unknown PostgreSQL type that cannot be mapped

  • UnsupportedArray(element_type: String)

    Array type with unsupported element type

Values

pub fn collect_imports(columns: List(ColumnMeta)) -> List(String)

Collect all unique imports needed for a list of column metadata

pub fn get_imports(gleam_type: GleamType) -> List(String)

Get the imports needed for a GleamType

pub fn is_custom_type(gleam_type: GleamType) -> Bool

Check if a GleamType is a custom type (enum or user-defined)

pub fn is_list_type(gleam_type: GleamType) -> Bool

Check if a GleamType is a list type

pub fn is_nullable_type(gleam_type: GleamType) -> Bool

Check if a GleamType is nullable (wrapped in Option)

pub fn map_column(
  column_name: String,
  data_type: String,
  udt_name: String,
  is_nullable: Bool,
  column_default: option.Option(String),
  enum_names: List(String),
) -> Result(ColumnMeta, TypeMappingError)

Map an introspected column to full column metadata

Parameters:

  • column_name: The column name
  • data_type: PostgreSQL data type
  • udt_name: Underlying type name
  • is_nullable: Whether the column allows NULL
  • column_default: Default value expression (if any)
  • enum_names: List of known enum type names

Returns:

  • Ok(ColumnMeta) with full metadata
  • Error(TypeMappingError) if type cannot be mapped
pub fn map_postgres_type(
  data_type: String,
  udt_name: String,
  is_nullable: Bool,
  enum_names: List(String),
) -> Result(GleamType, TypeMappingError)

Map a PostgreSQL column to its corresponding Gleam type

Parameters:

  • data_type: The PostgreSQL data type (e.g., “integer”, “character varying”)
  • udt_name: The underlying type name (e.g., “int4”, “varchar”, or enum name)
  • is_nullable: Whether the column allows NULL values
  • enum_names: List of known enum type names in the schema

Returns:

  • Ok(GleamType) with the mapped type (wrapped in Option if nullable)
  • Error(TypeMappingError) if the type cannot be mapped
pub fn pascal_case(input: String) -> String

Convert snake_case to PascalCase

pub fn render_qualified_type(gleam_type: GleamType) -> String

Render a GleamType as a fully qualified Gleam type string (with imports)

pub fn render_type(gleam_type: GleamType) -> String

Render a GleamType as a Gleam type string for code generation

pub fn snake_case(input: String) -> String

Convert PascalCase or snake_case to snake_case

pub fn types_equal(type_a: GleamType, type_b: GleamType) -> Bool

Check if two GleamTypes are equal

pub fn unwrap_list(gleam_type: GleamType) -> GleamType

Get the element type from a List type

pub fn unwrap_option(gleam_type: GleamType) -> GleamType

Get the inner type from an Option type

Search Document