forked from IBM/node-nano-doc-updater
/
index.js
192 lines (165 loc) · 6.61 KB
/
index.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Licensed Materials - Property of IBM
* (c) Copyright IBM Corporation 2015, 2016. All Rights Reserved.
*
* Note to U.S. Government Users Restricted Rights:
* Use, duplication or disclosure restricted by GSA ADP Schedule
* Contract with IBM Corp.
*/
"use strict";
/*
* Performs an insert/update on a CouchDB document. Where a document
* with the provided ID already exists, optionally runs user defined
* functions for either skipping an update (based on the existing document)
* or merging.
*
*/
var cloudant = require('@otc-core/cloudant'),
extend = require("./lib/extend"),
omit = require("./lib/omit");
module.exports = function () {
return new NanoDocUpdater();
};
function NanoDocUpdater () {
this._cloudantService = null;
this._dbName = null;
this._isDesignDoc = null;
this._existingDoc = null;
this._newDoc = null;
this._shouldUpdate = null;
this._merge = null;
this._id = null;
this._shouldCreate = true;
}
(function () {
["cloudantService", "dbName", "isDesignDoc", "existingDoc", "newDoc", "id", "shouldUpdate", "shouldCreate", "merge"].forEach(function (f) {
NanoDocUpdater.prototype[f] = function (newValue) {
this["_" + f] = newValue;
return this;
};
}.bind(this));
NanoDocUpdater.prototype.update = function (callback) {
updateDocument(
this._existingDoc,
this._newDoc,
this._id,
this._cloudantService,
this._dbName,
this._isDesignDoc,
this._shouldUpdate,
this._shouldCreate,
this._merge,
callback
);
};
NanoDocUpdater.prototype.updateJob = function () {
var existingDoc = this._existingDoc;
var newDoc = this._newDoc;
var id = this._id;
var cloudantService = this._cloudantService;
var dbName = this._dbName;
var isDesignDoc = this._isDesignDoc;
var shouldUpdate = this._shouldUpdate;
var shouldCreate = this._shouldCreate;
var merge = this._merge;
return function (callback) {
updateDocument(
existingDoc,
newDoc,
id,
cloudantService,
dbName,
isDesignDoc,
shouldUpdate,
shouldCreate,
merge,
callback
);
};
};
}());
/* Says ``always do something'' */
function always(/*existing, newVer*/) {
return true;
}
/* Given two documents (an existing one and a new one) returns a new one.
*/
function useNewVersion(existing, newVer) {
return newVer;
}
/* Create-or-update a document: First fetch the existing revision, then
insert a new one. Optional f_ShouldUpdate and f_Merge functions
control the specifics of merging. */
async function updateDocument(existingDoc, newDoc, id, cloudantService, dbName, isDesignDoc, f_ShouldUpdate, shouldCreate, f_Merge, callback) {
if (!f_ShouldUpdate) {
f_ShouldUpdate = always;
}
if (!f_Merge) {
f_Merge = useNewVersion;
}
// If we never grabbed the existing document from the DB, fetch it and try again.
if (!existingDoc) {
var fn = isDesignDoc ? cloudant.getDesignDocument : cloudant.getDocument;
var r = await fn(cloudantService, dbName, id)
.catch(async function(err) {
if (err.message === "not_found") {
if (!shouldCreate) {
// Don't create a document where one didn't exist, if the user so desires.
return callback(null, null);
}
fn = isDesignDoc ? cloudant.createDesignDocument : cloudant.createDocument;
var r2 = await fn(cloudantService, dbName, id, newDoc)
.catch(function(err) {
// If someone beat us here, we should retry this whole operation.
if (err.message === "conflict") {
return updateDocument(null, newDoc, id, cloudantService, dbName, isDesignDoc, f_ShouldUpdate, shouldCreate, f_Merge, callback);
}
// If anything else happened, we should bail.
return callback("Could not fetch existing document: " + err.toString());
})
if (!r2) {
return; /* handled in the catch above */
}
// The insert worked. We're done.
return callback(null, extend({}, newDoc, {_rev: r2.rev}));
}
// If the fetch came back with any other error, bail.
return callback("Could not fetch existing document: " + err.toString());
})
if (!r) {
return; /* handled in the catch above */
}
// We got the document. Let's try the update again.
return updateDocument(r, newDoc, id, cloudantService, dbName, isDesignDoc, f_ShouldUpdate, shouldCreate, f_Merge, callback);
}
if (!f_ShouldUpdate(existingDoc, newDoc)) {
return callback(null, existingDoc);
}
// Before merging, record that we want to undelete a document that has been deleted.
var mergedDoc = f_Merge(omit(existingDoc, "_deleted"), newDoc);
if (!mergedDoc || typeof mergedDoc !== "object" || mergedDoc instanceof Error) {
// Allow a user defined merge algorithm to say ``sorry...can't merge'' by returning a non-object or Error.
// Bubble this error, so that the user can then test for it in their callback.
return callback(mergedDoc);
}
// It's essential that we specify a revision or this will never terminate.
// It's probably not a good idea to trust a user-specified merge function to do this.
mergedDoc = extend({}, mergedDoc, { _rev: existingDoc._rev });
// The document exists, and is out of date. We need to overwrite it.
fn = isDesignDocument ? cloudant.updateDesignDocument : cloudant.updateDocument;
var result = await fn(cloudantService, dbName, id, mergedDoc)
.catch(function(err) {
// If someone beat us here, we need to try the whole thing again.
if (err.message === "conflict") {
return updateDocument(null, newDoc, id, cloudantService, dbName, isDesignDoc, f_ShouldUpdate, shouldCreate, f_Merge, callback);
}
// If anything else happened, bail.
return callback("Could not update the document to the proposed version: " + err.toString());
})
if (result) {
// Update the new document's _rev
mergedDoc._rev = result.rev;
// The replacement worked. We're done. Finally!
return callback(null, mergedDoc);
}
}