Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade mongoose from 5 to 8.8.4 #1676

Merged
merged 16 commits into from
Dec 16, 2024
Prev Previous commit
Next Next commit
update dropDatabase
  • Loading branch information
AlvaroVega committed Dec 10, 2024
commit 3a3deead3f62a2ed21eede41fce1cf8914a3b6bf
5 changes: 3 additions & 2 deletions lib/model/dbConn.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ function init(host, db, port, options, callback) {

mongoose
.connect(url, options)
.then((connection) => {
defaultDb = connection.connection;
.then(() => {
//defaultDb = connection.connection;
defaultDb = mongoose.connection;
logger.info(context, 'Successfully connected to MongoDB.');
loadModels();
callback();
Expand Down
50 changes: 38 additions & 12 deletions lib/services/commands/commandRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/

const logger = require('logops');
const dbService = require('../../model/dbConn');
//const dbService = require('../../model/dbConn');
const mongoose = require('mongoose');
const intoTrans = require('../common/domain').intoTrans;
const errors = require('../../errors');
const Command = require('../../model/Command');
Expand Down Expand Up @@ -92,9 +93,17 @@ function updateCommand(service, subservice, deviceId, command, callback) {
} else {
commandDAO.value = command.value;

commandDAO.save(function (error) {
callback(error, commandDAO.toObject());
});
commandDAO
.save({})
.then((commandDAOs) => {
callback(null, commandDAOs.toObject());
})
.catch((error) => {
callback(error);
});
// commandDAO.save(function (error) {
// callback(error, commandDAO.toObject());
// });
}
});
}
Expand All @@ -114,15 +123,24 @@ function createCommand(service, subservice, deviceId, command, callback) {

logger.debug(context, 'Storing command for deviceId [%s] with name [%s]', deviceId, command.name);

commandObj.save(function saveHandler(error, commandDAO) {
if (error) {
commandObj
.save({})
.then((commandDAO) => {
callback(null, commandDAO.toObject());
})
.catch((error) => {
logger.debug(context, 'Error storing command information: %s', error);

callback(new errors.InternalDbError(error));
} else {
callback(null, commandDAO.toObject());
}
});
});
// commandObj.save(function saveHandler(error, commandDAO) {
// if (error) {
// logger.debug(context, 'Error storing command information: %s', error);

// callback(new errors.InternalDbError(error));
// } else {
// callback(null, commandDAO.toObject());
// }
// });
}

function addCommand(service, subservice, deviceId, command, callback) {
Expand Down Expand Up @@ -310,7 +328,15 @@ function removeFromDate(creationDate, callback) {
}

function clear(callback) {
dbService.db.db.dropDatabase(callback);
//dbService.db.db.dropDatabase(callback);
mongoose.connection
.dropDatabase()
.then(() => {
callback(null);
})
.catch((error) => {
callback(error);
});
}

function init(newConfig, callback) {
Expand Down
13 changes: 11 additions & 2 deletions lib/services/devices/deviceRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
*/

const logger = require('logops');
const dbService = require('../../model/dbConn');
//const dbService = require('../../model/dbConn');
const mongoose = require('mongoose');
const config = require('../../commonConfig');
const fillService = require('./../common/domain').fillService;
const alarmsInt = require('../common/alarmManagement').intercept;
Expand Down Expand Up @@ -423,7 +424,15 @@ function update(previousDevice, device, callback) {
* Cleans all the information in the database, leaving it in a clean state.
*/
function clear(callback) {
dbService.db.db.dropDatabase(callback);
//dbService.db.db.dropDatabase(callback);
mongoose.connection
.dropDatabase()
.then(() => {
callback(null);
})
.catch((error) => {
callback(error);
});
}

function itemToObject(i) {
Expand Down
18 changes: 13 additions & 5 deletions lib/services/groups/groupRegistryMongoDB.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/

const logger = require('logops');
const dbService = require('../../model/dbConn');
//const dbService = require('../../model/dbConn');
const intoTrans = require('../common/domain').intoTrans;
const fillService = require('./../common/domain').fillService;
const alarmsInt = require('../common/alarmManagement').intercept;
Expand Down Expand Up @@ -191,7 +191,7 @@ function listGroups(service, limit, offset, callback) {
logger.debug('results of query: %j', results);
callback(error, {
count: results[1],
devices: results[0].map(toObjectFn)
services: results[0].map(toObjectFn)
});
});
// async.series(
Expand Down Expand Up @@ -274,7 +274,7 @@ function find(service, subservice, callback) {
cb(error);
});
}
function funcQueryCount(callback) {
function funcQueryCount(cb) {
queryCount
.exec({})
.then((res) => {
Expand All @@ -287,7 +287,7 @@ function find(service, subservice, callback) {
async.series([funcQuery, funcQueryCount], function (error, results) {
callback(error, {
count: results[1],
devices: results[0].map(toObjectFn)
services: results[0].map(toObjectFn)
});
});
// async.series(
Expand Down Expand Up @@ -425,7 +425,15 @@ function init(newConfig, callback) {
}

function clear(callback) {
dbService.db.db.dropDatabase(callback);
//dbService.db.db.dropDatabase(callback);
mongoose.connection
.dropDatabase()
.then(() => {
callback(null);
})
.catch((error) => {
callback(error);
});
}

exports.create = alarmsInt(constants.MONGO_ALARM + '_01', intoTrans(context, createGroup));
Expand Down