Skip to content

Introduce helpers to construct raw query string for FK data fetching with serializable class #965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

hieuwu
Copy link
Contributor

@hieuwu hieuwu commented Jun 14, 2025

What kind of change does this PR introduce?

Introduce helpers to construct raw query string for FK data fetching with serializable class

What is the current behavior?

Currently, when query data with foreign keys, we need to define a hardcoded string. For query that has one FK and multiple FK, they are different in format

  • Strings are error-prone and hard to manage
  • When DTO class changes, the related query has to be changed accordingly, now it is done manually

What is the new behavior?

Introduce helpers to construct raw query string with serializable classes

  • With new helpers, it work seamlessly with the mechanism of data class used to decode the response.
  • Same DTO class can be used to construct query and decode data
  • Raw query strings are updated accordingly when data class Json properties change
    For example:
val query = RawQueryHelper.queryWithMultipleForeignKeys<MessageDto>()

// Returns
 """
content,
recipient: recipient(id, username, email),
sender: sender(id, username, email),
created_at
"""
 val columns = Columns.raw(query)
 val messages = postgrest.from("messages")
                .select(
                    columns = columns
                ) {
                    filter {
                         eq(queryParam, queryValue)
                     }
    }.decodeList<MessageDto>()

return result
}

fun buildKeyString(descriptor: SerialDescriptor, className: String): String {

Check warning

Code scanning / detekt

Public functions require documentation. Warning

The function buildKeyString is missing documentation.
@jan-tennert
Copy link
Collaborator

jan-tennert commented Jun 14, 2025

I know that specifying columns currently isn't ideal especially with FKs, thats why I introduced #769, but to have somewhat of consistency between client libs I'm still waiting for implementations in other client libs. But great work, #769 has a more general approach for solving the columns problem via a KSP plugin

cc @grdsdev

@hieuwu
Copy link
Contributor Author

hieuwu commented Jun 15, 2025

I know that specifying columns currently isn't ideal especially with FKs, thats why I introduced #769, but to have somewhat of consistency between client libs I'm still waiting for implementations in other client libs. But great work, #769 has a more general approach for solving the columns problem via a KSP plugin

cc @grdsdev

Interesting. I also tried KSP at the beginning but it took me too much time for general approach so I ended up just build helper functions 😂.
Hey Jan will there be any chance we can add KSP solution as experiment API? I am building project and use FK quite heavily, happy to test it.

@jan-tennert
Copy link
Collaborator

I know that specifying columns currently isn't ideal especially with FKs, thats why I introduced #769, but to have somewhat of consistency between client libs I'm still waiting for implementations in other client libs. But great work, #769 has a more general approach for solving the columns problem via a KSP plugin
cc @grdsdev

Interesting. I also tried KSP at the beginning but it took me too much time for general approach so I ended up just build helper functions 😂. Hey Jan will there be any chance we can add KSP solution as experiment API? I am building project and use FK quite heavily, happy to test it.

Sure, I can publish a version. You can try 3.2.0-ksp-b1, once its done building

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants