deep-jade
deep-jade2y ago

How to call database function in supabase?

I have created a supbase database function and want to call from refine app, how can I do that? Is there a method in refine-supabase data provider or should I use the supabaseClient.rpc('function_name') method from supabase?
4 Replies
conscious-sapphire
conscious-sapphire2y ago
refine-supabase don't have implementations for functions. of course you can use supabaseClient
deep-jade
deep-jade2y ago
Ok thank you @alicanerdurmaz
dependent-tan
dependent-tan2y ago
Hey @dipbazz, just saw this thread. I was thinking about how can we support the functions for supabase the other day 😅 I thought maybe we can look for a metadata property in custom method so we can use them through useCustom and useCustomMutation 🤔 Do you have any ideas on this? Maybe a better way to make them available through refine? I don’t have much experience on the supabase functions and thought maybe you can help 🙏
deep-jade
deep-jade2y ago
Hey @aliemirs, I am also scratching the surface of supabase but what I know is database function has the power to compute the complex database operation on database level. Those functions either returns the response or not. We can perform multiple tables joins and return the data after computing our business logic and so on. As you have have mentioned may be we can use useCustom and take a parameters as (resource, method, payload, headers) to hit the endpoint. We may implement it similar to refine-simple-rest as
custom: async ({ resource, method, payload, headers }) => {
// other code related to different method
switch (method) {
case "put":
case "patch":
supabaseResponse = await supbaseClient.from(resource).update(payload);
case "post":
supabaseResponse = await supbaseClient.from(resource).insert(payload);
break;
case "delete":
supabaseResponse = await supabaseClient.from(resource).delete();
break;
case "function":
supabaseResponse = await supabaseClient.rpc(resource, payload); // <<<<====== Here in this way we can send a method as function
default:
supabaseResponse = await supabase.from(resource).select();
break;
}
custom: async ({ resource, method, payload, headers }) => {
// other code related to different method
switch (method) {
case "put":
case "patch":
supabaseResponse = await supbaseClient.from(resource).update(payload);
case "post":
supabaseResponse = await supbaseClient.from(resource).insert(payload);
break;
case "delete":
supabaseResponse = await supabaseClient.from(resource).delete();
break;
case "function":
supabaseResponse = await supabaseClient.rpc(resource, payload); // <<<<====== Here in this way we can send a method as function
default:
supabaseResponse = await supabase.from(resource).select();
break;
}
So in that way may be we can pass a method as function and call the rpc method from supabaseClient. I am not sure if this is the correct way to implement the function. But just a idea on using custom hook.