-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
194 lines (179 loc) · 5.73 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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
const process = require('node:process');
const mergeOptions = require('merge-options');
const mongoose = require('mongoose');
const { boolean } = require('boolean');
//
// NOTE: we have to use NativeConnection instead of MongooseConnection due to doClose not being exposed
//
const Connection = require('mongoose/lib/drivers/node-mongodb-native/connection');
function log(fn, message, hideMeta) {
if (hideMeta) fn(message, { [hideMeta]: true });
else fn(message);
}
class Mongoose {
constructor(config = {}) {
this.config = mergeOptions(
{
logger: console,
hideMeta: process.env.NODE_ENV === 'test' ? false : 'hide_meta',
bindEvents: true,
mongo: {
options: {
serverSelectionTimeoutMS:
process.env.NODE_ENV === 'test' ? 9000 : 30000, // default is 30s
heartbeatFrequencyMS: process.env.NODE_ENV === 'test' ? 3000 : 10000 // default is 10s
}
},
debug: boolean(process.env.MONGOOSE_DEBUG),
strict: true,
strictQuery: false,
maxTimeMS: process.env.NODE_ENV === 'test' ? 10000 : 30000 // default 30s timeout for a query to complete
},
config
);
// detect mongoose version
this._mongooseVersion = mongoose.version;
if (this._mongooseVersion < '6')
throw new Error(
'mongoose peer dependency > 6 required.\nnpm install mongoose@6'
);
// options from <https://mongoosejs.com/docs/api.html#mongoose_Mongoose-set>
const options = [
'allowDiskUse',
'applyPluginsToChildSchemas',
'applyPluginsToDiscriminators',
'autoCreate',
'autoIndex',
'bufferCommands',
'bufferTimeoutMS',
'debug',
'id',
'timestamps.createdAt.immutable',
'maxTimeMS',
'objectIdGetter',
'overwriteModels',
'returnOriginal',
'runValidators',
'sanitizeFilter',
'selectPopulatedPaths',
'strict',
'strictQuery',
'toJSON',
'toObject'
];
for (const prop of options) {
if (this.config[prop] !== undefined)
mongoose.set(prop, this.config[prop]);
}
// bind this
this.createConnection = this.createConnection.bind(this);
}
createConnection(
uri = this.config.mongo.uri,
options = this.config.mongo.options
) {
//
// create connection
//
// <https://github.com/Automattic/mongoose/issues/12970>
const connection = new Connection(mongoose);
connection._connectionString = uri;
connection._connectionOptions = options;
//
// hacky approach so that `openUri` is not called immediately
// <https://github.com/Automattic/mongoose/issues/12970>
//
connection.asPromise = function () {
if (
!this.$initialConnection ||
this.readyState === mongoose.ConnectionStates.disconnected
)
this.$initialConnection = this.openUri(
this._connectionString,
this._connectionOptions
);
return this.$initialConnection;
};
mongoose.connections.push(connection);
mongoose.events.emit('createConnection', connection);
//
// bind connection events
// <https://mongoosejs.com/docs/connections.html#connection-events>
//
if (this.config.bindEvents) {
connection.on('connecting', () =>
log(
this.config.logger.debug,
`Mongoose connection #${connection.id} connecting.`,
this.config.hideMeta
)
);
connection.on('connected', () =>
log(
this.config.logger.debug,
`Mongoose connection #${connection.id} connected to: ${connection.host}:${connection.port}/${connection.name}`,
this.config.hideMeta
)
);
connection.on('disconnecting', () =>
log(
this.config.logger.debug,
`Mongoose connection #${connection.id} disconnecting from: ${connection.host}:${connection.port}/${connection.name}`,
this.config.hideMeta
)
);
connection.on('disconnected', () => {
log(
this.config.logger.debug,
`Mongoose connection #${connection.id} disconnected.`,
this.config.hideMeta
);
});
connection.on('close', () =>
log(
this.config.logger.debug,
`Mongoose connection #${connection.id} closed.`,
this.config.hideMeta
)
);
connection.on('reconnected', () =>
log(
this.config.logger.debug,
`Mongoose connection #${connection.id} reconnected to: ${connection.host}:${connection.port}/${connection.name}`,
this.config.hideMeta
)
);
connection.on('fullsetup', () =>
log(
this.config.logger.debug,
`Mongoose connection #${connection.id} full setup with replica set primary and at least one secondary.`,
this.config.hideMeta
)
);
connection.on('all', () =>
log(
this.config.logger.debug,
`Mongoose connection #${connection.id} replica set connected to all servers.`,
this.config.hideMeta
)
);
connection.on('reconnectFailed', () =>
log(
this.config.logger.error,
`Mongoose connection #${connection.id} reconnect failed to: ${connection.host}:${connection.port}/${connection.name}`,
this.config.hideMeta
)
);
connection.on('reconnectTries', () =>
log(
this.config.logger.error,
`Mongoose connection #${connection.id} reconnect tries exceeded to: ${connection.host}:${connection.port}/${connection.name}`,
this.config.hideMeta
)
);
connection.on('error', (err) => this.config.logger.error(err));
}
return connection;
}
}
module.exports = Mongoose;