History, deletion and collection size

I have a couple of collections that have history_days set to null in order to maintain a document history that is used by our app. I was about to implement soft delete (or “trash”) functionality for those collection but then I realised that Delete actually merely adds a “delete” event to the collection/document history.

In theory it means that to get all “deleted” documents I need to get all “delete” events for the collection. And to restore the document I could delete the “delete” event.

A couple of things that came to mind though:

  • Does this mean that nothing is ever actually deleted from a collection that has history_days set to null?
  • Is this a sensible approach of a “soft-delete” like functionality?
  • Is there actually a way to get a list of events of a particular type? I’d assume it’s not trivial as you can’t really create an index on events
  • Am I missing something?

@alexeygolev

That is right. Document is marked Delete and from there on any Read operation (Get or Paginate) will not be able to fetch it. Having history_days to null can impact query performance if you have too many events on a document.

Yes, because you can always Remove the Delete event to make the document active again (Analogy of recovering a file back from Trash).

There is no built in function but here is an example to identify all documents that are deleted in a collection today ??

  1. Create an Index sorted asc(default) on ts field of the collection.
CreateCollection({ name: "offers"});

CreateIndex(
{
name: "offers_with_ts_asc",
source: Collection("offers"),
values: [{"field": ["ts"]}, {"field": ["ref"]}]
});
  1. Get the Events on this Index and Filter for action:delete and document
Map(Filter(
  Paginate(Match(Index("offers_with_ts_asc")), {
    events: true,
    size: 10000,
    after: ToMicros(ToTime(ToDate(Now())))
  }),
  Lambda("x", Equals(Select(["action"], Var("x")), "delete"))
), Lambda("x", Select(['document'], Var('x'))))

Hope this helps.

1 Like