77 lines
2.9 KiB
JavaScript
77 lines
2.9 KiB
JavaScript
/**
|
|
* @file This file wraps the functions in mainDB.js with promises for simpler
|
|
* use in promises and async/await
|
|
*/
|
|
|
|
const Q = require('q');
|
|
const httpStatus = require('http-status-codes');
|
|
|
|
//
|
|
// We MUST require maindDB with the exact same path as where it is initialised or we
|
|
// end up with a different instance of it where the collections have not been initialised.
|
|
//
|
|
const mainDB = require(global.pathPrefix + 'mainDB.js');
|
|
const utils = require(global.pathPrefix + 'utils.js');
|
|
|
|
module.exports = {
|
|
findOneObject: (...args) => Q.nfapply(mainDB.findOneObject, args),
|
|
addObject: (...args) => Q.nfapply(mainDB.addObject, args),
|
|
addMany: (...args) => Q.nfapply(mainDB.addMany, args),
|
|
updateObject: (...args) => Q.nfapply(mainDB.updateObject, args),
|
|
removeObject: (...args) => Q.nfapply(mainDB.removeObject, args),
|
|
addObjectPWithCode: (...args) => withCode(module.exports.addObject, args),
|
|
findOneObjectPWithCode: (...args) => withCode(module.exports.findOneObject, args),
|
|
updateObjectPWithCode: (...args) => withCode(module.exports.updateObject, args),
|
|
removeObjectPWithCode: (...args) => withCode(module.exports.removeObject, args),
|
|
|
|
updateObjectPCheckObjectUpdated: (...args) => checkObjectUpdated(mainDB.updateObject, args),
|
|
|
|
/**
|
|
* Share the mainDB file for easy access to the collections
|
|
*/
|
|
mainDB
|
|
};
|
|
|
|
/**
|
|
* Wrapper functions that allows for specific error handling or promise functions
|
|
*
|
|
* @type {Function} withCode
|
|
* @param {!Function} action - function that this function has wrapped around
|
|
* @param {!Array} args - Options for the insert command. Use 'undefined' if there are none.
|
|
*/
|
|
function withCode(action, args) {
|
|
const code = args[args.length - 1];
|
|
const params = args.slice(0, args.length - 1);
|
|
|
|
return action(...params).catch(() =>
|
|
Q.reject(utils.createError(code, 'Database offline.', httpStatus.BAD_GATEWAY)));
|
|
}
|
|
|
|
/**
|
|
* Specific Wrapper for mongoDB update
|
|
* Handles general mongoDB errors and if the update fails to update any objects.
|
|
*
|
|
* @type {Function} checkObjectUpdated
|
|
* @param {!Function} action - function that this function has wrapped around
|
|
* @param {!Array} args - Options for the insert command. Use 'undefined' if there are none.
|
|
*/
|
|
function checkObjectUpdated(action, args) {
|
|
const code = args[args.length - 1];
|
|
const params = args.slice(0, args.length - 1);
|
|
return Q.nfcall(action, ...params)
|
|
.then((result) => {
|
|
if (result.result.nModified === 1) {
|
|
return Q.resolve(result);
|
|
} else {
|
|
return Q.reject(utils.createError(code, 'Failed to update object', httpStatus.CONFLICT));
|
|
}
|
|
})
|
|
.catch((error) => {
|
|
if (error.code && error.httpCode && error.message) {
|
|
return Q.reject(error);
|
|
} else {
|
|
return Q.reject(utils.createError(code, 'Database offline.', httpStatus.BAD_GATEWAY));
|
|
}
|
|
});
|
|
}
|