-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload-cloudantp.js
61 lines (41 loc) · 1.75 KB
/
upload-cloudantp.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
/*
Upload the contents of a JSON file to Cloudant.
*/
var config = require('./config.json');
var cloudant = require('cloudant-promise')({ account: config.cloudant.account, password: config.cloudant.password });
var Promise = require('bluebird');
/* jshint esnext:true */
exports.upload = function(json, sourceurl) {
var db = cloudant.use(config.cloudant.database);
json._id = sourceurl; // is this a good idea?
return db.insert(json);
/*if (!err)
console.log('Uploaded ' + sourceurl);
else
console.log('**' + err.reason);*/
};
// if we want to update an existing feature, we need to first fetch the old one, and set _rev to it
exports.upsert = function(json, sourceurl) {
var db = cloudant.use(config.cloudant.database);
//console.log(JSON.stringify(json).yellow);
// would prefer to use fetchRevs, new in v6?
return Promise.resolve(db.head(sourceurl))
.then(headers => {
json._rev = headers[1].etag.replace(/"/g, ''); // really weird, the revids are wrapped in double quotes.
//console.log('Upserting: ' + sourceurl);
//console.log(json._rev);
}).catch((e) => {
// we just want to catch 404s (no existing document to replace), but we're ignoring all errors. TODO
if (!json._rev) {
//console.warn('No existing document');
}
json._id = sourceurl;
//console.log(json);
return db.insert(json);
});
};
/*
How to do bulk upsert
Can fetch all revids: https://opencouncildata.cloudant.com/test1/_design/features/_view/byTopic?limit=5&include_docs=false&reduce=false&foo=1
Then bulk update: https://docs.cloudant.com/document.html#bulk-operations
*/