UNPKG

node-rigorous

Version:
45 lines (32 loc) 1.48 kB
const mongoose = require('mongoose'); const RigorousError = require('../../../facades/RigorousError'); const errorsMessages = require('../../../etc/errorsMessages'); const mongoRead = require('./read'); const helper_format_checker = require('../../../helpers/format_checker'); module.exports = async (modelName, attributeObjectUpdated, beforeCreateTransformation, params) => { if (helper_format_checker.isNil(params)) { throw new RigorousError(errorsMessages.OperationError.ParametersMissing); } if (helper_format_checker.isNil(params.selectAttributesReturned) && helper_format_checker.isObjectEmpty(params.selectAttributesReturned)) { throw new RigorousError(errorsMessages.OperationError.ParametersMissing); } try { if (beforeCreateTransformation) { beforeCreateTransformation(attributeObjectUpdated); } // Create const result = await mongoose.model(modelName).create(attributeObjectUpdated); const ids = []; if (Array.isArray(result)) { result.forEach((resultUnit) => { ids.push(resultUnit._id); }); } else { ids.push(result._id); } const resultJson = await mongoRead(modelName, { _id: { $in: ids } }, params); return resultJson; } catch (errGlobal) { throw new RigorousError(errorsMessages.OperationError, errGlobal); } };