-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.js
137 lines (121 loc) · 2.94 KB
/
model.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* this file does the work of communicating with the Mongo database.
*/
'use strict';
var SvcConfig = require('./config/config').SvcConfig;
var config = new SvcConfig();
var constants = require('./lib/constants');
var StatusResponse = require('./lib/statusResponse').StatusResponse;
var winston = require('winston');
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({'timestamp': true, level: config.loggingLevel})
]
});
var async = require('async');
var util = require('util');
var uautils = require('./lib/utils');
var server = config.replSet;
var mongoDb = require('mongodb').Db;
var BSON = require('mongodb').BSONPure;
var db = new mongoDb(config.dbName, server, {safe: true});
var _isInitialized = false;
function initDb(callback) {
logger.info('model.initDb');
var statusResponse;
if (!_isInitialized) {
db.open(function (err, db) {
if (!err) {
_isInitialized = true;
callback(err,{modelName:'model',dbName:config.dbName});
}
else {
statusResponse = new StatusResponse(constants.STATUS_ERROR, "System Error, please try again", constants.STATUS_CODE_UNSPECIFIED, null, err);
logger.error(JSON.stringify(statusResponse));
callback(err, {modelName:'mgoModel',dbName:config.dbName});
}
});
}
else {
callback(null);
}
}//
function isInitialized() {
return _isInitialized;
}
function initAllModels(callback){
async.parallel([
function(asyncCallback){
initDb(function(err,data){
asyncCallback(err, data);
});
}
],
function(err, aResults){
var oResult;
for (var i = 0; i < aResults.length; i++) {
oResult = aResults[i];
if (oResult) {
logger.info('model: ' + oResult.modelName + ', connected to, Mongo Db: ' + oResult.dbName);
}
}
callback(err, aResults);
}
);
}
function insertMsgLog(sServiceName, oInsert, callback){
var collName = 'msgs_' + sServiceName;
db.collection(collName, {safe: true},
function (err, collection) {
if (!err) {
if (collection) {
collection.insert(oInsert,
function (err, result) {
if (err) {
logger.error();
callback(err);
}
else {
callback(err, result);
}
}
);
}
}
else {
logger.error(err);
callback(err);
}
}
);
}
function insertServiceLog(sServiceName, oInsert, callback){
var collName = 'svc_' + sServiceName;
db.collection(collName, {safe: true},
function (err, collection) {
if (!err) {
if (collection) {
collection.insert(oInsert,
function (err, result) {
if (err) {
logger.error();
callback(err);
}
else {
callback(err, result);
}
}
);
}
}
else {
logger.error(err);
callback(err);
}
}
);
}
exports.db = db;
exports.initAllModels = initAllModels;
exports.insertMsgLog = insertMsgLog;
exports.insertServiceLog = insertServiceLog;