Skip to content

About

Generate kotlinx.serialization data classes directly from a Postgres/Supabase schema

Resources

Stars

3 stars

Watchers

0 watching

Forks

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

postgres-kotlin-gen

Generate kotlinx.serialization data classes directly from a Postgres (or Supabase) database.

This is a small standalone Rust CLI that fills the gap until Kotlin type generation lands upstream in supabase/postgres-meta#1086. It introspects the live schema straight from pg_catalog (the same source postgres-meta uses) and emits idiomatic @Serializable classes, so it's a drop-in you can retire once the official generator ships.

Installation

Published on crates.io:

cargo install postgres-kotlin-gen

This installs the postgres-kotlin-gen binary into ~/.cargo/bin (make sure it's on your PATH). Verify with:

postgres-kotlin-gen --help

Requires a Rust toolchain. To install a specific version, use cargo install postgres-kotlin-gen --version 0.1.0.

What it emits

For each schema it generates one namespace object containing:

  • Enums → @Serializable enum class with @SerialName per label.
  • Composite types → @Serializable data class.
  • Tables → three variants:
    • Table — the full row (Select).
    • TableInsert — writable columns; columns with a default/identity or that are nullable become optional (T? = null). GENERATED ALWAYS columns are omitted.
    • TableUpdate — every writable column optional for partial updates.
  • Views / materialized views → a single read-only data class.

Kotlin-idiomatic details, matching the upstream PR:

  • Column names are camelCased; @SerialName("...") is added only when the name actually differs.
  • Nullable properties get = null defaults so Insert/Update payloads stay optional.
  • Empty tables become a plain @Serializable class (a data class needs at least one parameter).
  • Hard keywords (class, object, when, …) are backtick-escaped.
  • Imports (SerialName, JsonElement) are added only when used.

Type mapping

Postgres Kotlin
int2 Short
int4 / serial Int
int8 / bigserial / oid Long
float4 Float
float8 Double
numeric / decimal / money Double
bool Boolean
json / jsonb JsonElement
uuid, text, varchar, date/time, etc. String
enum type generated enum class
composite type generated data class
T[] List<T>
anything else JsonElement

Usage

# Local Supabase (postgres:postgres@127.0.0.1:54322)
postgres-kotlin-gen --local --out Database.kt

# Any connection string (hosted Supabase pooler, sslmode handled from the URL)
postgres-kotlin-gen --db-url "postgresql://postgres:pass@db.project.supabase.co:5432/postgres" \
  --package com.example.db --out Database.kt

# Falls back to $SUPABASE_DB_URL then $DATABASE_URL
export SUPABASE_DB_URL=postgresql://...
postgres-kotlin-gen --schema public --schema auth

Options

Flag Description
--db-url <URL> Connection string. Falls back to $SUPABASE_DB_URL, $DATABASE_URL.
--local Shortcut for the default local Supabase database.
--schema <NAME> Schema to include (repeatable). Defaults to public.
--package <NAME> Kotlin package declaration for the file.
--visibility <public|internal> Access modifier for generated declarations (default public).
-o, --out <FILE> Write to a file instead of stdout.

Build from source

For local development or building without installing from crates.io:

git clone https://github.com/AndroidPoet/postgres-kotlin-gen
cd postgres-kotlin-gen
cargo build --release
./target/release/postgres-kotlin-gen --help

About

Generate kotlinx.serialization data classes directly from a Postgres/Supabase schema

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages