zio / zio-quill

Compile-time Language Integrated Queries for Scala
https://zio.dev/zio-quill
Apache License 2.0
2.14k stars 347 forks source link

[Feature request] Allow re-use of case class for many table names (during compile) #3044

Open LannyRipple opened 3 weeks ago

LannyRipple commented 3 weeks ago

Version: 4.8.4 Module: quill-jdbc Database: postgresql

Desired Behavior

Allow for changing tableName enabling re-use of behaviors.

Actual behavior

// fails
class Foo(tableName: String) {
  def example(): List[PgTable] = {
    val query =
      schemaQuery[PgTable](tableName) 
        ...
  }
}

// val query = schemaQuery[PgTable]("NewTableName")...   works as expected

Above will fail to compile. Fair since tableName is not known at compile-time. I've used dynamicSchemaQuery[A](tableName) as a work-around. I'm hoping for some way to get at compile-time compilation of queries (for the perceived performance improvement since dynamic query documentation mentions a performance hit).

Motivation

Moving our system into a cluster that will not allow DocumentDB (which our system is based on). Decided on AWS RDS Postgres and Quill. Because we are moving from DocumentDB it seemed natural to use a

// quill encoder for Json <-> jsonb
case class PgTable(uuid: String, obj: Json, lastUpdated: ZonedDateTime)

to represent the layout of our many collections (now tables) and make use of Postgres's json operations. I created a basic PgRepository[A: Encoder: Decoder](tableName: String, quill: PostgresJdbcContext[Literal]) to handle most of the work. I wouldn't be adverse to using a renamed case class with same shapeless shape to handle tableName but that would also mean duplicating every service class for a new table with boilerplate def basicRepoMethod(id: String): A = { val q = query[NextTableName]... for every table.

Steps to reproduce the behavior

Happy to work up a gist or example repo if needed.

Workaround

None found (and looked at schemaMeta, etc).

With more knowledge I would make a plugin that generated the boilerplate needed for my list of tableNames against PgTable but that is beyond me.

I get that this is a niche request, a result of macro behavior, and not how one normally uses Postgres. Wanted to document the request in case it's not as difficult as I'm thinking it is.

@getquill/maintainers