OrientDB automatic on-record changelog via trigger

吃可爱长大的小学妹 提交于 2020-01-07 02:54:49

问题


long time lurker but first question.

Finally figured out how to maintain a changelog by trigger on this question Embedding record from function in OrientDB But I'd also like to maintain the user updating the record, and maybe other metadata like which props were updated.

My use case is passing batches of upserts from c# using HTTP requests with a JSON array of commands, into some base classes as follows:

ALTER CLASS V SUPERCLASS +OTriggered

CREATE CLASS ChangeHistory ABSTRACT 
CREATE PROPERTY ChangeHistory.user LINK
CREATE PROPERTY ChangeHistory.changetype STRING
CREATE PROPERTY ChangeHistory.timestamp DATETIME
ALTER PROPERTY ChangeHistory.timestamp DEFAULT sysdate()

CREATE PROPERTY V.changelog EMBEDDEDLIST ChangeHistory 
ALTER PROPERTY V.changelog DEFAULT {“changetype”:”initial creation”}
ALTER CLASS V CUSTOM onAfterUpdate=recordUpdated

Is there any way to access that data in triggers? And is there any risk of using

orient.getGraphNoTx()

Thanks in advance!

Edit

I figured out how to maintain a pretty robust changelog using the strategy in my answer, but I'm still not totally clear on the implications of getGraphNoTx() which I seem to have to use. Do dynamic hooks fire in a transaction that includes the update?


回答1:


OK so I've gotten a bit further and thought I'd post in case anyone else wants to achieve the same.

1) defaulting the changelog to "record created"

This is possible, I was just missing that you need @class and @type to DEFAULT, but only relevant fields to ADD. New line:

ALTER PROPERTY E.changelog DEFAULT [{'@type':'d','@class':'ChangeHistory', 'changetype':'initial creation'}];

2) trapping update user and updated fields

Solved this one too, by adding a schemaless prop to the updating record, getting it from the JS function object

doc.field("updater")

and passing this to the changelog before removing the record-level prop. This also works for the updated fields, if you pass in only the fields which are being updated. The full function creation SQL is below (updater only, not updated fields)

CREATE FUNCTION recordUpdated "
var graph = orient.getGraphNoTx(); 
var updater = graph.command( 'sql', 'SELECT FROM OUser WHERE name = \"' + doc.field('updater') + '\"'); 
graph.command( 'sql', 'UPDATE ' + doc.field('@rid') + ' ADD changelog = [{\"user\":\"' + updater[0].getId() + '\", \"changetype\":\"Update\"}]');
graph.command( 'sql', 'UPDATE ' + doc.field('@class') + ' REMOVE updater WHERE @rid = \"' + doc.field('@rid') + '\"');
" LANGUAGE JAVASCRIPT;

3) getGraphNoTx()

I could not find proof of getGraphNoTx() being safe, but my understanding is that it just prevents the function itself from running as a transaction. worst case scenario seems to be that changelog entries recieve the wrong updater?



来源:https://stackoverflow.com/questions/35663550/orientdb-automatic-on-record-changelog-via-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!