supabase-community / supabase-kt

A Kotlin Multiplatform Client for Supabase.
https://supabase.com/docs/reference/kotlin/introduction
MIT License
415 stars 37 forks source link

Introduce KSP compiler for generating columns via `@Selectable` annotation #769

Open jan-tennert opened 3 weeks ago

jan-tennert commented 3 weeks ago

What kind of change does this PR introduce?

Feature

What is the new behavior?

New KSP compiler module which processes @Selectable annotated data classes. I kept the KSP compiler more general, so if we ever want to add a new annotation, we can just use this module.

How it works

Design details

There are 6 annotations:

Todo:

Implementation

There is a new Postgrest config property columnRegistry which stores column data under the full class name for annotated data classes. The KSP compiler fills this registry when the generated addSelectableTypes() method is called. Example for the generated method [Not visible normally]:

/**
 * Adds the types annotated with [Selectable] to the ColumnRegistry. Allows to use the automatically generated columns in the PostgrestQueryBuilder.
 *
 * This file is generated by the SelectableSymbolProcessor.
 * Do not modify it manually.
 */
@OptIn(SupabaseInternal::class)
public fun Postgrest.Config.addSelectableTypes() {
  columnRegistry.registerColumns("Movie", "id,name,createdAt:created_at,studio:studio_id(id,name),metadata->video->resolution")
  columnRegistry.registerColumns("Studio", "id,name")
}

Additional context

See discussion in #761

grdsdev commented 3 weeks ago

Awesome @jan-tennert let me try to get roughly the same API in Swift before merging this ok? But it looks awesome.