/
upsertOptimizedForReplace.js
59 lines (52 loc) · 2.68 KB
/
upsertOptimizedForReplace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* A DocumentDB stored procedure that upserts a given document (insert new or update if present) using its id property.<br/>
* This implementation queries for the document's id, and creates if absent and replaces if found.
* Use this sproc if replaces are more common than creates, otherwise use "upsert"
*
* @function
* @param {Object} document - A document that should be upserted into this collection.
* @returns {Object.<string>} Returns an object with the property:<br/>
* op - created (or) replaced.
*/
var upsertOptimizedForReplace = {
id: "upsertOptimizedForReplace",
body: function (document) {
var context = getContext();
var collection = context.getCollection();
var collectionLink = collection.getSelfLink();
var response = context.getResponse();
// Not checking for existence of document.id for compatibility with createDocument.
if (!document) throw new Error("The document is undefined or null.");
retrieveDoc(document, null, callback);
function retrieveDoc(doc, continuation, callback) {
var query = { query: "select * from root r where r.id = @id", parameters: [ {name: "@id", value: doc.id}]};
var requestOptions = { continuation : continuation };
var isAccepted = collection.queryDocuments(collectionLink, query, requestOptions, function(err, retrievedDocs, responseOptions) {
if (err) throw err;
if (retrievedDocs.length > 0) {
tryReplace(retrievedDocs[0], doc, callback);
} else if (responseOptions.continuation) {
// Conservative check for continuation. Not expected to hit in practice for the "id query".
retrieveDoc(doc, responseOptions.continuation, callback);
} else {
tryCreate(doc, callback);
}
});
if (!isAccepted) throw new Error("Unable to query documents");
}
function tryCreate(doc, callback) {
var isAccepted = collection.createDocument(collectionLink, doc, callback);
if (!isAccepted) throw new Error("Unable to schedule create document");
response.setBody({"op": "created"});
}
function tryReplace(docToReplace, docContent, callback) {
var isAccepted = collection.replaceDocument(docToReplace._self, docContent, callback);
if (!isAccepted) throw new Error("Unable to schedule replace document");
response.setBody({"op": "replaced"});
}
function callback(err, doc, options) {
if (err) throw err;
}
}
}
module.exports = upsertOptimizedForReplace;