keeper/server/viewinsert.js

118 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-03-21 16:27:14 +00:00
var nano = require('nano')('http://localhost:5984');
2016-03-21 22:27:51 +00:00
var logger = require('log4js').getLogger();
2016-03-21 16:27:14 +00:00
var db_name = 'keeper';
var keeper = nano.use(db_name);
2016-03-21 22:27:51 +00:00
var tableList = [
{
name: '_design/titles', view: {
"titles": {
"map": function(doc) { emit(null, doc.title); }
2016-03-21 16:50:23 +00:00
}
2016-03-21 22:27:51 +00:00
}
}
, {
name: '_design/reducedView', view: {
"reducedView": {
"map": function(doc) { emit(null, [doc.title, doc.reduced]); }
}
2016-03-21 16:50:23 +00:00
}
2016-03-21 22:27:51 +00:00
}, {
name: '_design/taglist', view: {
2016-03-21 22:37:36 +00:00
"taglist": {
2016-03-21 22:27:51 +00:00
"map": function(doc) { if (doc.type == 1) { emit(null, doc); } }
2016-03-21 16:50:23 +00:00
}
2016-03-21 22:37:36 +00:00
}}, {
2016-03-21 22:27:51 +00:00
name: '_design/getAllTags', view: {
2016-03-21 22:37:36 +00:00
"getAllTags": {
2016-03-21 22:27:51 +00:00
"map": function(doc) {
2016-03-21 22:37:36 +00:00
if (doc.tags.list.length > 0) {
emit(null, doc.tags.list);
}
}
}
2016-03-21 16:50:23 +00:00
}
2016-03-21 22:27:51 +00:00
}, {
2016-03-21 22:37:36 +00:00
name: '_design/getTagByKey', view:{
"getTagByKey": {
2016-03-21 22:27:51 +00:00
"map": function(doc) {
2016-03-21 22:37:36 +00:00
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]);
}
}
}
}
2016-03-21 16:50:23 +00:00
}
2016-03-21 22:27:51 +00:00
}
];
var killTable = (table) =>
{
2016-03-21 16:47:08 +00:00
2016-03-21 22:27:51 +00:00
console.log(table.name);
keeper.get(table.name, {revs_info: true}, function(err, body) {
if (err)
console.log(err);
2016-03-21 16:57:17 +00:00
2016-03-21 22:27:51 +00:00
if (typeof body !== 'undefined') {
2016-03-21 16:47:08 +00:00
2016-03-21 22:27:51 +00:00
keeper.destroy(table.name, body._rev, function(_err, _body) {
if (err) {
console.log(_err);
2016-03-21 16:51:50 +00:00
}
2016-03-21 22:27:51 +00:00
else {
console.log(table.name + ' deleted');
console.log(_body);
2016-03-21 16:47:08 +00:00
2016-03-21 16:40:15 +00:00
}
2016-03-21 22:27:51 +00:00
});
}
});
};
2016-03-21 16:40:15 +00:00
2016-03-21 22:27:51 +00:00
var updateTable = (table) =>
{
2016-03-21 16:27:14 +00:00
2016-03-21 22:27:51 +00:00
logger.info(table.name);
keeper.get(table.name, {revs_info: true}, function(err, body) {
if (err)
logger.error(body);
2016-03-21 16:40:15 +00:00
2016-03-21 22:27:51 +00:00
if (typeof body !== 'undefined') {
2016-03-21 16:27:14 +00:00
2016-03-21 22:49:42 +00:00
logger.debug(body);
2016-03-21 22:27:51 +00:00
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);
2016-03-21 16:40:15 +00:00
2016-03-21 16:27:14 +00:00
}
2016-03-21 22:27:51 +00:00
});
}
else {
keeper.insert({
"views": table.view
}, table.name, function(error, response) {
2016-03-21 16:40:15 +00:00
if (error) {
2016-03-21 22:27:51 +00:00
logger.error(error);
2016-03-21 16:40:15 +00:00
}
2016-03-21 22:27:51 +00:00
else {
logger.info(response);
}
});
}
});
};
2016-03-21 16:27:14 +00:00
2016-03-21 22:27:51 +00:00
for (var t = 0; t < tableList.length; t++) {
updateTable(tableList[t]);
}