supabase-cache-helpers
supabase-cache-helpers copied to clipboard
single() on insert doesn't seem possible
Describe the bug By using useInsertMutation (or upsert or update of course), whatever we want to select() in the third parameter, it returns an array. A .single() method exists in the supabase query also on the insert which enables the possibility to not return an array but an object directly.
To Reproduce
In the following query, we can see the insertNewTeam which is used only to insert a single row, and so using normally a .single() in the insert is perfect, so that the value in the data return doesn't need to use a data[0] which is hugly.
const { mutateAsync: insertNewTeamMember } = useInsertMutation(
createClient().from("teams_members"),
["id_user", "id_team", "permission"],
"id_user, id_team, permission",
);
const { mutateAsync: insertNewTeam } = useInsertMutation(
createClient().from("teams"),
["id_owner", "name", "id"],
"id, name, id_owner",
);
const handleInsertNewPersonnalTeam = async () => {
if (!dataSession?.userId) {
return null;
}
insertNewTeam([{ id_owner: dataSession.userId, name: "Main Team" }], {
onSuccess: (data, variables) => {
if (!variables[0].id_owner) {
throw new Error("No user id");
}
//@ts-ignore
if (!data[0].id) {
throw new Error("No team id");
}
//@ts-ignore
insertNewTeamMember([
{
id_user: variables[0].id_owner,
//@ts-ignore
id_team: data[0].id,
permission: "full_access",
},
]);
},
});
};
Expected behavior Have the possibility to use .single() or .maybeSingle() method probably or at least an option "single" in the useInsert, useUpdate, etc.
Additional context Maybe this refine handling dataProvider could help think about the best way to achieve it : https://github.com/refinedev/refine/blob/master/packages/supabase/src/dataProvider/index.ts
The tag should be changed to "enhancement" but I can't, sorry for the mistake
yes this is on purpose, because it was much easier to get the types working for the array case only. changed the label to enhancement to reflect the request.
@psteinroe Yes I totally understand as we can achieve quite everything with arrays, but is there a specific reason why the updateMutation is receiving an object and not an array so ?
because cache helpers operates on primary keys only, so you can only update one at a time per request. its different for upsert and insert, because you pass the primary keys in the input.
Got it, thanks so much for explaining this in detail!