-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
101 lines (82 loc) · 3.5 KB
/
Copy pathindex.js
File metadata and controls
101 lines (82 loc) · 3.5 KB
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
/**
* Entry point
*
* Created by Christian Dallago on 20160416 .
*/
var context;
module.exports = {
start: function(callback) {
callback = callback || function(){};
// Imports
const mongoose = require('mongoose');
const fs = require('fs');
const path = require('path');
const q = require('q');
const formidable = require('formidable');
// Initialize the context
context = {
fs : fs,
mongoose : mongoose,
path : path,
promises : q,
formidable : formidable,
gridFs : {},
constants : {}
};
// Function to load all components from the respective folders (models, controllers, services, daos, utils)
context.component = function(componentName) {
if (!context[componentName]) {
context[componentName] = {};
}
return {
module: function(moduleName) {
if (!context[componentName][moduleName]) {
console.log('Loading component ' + componentName);
context[componentName][moduleName] = require(path.join(__dirname, componentName, moduleName))(context,
componentName, moduleName);
console.log('LOADED ' + componentName + '.' + moduleName);
}
return context[componentName][moduleName];
}
}
};
callback(context);
return context;
},
connect: function(callback){
const context = this.start();
const gridFs = require('gridfs-stream');
const config = require(__dirname + "/config");
context.config = config;
//Create the DB connection string
var databaseParams = config.database;
var dbConnection = "mongodb://";
if (databaseParams.username && databaseParams.password && databaseParams.username.length > 0 && databaseParams.password.length > 0) {
dbConnection += databaseParams.username + ":" + databaseParams.password + "@";
}
dbConnection += databaseParams.uri + ":" + databaseParams.port + "/" + databaseParams.collection;
dbConnection += (databaseParams.ssl !== undefined && databaseParams.ssl === true) ? "?ssl=true" : "";
context.mongoConnectionString = dbConnection;
console.log("Connecting to " + context.mongoConnectionString);
context.mongoose.connect(dbConnection);
var db = context.mongoose.connection;
// CONNECTION EVENTS: When successfully connected
db.on('connected', function () {
console.log('Mongoose connected');
});
// If the connection throws an error
db.on('error', function (err) {
console.log('Mongoose default connection error: ' + err);
return process.exit(1);
});
// When the connection is disconnected
db.on('disconnected', function () {
console.log('Mongoose default connection disconnected');
return process.exit(1);
});
db.on('open', function () {
context.gridFs = gridFs(db.db, context.mongoose.mongo);
return callback(context);
});
}
}