rival-black
rival-black2y ago

Custom CRUD operations

Can anyone point me towards resources/docs where I can learn about implementing custom crud operations. Like using useCreate outside of the Create view component.
5 Replies
kapa.ai
kapa.ai2y ago
Hi @ansellmax. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Sure, I can guide you on how to implement custom CRUD operations in refine. The useCustom data hook is used to consume the custom method in refine. Here is an example of how to use it:
import { useCustom, useApiUrl } from "@refinedev/core";

const { data, isLoading } = useCustom({
url: `${apiURL}/posts-unique-check`,
method: "get",
config: {
query: {
title: "Foo bar",
},
},
});
import { useCustom, useApiUrl } from "@refinedev/core";

const { data, isLoading } = useCustom({
url: `${apiURL}/posts-unique-check`,
method: "get",
config: {
query: {
title: "Foo bar",
},
},
});
You can find more information about the useCustom hook in the refine documentation (https://refine.dev/docs/api-reference/core/hooks/data/useCustom/). However, the information extracts provided do not contain specific examples of using useCreate outside of the Create view component. For more detailed information on this, I would recommend checking the official refine documentation or their API reference. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2y ago
<@585815815526285331> kapa.ai is still learning and improving, please let us how it did by reacting below
rival-black
rival-blackOP2y ago
@kapa.ai I'm using Supabase. I just need to know the recommended implementations when calling things like useCreate explicitly instead of relying on the provided Create view component.
kapa.ai
kapa.ai2y ago
Let me check the knowledge base for you. One moment! :books: ---------------------- Sure, you can use the useCreate hook explicitly in your components to create new records. Here's an example of how you can use it:
import { useCreate } from "@refinedev/core";

const { mutate } = useCreate();

mutate({
resource: "products",
values: {
name: "New Product",
material: "Wood",
},
});
import { useCreate } from "@refinedev/core";

const { mutate } = useCreate();

mutate({
resource: "products",
values: {
name: "New Product",
material: "Wood",
},
});
In this example, mutate is a method returned by useCreate which expects values and resource as parameters. These parameters will be passed to the create method from the dataProvider as parameters. The resource parameter is the name of the resource you want to create, and values are the new data for the resource. You can also customize the success notification that shows up when the data is fetched successfully. Here's how you can do it:
const { mutate } = useCreate();

mutate({
successNotification: (data, values, resource) => {
return {
message: `${data.title} Successfully fetched.`,
description: "Success with no errors",
type: "success",
};
},
});
const { mutate } = useCreate();

mutate({
successNotification: (data, values, resource) => {
return {
message: `${data.title} Successfully fetched.`,
description: "Success with no errors",
type: "success",
};
},
});
In this example, successNotification is a function that returns a notification object. This function is called when the data is fetched successfully. If you have more than one dataProvider, you can specify which one to use with the dataProviderName prop:
const { mutate } = useCreate();

mutate({
dataProviderName: "second-data-provider",
});
const { mutate } = useCreate();

mutate({
dataProviderName: "second-data-provider",
});
In this example, second-data-provider is the name of the dataProvider to use. You can find more information about useCreate in the refine documentation (https://refine.dev/docs/api-reference/core/hooks/data/useCreate/). ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2y ago
<@585815815526285331> kapa.ai is still learning and improving, please let us how it did by reacting below