Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit abd93c4

Browse files
authored
Update mongodb node module (#913)
1 parent b84928a commit abd93c4

File tree

3 files changed

+77
-32
lines changed

3 files changed

+77
-32
lines changed

source/management_api/SchemaUpdate3to4.js

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,32 @@ if (!databaseUrl) {
1111
databaseUrl = 'localhost/owtdb';
1212
}
1313

14-
var db = require('mongojs')(databaseUrl, ['rooms', 'services']);
14+
var MongoClient = require('mongodb').MongoClient;
15+
var connectOption = {
16+
useUnifiedTopology: true,
17+
};
18+
var db;
1519
var Fraction = require('fraction.js');
1620
var DefaultUtil = require('./data_access/defaults');
1721
var Room = require('./data_access/model/roomModel');
1822

23+
function prepareDB(next) {
24+
if (dbURL.indexOf('mongodb://') !== 0) {
25+
dbURL = 'mongodb://' + dbURL;
26+
}
27+
MongoClient.connect(dbURL, connectOption, function(err, client) {
28+
if (!err) {
29+
db = client.db();
30+
db.close = client.close;
31+
db.rooms = db.collection('rooms');
32+
db.services = db.collection('services');
33+
next();
34+
} else {
35+
console.error('Failed to connect mongodb:', err);
36+
}
37+
});
38+
}
39+
1940
function fracString(num) {
2041
var frac = Fraction(0);
2142
try {
@@ -143,7 +164,7 @@ function processRoom(rooms, i, onFinishRoom) {
143164
}
144165

145166
var room = upgradeRoom(rooms[i]);
146-
db.rooms.save(room, function (err, saved) {
167+
db.rooms.updateOne({_id: room._id}, room, function (err, saved) {
147168
if (err) {
148169
console.log('Error in updating room:', room._id, err.message);
149170
} else {
@@ -167,7 +188,7 @@ function processServices(services, i, onFinishService) {
167188
var processNext = function() {
168189
service.__v = 0;
169190
service.rooms = service.rooms.map((room) => (room._id ? room._id : room));
170-
db.services.save(service, function (e, saved) {
191+
db.services.updateOne({_id: service._id}, service, function (e, saved) {
171192
if (e) {
172193
console.log('Error in updating service:', service._id, e.message);
173194
} else {
@@ -195,17 +216,19 @@ function processServices(services, i, onFinishService) {
195216
}
196217

197218
function processAll(finishCb) {
198-
db.services.find({}).toArray(function (err, services) {
199-
console.log('Starting');
200-
if (err) {
201-
console.log('Error in getting services:', err.message);
202-
db.close();
203-
return;
204-
}
205-
processServices(services, 0, function() {
206-
db.close();
207-
if (typeof finishCb === 'function')
208-
finishCb();
219+
prepareDB(function() {
220+
db.services.find({}).toArray(function (err, services) {
221+
console.log('Starting');
222+
if (err) {
223+
console.log('Error in getting services:', err.message);
224+
db.close();
225+
return;
226+
}
227+
processServices(services, 0, function() {
228+
db.close();
229+
if (typeof finishCb === 'function')
230+
finishCb();
231+
});
209232
});
210233
});
211234
}

source/management_api/initdb.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ var currentVersion = '1.0';
1515
var fs = require('fs');
1616
var path = require('path');
1717

18+
var MongoClient = require('mongodb').MongoClient;
19+
var connectOption = {
20+
useUnifiedTopology: true,
21+
};
22+
var client;
1823
var db;
1924
var cipher = require('./cipher');
2025

@@ -29,6 +34,9 @@ var sampleEntryName = (require(samplePackageJson).main || DEFAULT_SAMPLE_ENTRY);
2934
var sampleServiceFile = path.resolve(dirName, SAMPLE_RELATED_PATH, sampleEntryName);
3035

3136
function prepareDB(next) {
37+
if (dbURL.indexOf('mongodb://') !== 0) {
38+
dbURL = 'mongodb://' + dbURL;
39+
}
3240
if (fs.existsSync(cipher.astore)) {
3341
cipher.unlock(cipher.k, cipher.astore, function cb (err, authConfig) {
3442
if (!err) {
@@ -38,22 +46,36 @@ function prepareDB(next) {
3846
+ '@' + dbURL;
3947
}
4048
} else {
41-
log.error('Failed to get mongodb auth:', err);
49+
console.error('Failed to get mongodb auth:', err);
4250
}
43-
db = require('mongojs')(dbURL, ['services', 'infos', 'rooms']);
44-
next();
51+
MongoClient.connect(dbURL, connectOption, function(err, cli) {
52+
if (!err) {
53+
client = cli;
54+
db = client.db();
55+
next();
56+
} else {
57+
console.error('Failed to connect mongodb:', err);
58+
}
59+
});
4560
});
4661
} else {
47-
db = require('mongojs')(dbURL, ['services', 'infos', 'rooms']);
48-
next();
62+
MongoClient.connect(dbURL, connectOption, function(err, cli) {
63+
if (!err) {
64+
client = cli;
65+
db = client.db();
66+
next();
67+
} else {
68+
console.error('Failed to connect mongodb:', err);
69+
}
70+
});
4971
}
5072
}
5173

5274
function upgradeH264(next) {
53-
db.rooms.find({}).toArray(function (err, rooms) {
75+
db.collection('rooms').find({}).toArray(function (err, rooms) {
5476
if (err) {
5577
console.log('Error in getting rooms:', err.message);
56-
db.close();
78+
client.close();
5779
return;
5880
}
5981
if (!rooms || rooms.length === 0) {
@@ -89,7 +111,7 @@ function upgradeH264(next) {
89111
}
90112
});
91113

92-
db.rooms.save(room, function (e, saved) {
114+
db.collection('rooms').updateOne({_id: room._id}, room, function (e, saved) {
93115
if (e) {
94116
console.log('Error in upgrading room:', room._id, e.message);
95117
}
@@ -103,28 +125,28 @@ function upgradeH264(next) {
103125
}
104126

105127
function checkVersion (next) {
106-
db.infos.findOne({_id: 1}, function cb (err, info) {
128+
db.collection('infos').findOne({_id: 1}, function cb (err, info) {
107129
if (err) {
108130
console.log('mongodb: error in query info');
109-
return db.close();
131+
return client.close();
110132
}
111133
if (info) {
112134
if (info.version === '1.0') {
113135
next();
114136
}
115137
} else {
116-
db.services.findOne({}, function cb (err, service) {
138+
db.collection('services').findOne({}, function cb (err, service) {
117139
if (err) {
118140
console.log('mongodb: error in query service');
119141
return db.close();
120142
}
121143
var upgradeNext = function(next) {
122144
upgradeH264(function() {
123145
info = {_id: 1, version: currentVersion};
124-
db.infos.save(info, function cb (e, s) {
146+
db.collection('infos').save(info, function cb (e, s) {
125147
if (e) {
126148
console.log('mongodb: error in updating version');
127-
return db.close();
149+
return client.close();
128150
}
129151
next();
130152
});
@@ -162,15 +184,15 @@ function checkVersion (next) {
162184
}
163185

164186
function prepareService (serviceName, next) {
165-
db.services.findOne({name: serviceName}, function cb (err, service) {
187+
db.collection('services').findOne({name: serviceName}, function cb (err, service) {
166188
if (err || !service) {
167189
var crypto = require('crypto');
168190
var key = crypto.pbkdf2Sync(crypto.randomBytes(64).toString('hex'), crypto.randomBytes(32).toString('hex'), 4000, 128, 'sha256').toString('base64');
169191
service = {name: serviceName, key: cipher.encrypt(cipher.k, key), encrypted: true, rooms: [], __v: 0};
170-
db.services.save(service, function cb (err, saved) {
192+
db.collection('services').save(service, function cb (err, saved) {
171193
if (err) {
172194
console.log('mongodb: error in adding', serviceName);
173-
return db.close();
195+
return client.close();
174196
}
175197
saved.key = key;
176198
next(saved);
@@ -228,7 +250,7 @@ prepareDB(function() {
228250
var sampleServiceKey = service.key;
229251
console.log('sampleServiceId:', sampleServiceId);
230252
console.log('sampleServiceKey:', sampleServiceKey);
231-
db.close();
253+
client.close();
232254

233255
writeSampleFile(sampleServiceId, sampleServiceKey);
234256
});

source/management_api/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"amqp": "*",
66
"log4js": "^1.1.1",
77
"express": "*",
8-
"mongojs": "",
8+
"mongodb": "^3.6.4",
99
"toml": "*",
1010
"ajv": "^6.12.3",
1111
"body-parser": "*",

0 commit comments

Comments
 (0)