UNPKG

ernest

Version:

Web framework for HTTP and HTTPS, using ExpressJS, Session, Mongo, Socket IO, Redis

367 lines (332 loc) 6.44 kB
"use strict"; var mongojs = require('mongojs'); var str_changedat = 'changedAt'; var str_changedby = 'changedBy'; var str_insertdby = 'insertedBy'; var str_insertdat = 'insertedAt'; var str_set = '$set'; var str_action = 'action'; var str_updated = 'updated'; class ErnestDB { constructor(db_name) { this.db = mongojs(db_name); this.admin = null; }; setAdmin(adminDB) { this.admin = mongojs(adminDB); }; rsStatus(callback) { this.AdminCommand({replSetGetStatus:1},function(e,r) { callback(e,r); }); }; ServerStatus(callback) { this.AdminCommand({serverStatus:1},function(e,r) { callback(e,r); }); }; AdminCommand(command,callback) { var _this = this; if(_this.admin == null) { callback({error: true, msg:"No admin DB configured"},null); } else { this.admin.runCommand(command,function(e,r) { callback(e,r); }); } }; DBCommand(command,callback) { this.db.runCommand(command,function(e,r) { callback(e,r); }); }; InsertInCollection (data,collec,callback) { var col = this.GetCollection(collec); data[str_insertdat] = new Date(); col.insert(data,function(e,d) { callback(e,d); }); } InsertManyCollection (data,collec,callback) { var col = this.GetCollection(collec); col.insert(data,function(e,docs) { callback(e,docs); }); }; InsertInCollectionBy (data,collec,insertedBy,callback) { var col = this.GetCollection(collec); data[str_insertdby] = insertedBy; data[str_insertdat] = new Date(); col.insert(data,function(e,d) { callback(e,d); }); }; FindOneInCollection (data,collec,callback) { var col = this.GetCollection(collec);col.findOne(data,function(e,doc) { callback(e,doc); }); } FindInCollection (data,collec,callback) { var col = this.GetCollection(collec);col.find(data,function(e,docs) { callback(e,docs); }); } FindProyectInCollection (data,proy,collec,callback) { var col = this.GetCollection(collec);col.find(data,proy,function(e,docs) { callback(e,docs); }); } FindOneInCollShow (data,show,collec,callback) { var col = this.GetCollection(collec);col.findOne(data,show,function(e,doc) { callback(e,doc); }); } FindInCollecShow (data,show,collec,callback) { var col = this.GetCollection(collec); col.find(data,show,function(e,docs) { callback(e,docs); });} UpdateOneinCollec (crit,set,collec,callback) { var col = this.GetCollection(collec); col.update(crit,set,function(e,d) { callback(e,d); }); } UpdateOneinCollecBy (crit,set,collec,changedBy,callback) { set[str_set][str_changedat] = currentday; set[str_set][str_changedby] = changedBy; set[str_set][str_action] = str_updated; col.update(crit,set,function(e,d) { callback(e,d); }); } UpdateManyinCollec (crit,set,collec,callback) { var col = this.GetCollection(collec); col.update(crit,set,{multi:true},function(e,d) { callback(e,d); }); } DeleteFromCollection (crit,collec,callback) { var col = this.GetCollection(collec); col.remove(crit,function(e,d) { callback(e,d); }); } FindSortLimInCollect (crit,order,lim,collec,callback) { var col = this.GetCollection(collec); col.find(crit).sort(order).limit(lim,function (e,d) { callback(e,d); }); }; FindSortInCollection (crit,order,collec,callback) { var col = this.GetCollection(collec); col.find(crit).sort(order,function (err, docs) { callback(err,docs); }); } AggregatetoArray (agg_arry,collec,callback) { var col = this.GetCollection(collec); col.aggregate(agg_arry).toArray(function(err,docs) { callback(err,docs); }); }; GetDistincts (crit,query,collec,callback) { var col = this.GetCollection(collec); col.distinct(crit,query,function(err,docs) { callback(err,docs); }); }; getCollectionNames (callback) { this.db.getCollectionNames(function(err,colls) { callback(err,colls); }); }; CollectionExist(collec,callback) { this.db.getCollectionNames(function(e,colls) { if(e) { callback(e,null); } else { var r = false; var till = colls.length -1; if(till >= 0) { colls.map(function(c,index) { if(c == collec) { r = true; }; if(till == index) { callback(null,r); }; }); } else { callback(null,r); }; }; }); }; GetCollection (collec) { var collection = this.db.collection(collec); return collection; } RenameCollection (oldcollec,newcollec,callback) { var old_collection = this.GetCollection(oldcollec); old_collection.rename(newcollec,function(e,d) { callback(e,d); }); } CreateCollection (collec,callback) { this.db.createCollection(collec,function(e,d) { callback(e,d); }); } DropCollection (collec,callback) { var coll = this.GetCollection(collec); coll.drop(function(e,d) { callback(e,d); }); } EmptyCollection (collec,callback) { var coll = this.GetCollection(collec); coll.remove({},function(e,d) { callback(e,d); }); } EraseCollections (erase,callback) { var _this = this; var error = null; var result = true; this.db.getCollectionNames(function(e,colls) { if(e) { callback(e,null); } else { var length = colls.length; var till = length -1; var cnt = 0 ; colls.forEach(function(coll) { if(!error) { if(coll.indexOf(erase) == 0) { _this.DropCollection(coll,function(e1,r) { if(e1) { error = e1; result = null; }; }); } } if(cnt == till) { callback(error,result); }; cnt++; }); }; }); }; CloneCollection (src,dst,callback) { var collec_src = this.GetCollection(src); this.CreateCollection(dst,function(e,d) { if(e) { callback(e,d); } else { collec_src.find({},function(e1,docs) { if(e1) { callback(e1,docs); } else { var collec_dst = this.GetCollection(dst); collec_dst.insert(docs,function(e2,ndocs) { callback(e2,ndocs); }); } }); } }); } }; module.exports = ErnestDB;