Skip to content
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

Generic query for PostgrestBuilder #253

Closed
bryan-vh opened this issue Mar 1, 2024 · 6 comments
Closed

Generic query for PostgrestBuilder #253

bryan-vh opened this issue Mar 1, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@bryan-vh
Copy link

bryan-vh commented Mar 1, 2024

Feature request

I'm currently working on trying to make a generic class to handle typical CRUD operations on a database. Rather than create multiple classes for each model instance, I'd like to use one. While this works for simple use cases, when trying to chain together additional operators at will becomes cumbersome, and I have to revert back to using a standalone class for this.

A clear and concise description of what you want and what your use case is.

Rather than having to write supabase.database.from("posts").select().equals(...).notEquals(...).execute().value, it'd be nice if I could do something like the following: supabase.database.from("posts").query(query), where now I can write my custom query in another location and keep the generic supabase.database.from()....

Describe alternatives you've considered

It would be easy to just repeat this code elsewhere or not create my own abstraction on top of the Supabase client, but other folks may find this handy

@bryan-vh bryan-vh added the enhancement New feature or request label Mar 1, 2024
@therickys93
Copy link

👍 it could be a useful function.

@grdsdev
Copy link
Collaborator

grdsdev commented Mar 7, 2024

Hi @bryan-vh thanks for opening this issue.

I think the implementation for this would be very straightforward and could be done via an extension, and not provided by the library, but since you and @therickys93 finds this useful, I'll verify the possibility of adding it in the library.

Do you have any implementation you're already using and don't mind sharing it with us?

@therickys93
Copy link

Currently I do not have an implementation, because it is, at least for me, a feature request.

I don't know if it is visible, but I was thinking something like that:

String query = "SELECT * FROM users WHERE id = 1";
client.query(query);

If it is something that it is not ok for you, please ignore this comment.

@grdsdev
Copy link
Collaborator

grdsdev commented May 21, 2024

Hi @therickys93 if you want to execute a raw SQL in the DB, the way you can accomplish this today is by creating a RPC function that accepts the SQL and runs it.

Example:

struct RawSQL: Encodable {
   let sql: String
}

try await supabase.rpc("execute_sql", params: RawSQL(sql: "SELECT * FROM users WHERE id = 1").execute()

Edit: Although this is possible, this IS NOT RECOMMENDED and it can lead to serious security problems

@therickys93
Copy link

Hi @grdsdev thanks so much for your reply. I have only one question. How can I create a RPC function that accepts the SQL and runs it? Maybe it is off-topic.

@grdsdev
Copy link
Collaborator

grdsdev commented May 21, 2024

@therickys93 I edited my comment, although this is possible, this IS NOT RECOMMENDED and it can lead to serious security problems.

Another approach for the generic query could be:

typealias GenericQuery = (PostgrestQueryBuilder) throws -> PostgrestBuilder

extension PostgrestQueryBuilder {
  func query(_ apply: GenericQuery) rethrows -> PostgrestBuilder {
    try apply(self)
  }
}

And then use it as:

func insert(_ todo: Todo) -> GenericQuery {
    {
        $0.insert(todo).single()
    }
}

try await supabase.from("todo")
    .query(insert(todo))
    .execute
    .value as Todo

So it is possible to create a generic query approach, I'm not adding this to the library, as we plan to make some improvements soon that would touch this.

@grdsdev grdsdev closed this as completed May 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants