-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
105 lines (88 loc) · 3.53 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
99
100
101
102
103
104
105
const mongoose = require('mongoose');
class MongooseService {
constructor({appdir, config, logger}) {
const mconf = config.get('mongoose');
this.mongoose = mongoose;
this.models = {};
this.schemas = {};
this.appdir = appdir;
if (mconf.url) {
// prefered way to connect
this.connectUrl = mconf.url;
} else {
// retro compatible with 0.1.0
this.connectUrl = `mongodb://${mconf.host}:${mconf.port}/${mconf.db}`;
}
this.options = Object.assign({
},mconf.options||{});
if (mconf.user) this.options.user = mconf.user;
if (mconf.pass) this.options.pass = mconf.pass;
if (mconf.db) this.options.dbName = mconf.db;
// only connect if a runnable service is set
// otherwise it will prevent process from exit
if (process.env.ENTRYPOINT) {
mongoose.connect(this.connectUrl, this.options);
}
this.connection = mongoose.connection;
// subscribers to first successful connection event
this.onReadySubscribers = [];
// Emitted when an error occurs on this this.connection.
this.connection.on('error', (err) => {
logger.error(`mongoose error`, {
err: err.message,
stack: err.stack
});
});
// Emitted when this connection successfully connects to the db. May be emitted multiple times in reconnected scenarios.
this.connection.on('connecting', () => {
logger.info(`mongoose connecting to ${this.connectUrl} ...`);
});
// Emitted after we connected and onOpen is executed on all of this connections models.
this.connection.once('open', () => {
logger.info(`mongoose connected to ${this.connectUrl}`);
this.onReadySubscribers.forEach(f => f());
});
// Emitted when this.connection.close() was executed.
this.connection.on('disconnecting', () => {
logger.info(`mongoose disconnecting from ${this.connectUrl} ...`);
});
// Emitted after getting disconnected from the db.
this.connection.on('disconnected', () => {
logger.info(`mongoose disconnected from ${this.connectUrl}`);
});
// Emitted after we disconnected and onClose executed on all of this connections models..
this.connection.on('close', () => {
logger.info(`mongoose disconnected from ${this.connectUrl} (2)`);
});
// Emitted after we connected and subsequently disconnected, followed by successfully another successfull this.connection.
this.connection.on('reconnected', () => {
logger.info(`mongoose reconnected to ${this.connectUrl}`);
});
// Emitted in a replica-set scenario, when primary and at least one seconaries specified in the connection string are connected
this.connection.on('fullsetup', () => {
logger.info(`Emitted in a replica-set scenario, when primary and at least one seconaries specified in the connection string are connected`);
});
// Emitted in a replica-set scenario, when all nodes specified in the connection string are connected.
this.connection.on('all', () => {
logger.info(`Emitted in a replica-set scenario, when all nodes specified in the connection string are connected.`);
});
}
/**
* Subscribe to successful connection
*
* @param {Function} _cb
*/
onReady(_cb) {
const addReadyListener = (_cb) => {
this.onReadySubscribers.push(_cb);
};
}
/**
* DI for models
*/
register() {
const base = `${this.appdir}/res/mongoose`;
return require('fs').readdirSync(base).filter(file => file.endsWith('.js')).map(file => `${base}/${file}`);
}
}
module.exports = MongooseService;