paralect / node-mongo

Node Mongo — is reactive 🚀 extension to MongoDB API
https://github.com/paralect/node-mongo
23 stars 7 forks source link

Feature: Soft Remove #21

Closed alldayalone closed 4 years ago

alldayalone commented 4 years ago
  1. Add option { soft: bool } (default false) to the createService options which opts on the feature
  2. On remove document makes update { deletedOn: new Date() }
  3. Add function restore which makes update { $unset: { deletedOn: "" } }
  4. Add option { soft: bool } (default true) to all query methods which adds { deletedOn: { $exists: false } } to the query

To discuss:

jqueryisamonad commented 4 years ago

I think we shouldn't add application specific logic to library. Especially since you can define custom service methods and use them instead of usual methods:

// db.js

db.setServiceMethod('softRemove', (service, query) => {
  return service.atomic.update(
    query,
    { $set: { deletedOn: new Date() } },
    { multi: true },
  );
});

db.setServiceMethod('softRestore', (service, query) => {
  return service.atomic.update(
    query,
    { $unset: { deletedOn: '' } },
    { multi: true },
  );
});

db.setServiceMethod('softFind', (service, query) => {
  return service.find({ ...query, deletedOn: { $exists: false } });
});