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
Comments
👍 it could be a useful function. |
Hi @bryan-vh thanks for opening this issue. I think the implementation for this would be very straightforward and could be done via an Do you have any implementation you're already using and don't mind sharing it with us? |
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. |
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 |
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. |
@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. |
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 genericsupabase.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
The text was updated successfully, but these errors were encountered: