-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (73 loc) · 2.1 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
function upsertFn(db) {
function upsert(docId, transform, callback) {
if (!transform) {
return callback(
'transform missing. signature is: upsert(docId, transform, callback)'
);
}
const MSG_INVALID_DOC = 'Error: caller did not supply a valid doc';
const transformDoc = curDoc => {
const id = curDoc && (curDoc._id || curDoc.id);
const rev = curDoc && (curDoc._rev || curDoc.rev);
const doc = transform(curDoc);
if (doc) {
doc._id = id || doc._id || undefined;
doc._rev = rev || doc._rev || undefined;
}
return doc;
};
const mergeIdRev = (doc, inserted) => {
doc._id = (inserted && (inserted.id || inserted._id)) || doc._id;
doc._rev = (inserted && (inserted.rev || inserted._rev)) || doc._rev;
return doc;
};
const promiseHelper = curDoc => {
const doc = transformDoc(curDoc);
if (doc) {
return db.insert(doc, doc._id).then(data => mergeIdRev(doc, data));
} else {
return Promise.reject(MSG_INVALID_DOC);
}
};
const callbackHelper = curDoc => {
const doc = transformDoc(curDoc);
if (doc) {
db.insert(doc, doc._id, function(err, data) {
if (err) callback(err);
else callback(null, mergeIdRev(doc, data));
});
} else {
callback(MSG_INVALID_DOC);
}
};
if (callback) {
if (!docId) callbackHelper();
else {
db.get(docId, { include_docs: true }, (err, body) => {
if (err && err.statusCode !== 404) callback(err);
else callbackHelper(body);
});
}
} else {
return !docId
? promiseHelper()
: db
.get(docId, { include_docs: true })
.catch(e => {
if (e.statusCode === 404) return null;
throw e;
})
.then(promiseHelper);
}
}
return upsert;
}
module.exports = function(cloudant) {
var use = cloudant.db.use;
cloudant.db.use = function(dbName) {
var db = use(dbName);
db.upsert = upsertFn(db);
return db;
};
return cloudant;
};