未触发DocumentDb中的触发器? [英] Trigger in DocumentDb not fired?

查看:65
本文介绍了未触发DocumentDb中的触发器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在DocumentDB集合中创建了一个简单的Pre Trigger.

I have created a simple Pre Trigger in my DocumentDB collection.

function testTrigger() {
    var context = getContext();
    var request = context.getRequest();
    var documentToCreate = request.getBody();
    documentToCreate["test"] = "Added by trigger";
    request.setBody(documentToCreate);
}

尽管看来,此触发器根本没有触发.

Though it seems, this trigger isn't fired at all.

令人不快的是,脚本浏览器中的getContext()调用用绿色的波浪线标记.

What's also irritating, the getContext() call is marked with a green squiggly line in the script explorer.

推荐答案

触发器似乎没有...好...被触发的问题是,您没有在触发操作中指定它.触发器不是自动的.必须为每个要执行它们的数据库操作指定它们.

The problem that the trigger doesn't seem to be... well... triggered, is that you aren't specifying it on the triggering operation. Triggers are not automatic. They must be specified for each database operation where you want them executed.

我个人认为这是一个不幸的设计选择,我决定在自己的代码中根本不使用触发器.相反,我将所有业务规则约束都嵌入了存储过程中.我发现这样做会更清洁一些,并且消除了我忘记指定触发器或(更有可能)其他人向数据库编写代码的人甚至都不知道应该这样做的担心.我意识到有人可以通过直接进入文档来绕过我的存储过程并违反业务规则,但是我不知道DocumentDB中有什么方法可以防止这种情况发生.如果有一种方法可以使触发器自动执行,那将可以防止这种情况发生.

Personally, I think that was a unfortunate design choice and I've decided to not use triggers at all in my own code. Rather, I embed all the business rule constraints in stored procedures. I find this to be a bit cleaner and removes the worry that I'll forget to specify a trigger or (more likely) someone else writing code to the database won't even know that they should. I realize that someone can bypass my stored procedure and violate the business rules by going directly to the documents, but I don't know of any way in DocumentDB to protect against that. If there was a way to make triggers be automatic, that would have protected against that.

使用REST API时,可以在标头中使用x-ms-documentdb-pre-trigger-includex-ms-documentdb-post-trigger-include指定触发器.在node.js SDK中,您可以使用RequestOptions preTriggerIncludepreTriggerInclude属性.对于.NET,您可以这样指定它:

When using the REST API, you specify triggers in a header with x-ms-documentdb-pre-trigger-include or x-ms-documentdb-post-trigger-include. In the node.js SDK, you use the RequestOptions preTriggerInclude or preTriggerInclude property. For .NET, you specify it like this:

Document createdItem = await client.CreateDocumentAsync(collection.SelfLink, new Document { Id = "documentdb" },
new RequestOptions
{
    PreTriggerInclude = new List<string> { "CapitalizeName" },
});

查看有关绿色问题的评论.

See comments for discussion on the green squiggly.

这篇关于未触发DocumentDb中的触发器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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