refine icon indicating copy to clipboard operation
refine copied to clipboard

[FEAT] - filters in useCustomMutation

Open rodbs opened this issue 1 year ago • 7 comments

Is your feature request related to a problem? Please describe.

I want to send custom mutation queries based on specific fields (not on id) DELETE FROM your_table WHERE last_updated < 'some_date';

Describe alternatives you've considered

No idea how to send custom mutation queries (apart form hard-coding it on the data provider)

Additional context

No response

Describe the thing to improve

I'd like to have filters on useCustomMutation (the same way as in useCustom) to be able to specify conditions for UPDATEs and DELETEs

rodbs avatar Dec 19 '23 23:12 rodbs

Hey @rodbs you can send extra params to your data provider through meta field, and utilize these fields there.

I think it would still be nice to accept filter params to hooks such as useUpdateMany and useDeleteMany. cc @aliemir

BatuhanW avatar Dec 20 '23 07:12 BatuhanW

We are open to contributions for this issue!

Basically, we need to accept optional filter parameter to useUpdateMany, useDeleteMany hooks and pass them to the data providers.

After this, data providers can be revised to include filters in their requests.

BatuhanW avatar Jan 10 '24 08:01 BatuhanW

I am up for this

Vikrant-Khedkar avatar Feb 16 '24 06:02 Vikrant-Khedkar

Hi I am interested as well! Would like to attempt this if there is no PR in one week 🙏🙏

ziqing26 avatar Feb 17 '24 16:02 ziqing26

@BatuhanW Can I work on this?

beg1c avatar Feb 29 '24 13:02 beg1c

Hey @beg1c, sure. Assigned this issue to you 🙌

BatuhanW avatar Feb 29 '24 14:02 BatuhanW

@BatuhanW I looked into this a bit but I am a little bit confused. If we extend DeleteManyParams with filters, we end up with:

export interface DeleteManyParams<TVariables = {}> {
  resource: string;
  ids: BaseKey[];
  filters?: CrudFilters;
  variables?: TVariables;
  meta?: MetaQuery;
  metaData?: MetaQuery;
}

Since ids can't be undefined, we would still have to select all known ids and send them with filters to back-end. That could be handled on back-end as ids could be ignored and deletion would be performed by filters.

Problem arises if deleteMany method is not implemented in our dataProvider, useDeleteMany will execute a lot of requests with deleteOne and that could cause problems.

const mutationFn = () => {
  if (selectedDataProvider.deleteMany) {
    return selectedDataProvider.deleteMany<TData, TVariables>({
      resource: resource.name,
      ids,
      filters: filters,
      meta: combinedMeta,
      metaData: combinedMeta,
      variables: values,
    });
  }
  return handleMultiple(
    ids.map((id) =>
      selectedDataProvider.deleteOne<TData, TVariables>({
        resource: resource.name,
        id,
        filters: filters,
        meta: combinedMeta,
        metaData: combinedMeta,
        variables: values,
      }),
    ),
  );
};

Should we force deleteMany if filters are present so that user can, for example, delete every inactive product?

beg1c avatar Mar 01 '24 00:03 beg1c

Hey @beg1c, you are right. Initially, we thought that it should makes sense, but we've discussed again, and since users are explicitly passing ids, filters would be redundant.

BatuhanW avatar Apr 17 '24 07:04 BatuhanW