Skip to content
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

single() on insert doesn't seem possible #510

Open
damien-schneider opened this issue Oct 28, 2024 · 4 comments
Open

single() on insert doesn't seem possible #510

damien-schneider opened this issue Oct 28, 2024 · 4 comments
Labels
enhancement New feature or request

Comments

@damien-schneider
Copy link
Contributor

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

@damien-schneider damien-schneider added the bug Something isn't working label Oct 28, 2024
@damien-schneider
Copy link
Contributor Author

The tag should be changed to "enhancement" but I can't, sorry for the mistake

@psteinroe psteinroe added enhancement New feature or request and removed bug Something isn't working labels Nov 15, 2024
@psteinroe
Copy link
Owner

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
Copy link
Owner

@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.

@damien-schneider
Copy link
Contributor Author

Got it, thanks so much for explaining this in detail!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants