Configuration

Without --config, sqlode looks in the current directory for sqlode.yaml, sqlode.yml, sqlc.yaml, sqlc.yml or sqlc.json, and stops if it finds more than one.

version: "2"
sql:
  - name: "app"
    schema: "db/schema.sql"
    queries: "db/query.sql"
    engine: "postgresql"
    gen:
      gleam:
        out: "src/db"
        runtime: "native"

schema and queries take a path, a list of paths or a directory of .sql files. The schema can be a snapshot or a migration history. name labels the block in messages.

gen.gleam keys

KeyDefaultEffect
outrequiredOutput directory
runtimerawnative also writes an adapter for pog, sqlight or shork
type_mappingstringrich or strong for date, time, UUID and JSON types (below)
vendor_runtimefalseCopies sqlode/runtime into out so sqlode can be a dev dependency
strict_viewstruefalse warns about view columns it cannot resolve and drops them
emit_sql_as_commentfalseAdds each query’s SQL as a comment on its adapter function
emit_exact_table_namesfalseauthors becomes Authors instead of Author
omit_unused_modelsfalseLeaves out table records that no query uses
query_parameter_limitnoneverify reports queries with more inferred parameters

Types

SQL typeGleam type
INT, INTEGER, SMALLINT, BIGINT, SERIAL, BIGSERIALInt
FLOAT, DOUBLE, REAL, NUMERIC, DECIMAL, MONEYFloat
BOOLEAN, BOOL, MySQL TINYINT(1)Bool
MySQL DECIMAL, NUMERICString, so no digits are lost
TEXT, VARCHAR, CHAR, and other text-like typesString
BYTEA, BLOB, BINARYBitArray
TIMESTAMP, DATETIME, DATE, TIME, TIMETZ, INTERVAL, UUID, JSON, JSONBString
CITEXT, INET, CIDR, MACADDR, XML, BIT, TSVECTOR, TSQUERY, geometric typesString
TYPE[], TYPE ARRAYList(TYPE)
PostgreSQL CREATE TYPE ... AS ENUM, MySQL ENUM and SETA custom type with to_string and from_string functions

A column without NOT NULL becomes Option(T).

type_mapping: "rich" turns the date, time, UUID and JSON types, and MySQL decimals, into String aliases such as SqlUuid, which document a signature without a compiler check. strong makes them wrapper types such as SqlUuid(String) with a sql_uuid_to_string function, so a UUID and a String cannot be mixed up.

Overrides and renames

sql:
  - schema: "db/schema.sql"
    queries: "db/query.sql"
    engine: "postgresql"
    gen:
      gleam:
        out: "src/db"
    overrides:
      types:
        - db_type: "uuid"
          gleam_type: "String"
        - column: "users.id"
          gleam_type: "myapp/types.UserId"
          encode: "user_id_to_int"
          decode: "int_to_user_id"
      renames:
        - table: "authors"
          column: "bio"
          rename_to: "biography"

db_type applies to every column of that type, and column to one column. A column override wins.

A custom gleam_type without encode and decode must be a type alias of the underlying type. For any other type, give both functions; sqlode looks them up in the module of gleam_type, so the example calls types.user_id_to_int.

What sqlode understands

Schema statements: CREATE TABLE, CREATE VIEW, CREATE TYPE ... AS ENUM, ALTER TABLE (add, drop and rename columns, rename the table, change a column’s type and nullability), and DROP TABLE, DROP VIEW, DROP TYPE. PostgreSQL and SQLite skip other statements; MySQL stops with SQD2004.

Parameter types are inferred from:

Anywhere else, generation stops with SQD4003. Add a cast.

Result columns are resolved through joins, RETURNING, CTEs and views.

✨ Search Document