var nano = require('nano')('http://localhost:5984'); var logger = require('log4js').getLogger(); var db_name = 'keeper'; var keeper = nano.use(db_name); var tableList = [ { name: '_design/titles', view: { "titles": { "map": function(doc) { emit(null, doc.title); } } } } , { name: '_design/reducedView', view: { "reducedView": { "map": function(doc) { emit(null, [doc.title, doc.reduced]); } } } }, { name: '_design/taglist', view: { "taglist": { "map": function(doc) { if (doc.type == 1) { emit(null, doc); } } } } }, { name: '_design/getAllTags', view: { "getAllTags": { "map": function(doc) { if (doc.tags.list.length > 0) { emit(null, doc.tags.list); } } } } }, { name: '_design/getTagByKey', view: { "getTagByKey": { "map": function(doc) { if (doc.tags.list.length > 0) { for (var t = 0; t < doc.tags.list.length; t++) { emit(doc._id, [doc.tags.list[t], doc.title]); } } } } } }, { name: '_design/pocketList', view: { "pocketList": { "map":function (doc) { emit(doc._id, {'host':doc.host, 'tn':doc.thumbnail, 'title':doc.title, 'nib':doc.nib, 'tags':doc.tags}); } } } } ]; var killTable = (table) => { console.log(table.name); keeper.get(table.name, {revs_info: true}, function(err, body) { if (err) console.log(err); if (typeof body !== 'undefined') { keeper.destroy(table.name, body._rev, function(_err, _body) { if (err) { console.log(_err); } else { console.log(table.name + ' deleted'); console.log(_body); } }); } }); }; var updateTable = (table) => { logger.info(table.name); keeper.get(table.name, {revs_info: true}, function(err, body) { if (err) logger.error(body); if (typeof body !== 'undefined') { logger.debug(body); keeper.insert({_id: body._id, _rev: body._rev, views: table.view}, function(_err, _body) { if (err) { logger.error(_err); } else { logger.info(table.name + ' updated'); logger.info(_body); } }); } else { keeper.insert({ "views": table.view }, table.name, function(error, response) { if (error) { logger.error(error); } else { logger.info(response); } }); } }); }; for (var t = 0; t < tableList.length; t++) { updateTable(tableList[t]); }