Azure CosmosDb 存储过程 IfMatch 谓词
Azure CosmosDb Stored Procedure IfMatch Predicate
在 DocDb 存储过程中,作为检索我正在变异的数据的过程的第一步,我读取并使用数据,前提是它与 etag 匹配,如下所示:
collection.readDocument(reqSelf, function(err, doc) {
if (doc._etag == requestEtag) {
// Success - want to update
} else {
// CURRENTLY: Discard the read result I just paid lots of RUs to read
// IDEALLY: check whether response `options` or similar indicates retrieval
was skipped due to doc not being present with that etag anymore
...
// ... Continue with an alternate strategy
}
});
有没有办法将 options
传递给 readDocument
调用,以便通知回调 "It's changed so we didn't get it, as you requested"?
(我真正的问题是除了the readDocument
undocumentation in the js-server docs我找不到任何文档)
感谢@Nick Chapsas 的一些推动,this self-answer from @Redman 我发现在我的情况下我可以实现我的目标(通过 self-link 阅读当前文档,或者通过在存储过程中生成一个 Alt link 来替换它的更新版本具有相同的 id
),如下所示:
var docId = collection.getAltLink() + "/docs/"+req.id;
var isAccepted = collection.readDocument(docId, {}, function (err, doc, options) {
if (err) throw err;
// Will be null or not depending on whether it exists
executeUpsert(doc);
});
if (!isAccepted) throw new Error("readDocument not Accepted");
从技术上讲,您可以通过创建一个 responseOptions
对象并将其传递给调用来实现。
function sample(selfLink, requestEtag) {
var collection = getContext().getCollection();
var responseOptions = { accessCondition: { type: "IfMatch", condition: requestEtag } };
var isAccepted = collection.readDocument(selfLink, responseOptions, function(err, doc, options) {
if(err){
throw new Error('Error thrown. Check the status code for PreconditionFailed errors');
}
var response = getContext().getResponse();
response.setBody(doc);
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
但是,即使您提供的 etag 不是文档所具有的 etag,您也不会收到错误消息,并且会正确取回文档本身。只是不应该在存储过程中使用 readDocument
函数。
在 DocDb 存储过程中,作为检索我正在变异的数据的过程的第一步,我读取并使用数据,前提是它与 etag 匹配,如下所示:
collection.readDocument(reqSelf, function(err, doc) {
if (doc._etag == requestEtag) {
// Success - want to update
} else {
// CURRENTLY: Discard the read result I just paid lots of RUs to read
// IDEALLY: check whether response `options` or similar indicates retrieval
was skipped due to doc not being present with that etag anymore
...
// ... Continue with an alternate strategy
}
});
有没有办法将 options
传递给 readDocument
调用,以便通知回调 "It's changed so we didn't get it, as you requested"?
(我真正的问题是除了the readDocument
undocumentation in the js-server docs我找不到任何文档)
感谢@Nick Chapsas 的一些推动,this self-answer from @Redman 我发现在我的情况下我可以实现我的目标(通过 self-link 阅读当前文档,或者通过在存储过程中生成一个 Alt link 来替换它的更新版本具有相同的 id
),如下所示:
var docId = collection.getAltLink() + "/docs/"+req.id;
var isAccepted = collection.readDocument(docId, {}, function (err, doc, options) {
if (err) throw err;
// Will be null or not depending on whether it exists
executeUpsert(doc);
});
if (!isAccepted) throw new Error("readDocument not Accepted");
从技术上讲,您可以通过创建一个 responseOptions
对象并将其传递给调用来实现。
function sample(selfLink, requestEtag) {
var collection = getContext().getCollection();
var responseOptions = { accessCondition: { type: "IfMatch", condition: requestEtag } };
var isAccepted = collection.readDocument(selfLink, responseOptions, function(err, doc, options) {
if(err){
throw new Error('Error thrown. Check the status code for PreconditionFailed errors');
}
var response = getContext().getResponse();
response.setBody(doc);
});
if (!isAccepted) throw new Error('The query was not accepted by the server.');
}
但是,即使您提供的 etag 不是文档所具有的 etag,您也不会收到错误消息,并且会正确取回文档本身。只是不应该在存储过程中使用 readDocument
函数。