-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
144 lines (128 loc) · 3.23 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
const { MongoClient, Logger: mLogger, ObjectId } = require('mongodb');
/**
* @class Mongo
*/
class Mongo {
/**
* @param {string} name - unique name to this service
* @param {EventEmitter} emitter
* @param {Object} config - configuration object of service
*/
constructor(name, emitter, config) {
this.name = name;
this.emitter = emitter;
this.client = null;
this.config = Object.assign({
host: 'localhost',
port: 27017,
}, config, {
auth: Object.assign({
use: false,
}, config.auth),
replica: Object.assign({
use: false,
}, config.replica),
options: Object.assign({
keepAlive: true,
useUnifiedTopology: true,
autoReconnect: true,
poolSize: 5,
connectTimeoutMS: 30000,
socketTimeoutMS: 30000,
connectWithNoPrimary: false,
readPreference: 'secondaryPreferred',
}, config.options)
});
}
log(message, data) {
this.emitter.emit('log', {
service: this.name,
message,
data,
});
}
success(message, data) {
this.emitter.emit('success', {
service: this.name, message, data,
});
}
error(err, data) {
this.emitter.emit('error', {
service: this.name,
data,
err,
});
}
/**
* Connect to server
*/
init() {
const { config } = this;
const { auth, options, replica } = config;
if (this.client) {
return Promise.resolve(this);
}
const infoObj = {};
let url = 'mongodb://';
if (auth.use === true) {
Object.assign(infoObj, {
authentication: 'TRUE',
});
url += `${auth.username}:${auth.password}@`;
Object.assign(options, {
authSource: auth.authSource,
});
} else {
Object.assign(infoObj, {
authentication: 'FALSE',
});
}
if (replica.use === true) {
Object.assign(infoObj, {
mode: 'REPLICAS',
servers: replica.servers,
});
url += replica.servers.map(s => `${s.host}:${s.port}`).join(',');
Object.assign(options, {
replicaSet: replica.name,
});
} else {
Object.assign(infoObj, {
mode: 'SINGLE',
host: config.host,
port: config.port,
});
url += `${config.host}:${config.port}`;
}
Object.assign(infoObj, {
db: config.db,
options,
});
this.log(`Connecting in ${infoObj.mode} mode`, infoObj);
return MongoClient.connect(url, options).then(client => {
this.client = client.db(config.db);
this.connected = true;
this.success(`Successfully connected in ${infoObj.mode} mode`);
mLogger.setLevel('info');
mLogger.setCurrentLogger((msg, context) => {
this.log(msg, context);
});
return this;
});
}
}
function isValidObjectId(value) {
const regex = /[0-9a-f]{24}/;
const matched = String(value).match(regex);
if (!matched) {
return false;
}
return ObjectId.isValid(value);
}
function castToObjectId(value) {
if (isValidObjectId(value) === false) {
throw new TypeError(`Value passed is not valid objectId, is [ ${value} ]`);
}
return ObjectId.createFromHexString(value);
}
module.exports = { Mongo, ObjectId, isValidObjectId, castToObjectId };