Lotus.Storage.TypeMapper (Lotus v0.16.5)

Copy Markdown View Source

Maps database column types to Lotus internal types for automatic casting.

Different databases use different type names for the same logical types. This module normalizes those differences into a consistent set of Lotus types that can be used for automatic value casting.

Supported Databases

  • PostgreSQL: uuid, integer, numeric, date, timestamp, text, boolean, json, etc.
  • MySQL: char(36), binary(16), int, decimal, date, datetime, varchar, json, etc.
  • SQLite: INTEGER, REAL, TEXT, BLOB, etc. (dynamic typing)

Usage

# Map PostgreSQL UUID to Lotus type
TypeMapper.db_type_to_lotus_type("uuid", Lotus.Sources.Postgres)
# => :uuid

# Map MySQL UUID storage to Lotus type
TypeMapper.db_type_to_lotus_type("char(36)", Lotus.Sources.MySQL)
# => :uuid

# Get Ecto type for casting
TypeMapper.lotus_type_to_ecto_type(:uuid)
# => Ecto.UUID

Summary

Functions

Convert database-specific type to Lotus internal type.

Returns the Ecto type equivalent for a Lotus type.

Types

lotus_type()

@type lotus_type() ::
  :uuid
  | :integer
  | :float
  | :decimal
  | :boolean
  | :date
  | :time
  | :datetime
  | :json
  | :binary
  | :text
  | :enum
  | :composite
  | {:array, lotus_type()}

Functions

db_type_to_lotus_type(db_type, arg2)

@spec db_type_to_lotus_type(
  db_type :: String.t(),
  source_module :: module()
) :: lotus_type()

Convert database-specific type to Lotus internal type.

Uses source module to determine database flavor and map accordingly.

Examples

db_type_to_lotus_type("uuid", Lotus.Sources.Postgres)
# => :uuid

db_type_to_lotus_type("char(36)", Lotus.Sources.MySQL)
# => :uuid

db_type_to_lotus_type("INTEGER", Lotus.Sources.SQLite3)
# => :integer

lotus_type_to_ecto_type(arg1)

@spec lotus_type_to_ecto_type(lotus_type()) :: atom() | {:array, atom()}

Returns the Ecto type equivalent for a Lotus type.

Used for casting values via Ecto's type system.

Examples

lotus_type_to_ecto_type(:uuid)
# => Ecto.UUID

lotus_type_to_ecto_type(:integer)
# => :integer