absent-sapphire
absent-sapphire5mo ago

Supabase functions

I need to use a supabase function to fetch a result of location decoding from long encoded postgis location string string to x and y values and distance calculation, i've implemented the function fetch_parking_spaces_by_distance(lat float, long float) that returns my desired table. I don't know how to implement it to display it in a list. Is there a way to override the schema and datasource for a supabase.rpc()?
11 Replies
kapa.ai
kapa.ai5mo ago
Hi @Fifinio. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Hmm, I don't know enough to give you a confident answer yet. The information extracts provided do not contain specific details on how to use a Supabase function to fetch a result of location decoding from a long encoded PostGIS location string to x and y values and distance calculation. They also do not provide information on how to implement it to display it in a list or how to override the schema and datasource for a supabase.rpc(). However, Refine does allow you to use a custom schema by passing the schema property in the meta object. Here's an example:
const tableProps = useTable<IUser>({
resource: "posts",
meta: {
schema: "foo",
},
});
const tableProps = useTable<IUser>({
resource: "posts",
meta: {
schema: "foo",
},
});
Source (https://refine.dev/docs/data/packages/supabase#schema---using-custom-schema) For more detailed information, I recommend checking the official Refine (https://refine.dev/docs) and Supabase (https://supabase.io/docs) documentation. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@327451535170600960> kapa.ai is still learning and improving, please let us know how it did by reacting below
absent-sapphire
absent-sapphire5mo ago
How can I modify existing supabase data-provider to support supabase.rpc()? @kapa.ai How can I modify existing supabase data-provider to support supabase.rpc()?
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- To modify the existing Supabase data provider to support supabase.rpc(), you would need to extend the existing data provider and add a new method that uses supabase.rpc(). Here's an example of how you might do this:
import { dataProvider as supabaseDataProvider } from "@refinedev/supabase";
import { supabaseClient } from "utility";

const dataProvider = {
...supabaseDataProvider(supabaseClient),
rpc: async (resource, params) => {
const { data, error } = await supabaseClient.rpc(resource, params);
if (error) {
console.error(error);
return;
}
return data;
},
};

function App() {
return (
<Refine
dataProvider={dataProvider}
//...
>
{/* ... */}
</Refine>
);
}

export default App;
import { dataProvider as supabaseDataProvider } from "@refinedev/supabase";
import { supabaseClient } from "utility";

const dataProvider = {
...supabaseDataProvider(supabaseClient),
rpc: async (resource, params) => {
const { data, error } = await supabaseClient.rpc(resource, params);
if (error) {
console.error(error);
return;
}
return data;
},
};

function App() {
return (
<Refine
dataProvider={dataProvider}
//...
>
{/* ... */}
</Refine>
);
}

export default App;
In this example, we're spreading the existing data provider methods and adding a new rpc method. This rpc method takes a resource and params as arguments, which correspond to the function name and parameters in your Supabase database. It then calls supabaseClient.rpc() with these arguments and returns the data. Please note that this is a general example and might need adjustments based on your specific use case. For more information on how to use the data provider in Refine, you can check the official documentation (https://refine.dev/docs/api-reference/core/providers/data-provider/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@327451535170600960> kapa.ai is still learning and improving, please let us know how it did by reacting below
absent-sapphire
absent-sapphire5mo ago
@kapa.ai can you show usage?
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Sure, once you have your custom data provider set up with the rpc method, you can use it in your components like this:
import { useDataProvider } from "@refinedev/core";

function MyComponent() {
const dataProvider = useDataProvider();

const fetchData = async () => {
const data = await dataProvider.rpc("fetch_parking_spaces_by_distance", { lat: 40.7128, long: 74.0060 });
console.log(data);
};

useEffect(() => {
fetchData();
}, []);

// ...
}
import { useDataProvider } from "@refinedev/core";

function MyComponent() {
const dataProvider = useDataProvider();

const fetchData = async () => {
const data = await dataProvider.rpc("fetch_parking_spaces_by_distance", { lat: 40.7128, long: 74.0060 });
console.log(data);
};

useEffect(() => {
fetchData();
}, []);

// ...
}
In this example, useDataProvider hook is used to access the dataProvider inside a component. Then, the rpc method is called with the name of the function (fetch_parking_spaces_by_distance) and the parameters ({ lat: 40.7128, long: 74.0060 }). The result is logged to the console. Please note that this is a general example and might need adjustments based on your specific use case. For more information on how to use the data provider in Refine, you can check the official documentation (https://refine.dev/docs/api-reference/core/providers/data-provider/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@327451535170600960> kapa.ai is still learning and improving, please let us know how it did by reacting below
absent-sapphire
absent-sapphire5mo ago
can i integrate it with useTable? @kapa.ai can i integrate it with useTable?
kapa.ai
kapa.ai5mo ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Yes, you can integrate it with useTable. However, you would need to create a custom hook that uses useTable and your custom rpc method from the data provider. Here's an example of how you might do this:
import { useDataProvider, useTable } from "@refinedev/core";

function useTableWithRpc<TRecord, TError>(
resource: string,
initialParams: any
) {
const dataProvider = useDataProvider();
const { tableProps, ...rest } = useTable<TRecord, TError>({ resource });

useEffect(() => {
const fetchData = async () => {
const data = await dataProvider.rpc(resource, initialParams);
// process data and update tableProps
};

fetchData();
}, [resource, initialParams]);

return { tableProps, ...rest };
}
import { useDataProvider, useTable } from "@refinedev/core";

function useTableWithRpc<TRecord, TError>(
resource: string,
initialParams: any
) {
const dataProvider = useDataProvider();
const { tableProps, ...rest } = useTable<TRecord, TError>({ resource });

useEffect(() => {
const fetchData = async () => {
const data = await dataProvider.rpc(resource, initialParams);
// process data and update tableProps
};

fetchData();
}, [resource, initialParams]);

return { tableProps, ...rest };
}
In this example, useTableWithRpc is a custom hook that takes a resource and initialParams as arguments. It uses useDataProvider to get the data provider and useTable to get the table props. Then, it calls dataProvider.rpc with the resource and initialParams and updates the table props with the returned data. You can use this custom hook in your components like this:
function MyComponent() {
const { tableProps } = useTableWithRpc("fetch_parking_spaces_by_distance", { lat: 40.7128, long: 74.0060 });

return (
<Table {...tableProps}>
{/* table columns */}
</Table>
);
}
function MyComponent() {
const { tableProps } = useTableWithRpc("fetch_parking_spaces_by_distance", { lat: 40.7128, long: 74.0060 });

return (
<Table {...tableProps}>
{/* table columns */}
</Table>
);
}
Please note that this is a general example and might need adjustments based on your specific use case. For more information on how to use the data provider and useTable in Refine, you can check the official documentation (https://refine.dev/docs/api-reference/core/providers/data-provider/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai5mo ago
<@327451535170600960> kapa.ai is still learning and improving, please let us know how it did by reacting below