feathers-hooks-common icon indicating copy to clipboard operation
feathers-hooks-common copied to clipboard

Support batch remove when using softDelete() hook

Open miguelrk opened this issue 2 years ago • 2 comments
trafficstars

Is there a way to use the softDelete hook for batch requests?

I found out deleting multiple records at once via batch remove does nothing, the workaround is simply making multiple (individual) remove calls.

Steps to reproduce

// DOES NOT WORK: batch/multi-remove by softDelete()
app.service('files').remove(null, params)

// WORKS: less-performant workaround
const files = await app.service('files').find({ 
  ...params,
  paginate: false
})
await Promise.all(files.map(({ _id ) => app.service('files').remove(_id)))

The softDelete hook is configured as follows:

softDelete({
  // returns query object to spread in    query
  deletedQuery: async (ctx: HookContext) => {
    const param = ctx.params.query?.deletedAt
    const isTruthy = ['true', true].includes(param)
    return { deletedAt: truthy ? { $ne: null } : null}
  },
  // returns patch data to spread in data for remove
  removeData: async (ctx: HookContext) => ({               deletedAt: new Date().toISOString() })
  })

Expected behavior

Files should be set to new Date().toISOString() when deleting one, or multiple in batch.

Actual behavior

Only files deleted individually are set to new Date().toISOString().

miguelrk avatar Jan 06 '23 00:01 miguelrk

I never used the softDelete hook myself. On my setups, feathers-sequelize handles this out of the box.

So I'm not planning to work on that but a PR is highly appreciated.

Please let me know, if you're up to work on this and also contact me, if you need guidance. 😊

fratzinger avatar Jan 06 '23 10:01 fratzinger