This repository has been archived by the owner on Oct 30, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
98 lines (76 loc) · 2.19 KB
/
index.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
'use strict';
const assert = require('assert');
const mongoose = require('mongoose');
const merge = require('merge');
require('dotenv').config({ silent: true });
require('mongoose-types').loadTypes(mongoose);
mongoose.Promise = require('bluebird');
/**
* MongoDB storage interface
* @constructor
* @param {Object} mongoConf
* @param {Object} options
*/
function Storage(mongoURI, mongoOptions, storageOptions) {
if (!(this instanceof Storage)) {
return new Storage(mongoURI, mongoOptions, storageOptions);
}
assert(typeof mongoOptions === 'object', 'Invalid mongo options supplied');
this._uri = mongoURI;
this._options = mongoOptions;
const defaultLogger = {
info: console.log,
debug: console.log,
error: console.error,
warn: console.warn
};
this._log = defaultLogger;
if (storageOptions && storageOptions.logger) {
this._log = storageOptions.logger;
}
// connect to the database
this._connect();
}
Storage.models = require('./lib/models');
Storage.constants = require('./lib/constants');
/**
* Connects to the database
* @returns {mongoose.Connection}
*/
Storage.prototype._connect = function() {
var self = this;
var defaultOpts = {
mongos: false,
ssl: false,
server: {
auto_reconnect: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 5000
}
};
var opts = merge.recursive(true, defaultOpts, this._options);
this._log.info('opening database connection at %s', this._uri);
this.connection = mongoose.createConnection(this._uri, opts);
this.connection.on('error', function(err) {
self._log.error('database connection error:', err.message);
});
this.connection.on('disconnected', function() {
self._log.warn('disconnected from database');
});
this.connection.on('connected', function() {
self._log.info('connected to database');
});
this.models = this._createBoundModels();
};
/**
* Return a dictionary of models bound to this connection
* @returns {Object}
*/
Storage.prototype._createBoundModels = function() {
var bound = {};
for (let model in Storage.models) {
bound[model] = Storage.models[model](this.connection);
}
return bound;
};
module.exports = Storage;