OrientDB通过触发自动记录变更记录 [英] OrientDB automatic on-record changelog via trigger

查看:587
本文介绍了OrientDB通过触发自动记录变更记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长时间潜伏,但第一个问题。



最后想出了如何通过触发器在这个问题上维护一个changelog。
在OrientDB中嵌入函数中的记录
但是我也想保持用户更新记录,或者其他元数据,比如哪些道具被更新。



我的用例是使用带有JSON数组命令的HTTP请求将批量的upserts从c#传递到一些基类,如下所示:

  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

有没有什么方法来访问触发器中的数据?是否有使用风险

  orient.getGraphNoTx()

提前感谢!



编辑

我想出了如何在我的答案中使用策略来维护一个非常强大的changelog,但我仍然不完全清楚getGraphNoTx()的影响,我似乎必须使用。在包含更新的事务中触发动态挂钩?

解决方案

d post,以防任何人想要实现相同。



1)将更改日志默认为创建记录



,我只是缺少,你需要@class和@type为DEFAULT,但只有相关字段ADD。
新行:

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

2)陷阱更新用户和更新的字段
$ b

通过向更新的记录添加一个无模式的道具,从JS函数对象获取它,解决了这个问题

  doc.field(updater)

记录级道具。如果只传递正在更新的字段,这也适用于更新的字段。完整函数创建SQL在下面(仅更新,不更新字段)

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



我找不到证明getGraphNoTx()是安全的,但我的理解是,它只是阻止函数本身作为一个事务运行。最坏的情况似乎是更改日志条目收到错误更新?


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?

解决方案

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?

这篇关于OrientDB通过触发自动记录变更记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆