Add a Kotlin type generator to postgres-meta, enabling supabase gen types kotlin via the CLI.
This addresses supabase-community/supabase-kt#647, which requests generating Kotlin types from database schema for the Kotlin Supabase SDK.
Type generators already exist for TypeScript, Swift, Go, and Python. Kotlin is the primary language
for Android development and is widely used in multiplatform projects. The Kotlin Supabase SDK
(supabase-kt) currently requires developers to
manually write @Serializable data classes to match their database schema.
Given a table like:
create table todos (
id bigint generated always as identity primary key,
task text not null,
is_complete boolean default false,
created_at timestamptz default now()
);
The generator would produce:
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
@Serializable
data class TodosSelect(
@SerialName("created_at")
val createdAt: String,
val id: Long,
@SerialName("is_complete")
val isComplete: Boolean? = null,
val task: String
)
@Serializable
data class TodosInsert(
@SerialName("is_complete")
val isComplete: Boolean? = null,
val task: String
)
@Serializable
data class TodosUpdate(
@SerialName("is_complete")
val isComplete: Boolean? = null,
val task: String? = null
)
Design
Follows the same pattern as the Swift generator (PR #779):
- Three data classes per table: Select, Insert, Update
- Select-only data class for views
- @Serializable enum classes for PostgreSQL enums
- @SerialName annotations where camelCase property names differ from column names
- Nullable properties with = null defaults based on operation type
- Kotlin keyword escaping with backticks
Takeshi Hagikura proposes adding a Kotlin type generator to postgres-meta, enabling the command supabase gen types kotlin via the CLI. This feature would automate the generation of Kotlin data classes from database schemas, similar to existing generators for TypeScript, Swift, Go, and Python. The proposal includes an implementation plan with a focus on creating plain data classes initially, followed by a type-safe client integration.
If it sounds right, I have following implementation plan in mind
PR: supabase/postgres-meta#1050
Adds a Kotlin template to postgres-meta, enabling supabase gen types kotlin. Generated output is
plain @Serializable data classes — same approach as the Swift generator (#779).
// Generated from database schema
@Serializable
data class MoviesSelect(
val id: Long,
val title: String,
val genre: String? = null,
@SerialName("created_at")
val createdAt: String? = null
)
@Serializable
data class MoviesInsert(
val title: String,
val genre: String? = null
)
@Serializable
data class MoviesUpdate(
val title: String? = null,
val genre: String? = null
)
Usage:
val movies: List<MoviesSelect> = client.from("movies").select().decodeList()
client.from("movies").insert(MoviesInsert(title = "Foo"))
The client has no compile-time knowledge of table names or schemas — you manually pair the right type with the right table string. This matches the current state of Swift type generation.
Depends on v0. Would add a TypedTable abstraction to supabase-kt's Postgrest module so the compiler
enforces correct types:
// Generated
object Tables {
val movies = TypedTable<MoviesSelect, MoviesInsert, MoviesUpdate>("movies")
}
// Usage — compiler knows the types
val movies = client.from(Tables.movies).select() // returns List<MoviesSelect>
client.from(Tables.movies).insert(MoviesInsert(title = "Foo")) // type-safe
client.from(Tables.movies).insert(MoviesSelect(...)) // compile error
This is analogous to how TypeScript's createClient<Database>() flows types through .from() and
.select(), adapted to Kotlin's type system using generics instead of mapped types.
| v0 | v1 | |
|---|---|---|
| Where | postgres-meta | postgres-meta + supabase-kt |
| What | Plain data classes | TypedTable abstraction + Tables object |
| Type safety | Serialization only | Table names + column types at compile time |
| Prerequisite | None | v0 |