forked from bcgov/eagle-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_helper.js
93 lines (87 loc) · 2.78 KB
/
app_helper.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
const mongoose = require('mongoose');
const _ = require('lodash');
const winston = require('winston');
const options = require('./config/mongoose_options').mongooseOptions;
// Logging middleware
winston.loggers.add('default', {
file: {
level: 'info',
filename: `/tmp/epic-app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
label: 'default',
},
console: {
colorize: 'true',
handleExceptions: true,
json: false,
level: 'info',
label: 'default',
}
});
var defaultLog = winston.loggers.get('default');
var dbName = (process.env.MONGODB_DATABASE || 'epic');
var dbConnection = 'mongodb://'
+ (process.env.MONGODB_SERVICE_HOST || process.env.DB_1_PORT_27017_TCP_ADDR || 'localhost')
+ '/'
+ dbName;
var db_username = process.env.MONGODB_USERNAME || '';
var db_password = process.env.MONGODB_PASSWORD || '';
var credentials = {
db_username : db_username,
db_password : db_password
};
async function loadModels(dbConnection, options, logger) {
log(logger, 'Connecting to:' + dbConnection);
await mongoose.connect(dbConnection, options).then(() => {
log(logger, 'Database connected');
log(logger, 'loading db models');
require('./api/helpers/models/audit');
require('./api/helpers/models/list');
require('./api/helpers/models/user');
require('./api/helpers/models/group');
require('./api/helpers/models/pin');
require('./api/helpers/models/organization');
require('./api/helpers/models/vc');
require('./api/helpers/models/inspectionItem');
require('./api/helpers/models/inspection');
require('./api/helpers/models/inspectionElement');
require('./api/helpers/models/project');
require('./api/helpers/models/recentActivity');
require('./api/helpers/models/document');
require('./api/helpers/models/comment');
require('./api/helpers/models/commentperiod');
require('./api/helpers/models/topic');
require('./api/helpers/models/projectNotification');
require('./api/helpers/models/cacUser');
log(logger, 'db model loading done.');
},
err => {
log(logger, 'err:' + err);
return;
});
}
function log(logger, msg) {
if (!_.isEmpty(logger)) {
logger.info(msg);
} else {
console.log(msg);
}
}
async function loadMongoose() {
if (!_.isEmpty(credentials)) {
options.user = credentials.db_username;
options.pass = credentials.db_password;
}
mongoose.Promise = global.Promise;
await loadModels(dbConnection, options, defaultLog);
}
exports.loadMongoose = loadMongoose;
exports.loadModels = loadModels;
exports.dbName = dbName;
exports.dbConnection = dbConnection;
exports.credentials = credentials;
exports.defaultLog = defaultLog;