Skip to content

Support batch remove when using softDelete() hook #701

Open
@miguelrk

Description

@miguelrk

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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions