This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMongoDBHandler.js
87 lines (83 loc) · 2.81 KB
/
MongoDBHandler.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
82
83
84
85
86
87
// Retrieve
//var MongoDB = require('mongodb');
var config = require('./configuration');
var logger = require("./logger");
var mongo = require('mongodb');
var ptype = MonogoDBHandler.prototype;
function MonogoDBHandler(){
ptype.dbName = config.dbName;
ptype.dbServer = config.dbServer;
ptype.dbPort = config.dbPort;
__self = this;
mongo.MongoClient.connect("mongodb://" + this.dbServer + ":" + this.dbPort + "/" + this.dbName, function(err, db) {
if(!err) {
console.log("** DB is connected **");
____self.activeDBLink = db;
__self.newsCollection = db.collection("News");
__self.logsCollection = db.collection("logs");
__self.articlesWords = db.collection("articlesWords");
if(__self.pendingArticlesForInsertion.length>0){
__self.insertArticlesArray(__self.pendingArticlesForInsertion);
}
}else{
console.log(err);
}
});
ptype.newsCollection = undefined;
ptype.pendingArticlesForInsertion = [];
}
ptype.insertArticleWords = function(articleWords){
if (this.activeDBLink != undefined) {
this.articlesWords.insert(articleWords,function(err, result) {
if(err){
logger.log("WordsCounterDBError",err);
}
});
}
}
ptype.getArticlesForProcessing = function (entryCallback, completeCallback){
if (this.activeDBLink != undefined && entryCallback != undefined) {
this.newsCollection.find({processed:false},function(err,res){
if(err){
console.log(res);
}else{
res.toArray(function(err,res){
res.forEach(function(item,index){
entryCallback(item);
});
if(completeCallback!=undefined){
completeCallback();
}
});
}
});
}
}
ptype.insertArticlesArray = function (arrOfArticles) {
if (this.activeDBLink != undefined) {
this.newsCollection.insert(arrOfArticles,function(err, result) {
if(err){
logger.log("DBError",err);
}
});
}else{
console.log("we will wait");
this.pendingArticlesForInsertion = this.pendingArticlesForInsertion.concat(arrOfArticles);
}
}
//ptype.insertLog = function (log) {
// if (this.activeDBLink != undefined) {
// this.logsCollection.insert(log,function(err, result) {
// if(err)
// logger.log("DBError",err);
// });
// }else{
// console.log("Log is passed because DB is not ready");
// }
//}
ptype.closeDB = function (){
if(__self.activeDBLink)
__self.activeDBLink.close();
}
module.exports = MonogoDBHandler;
module.exports.db = new MonogoDBHandler();