Injecting custom data into document events

I’d like to use document events as a change log for certain collections. I get that using Insert() allows you to manipulate document history, but in this case I’d want an additional property (‘editedBy’ for eg.) to be injected at transaction time, and ideally sit outside the data prop of the event and for sure not reflect in the document, only in the event. Similar to ‘ts’ and ‘action’.

Anyone have a clever way of doing this? I’d really love to not have to roll my own change log mechanism if I can leverage Events, which has almost everything I need.

Hi @jacobfvanzyl,

This is an interesting use of the document history. I would think your safest way to do this would be with custom UDFs that perform the desired function (Create(), Update(), Delete()), then make the update to the history document once those complete. Then you’d call CustomCreate() (for example) when normally you’d call Create().

If you’re using the document history in this way you might want a custom Remove() function, as well; or only allow very limited access via ABAC to that function so that no one accidentally, or maliciously, removes the document history you’re depending on.

One thing to bare in mind with this approach: as your history increases, the read operations on that collection and any associated indexes will increase, as well. This is because an index needs to read through the history of any documents associated with it to make sure it’s getting the right one(s). So you might instead opt to still use custom functions to record the operation, ts, and editedBy, but do so in a dedicated collection instead of the history. That would avoid those issues with large history collections. It would also potentially allow for much shorter history retention, since the audit data would now be stored separate from it and would not be in danger of being garbage collected.

Cory

Hi @jacobfvanzyl,

Do you still need any help on this question, or did the previous solve it for you? Please let us know either way.

Thanks,
Cory