UNPKG

node-rigorous

Version:
153 lines (99 loc) 4.59 kB
const RigorousError = require('../../facades/RigorousError'); const errorsMessages = require('../../etc/errorsMessages'); const mongoCreate = require('./mongoose/create'); const mongoUpdate = require('./mongoose/update'); const mongoDelete = require('./mongoose/delete'); const mongoRead = require('./mongoose/read'); const mongoReadMany = require('./mongoose/read_many'); const rigorousConfig = require('../../config'); class ObjectCrud { constructor(modelName, attributesSettings, paginateMaxResultPerPage, beforeCreateTransformation) { this.modelName = modelName; this.attributesSettings = attributesSettings; this.beforeCreateTransformation = beforeCreateTransformation; this.paginateMaxResultPerPage = paginateMaxResultPerPage; } async createObject(attributesToBeDefined, params) { const instanciateObject = {}; try { // Execute in parallele but wait all finish await Promise.all( Object.keys(this.attributesSettings).map(async (key) => { const attr = this.attributesSettings[key]; const onInstanciationValue = attr.on.create.value; const attrName = key; if (onInstanciationValue === rigorousConfig.TO_BE_DEFINE_WITH_BEFORE_CREATE_TRANSFORMATION) { return; } if (onInstanciationValue === rigorousConfig.TO_BE_DEFINE_ON_CREATE) { instanciateObject[attrName] = attributesToBeDefined[attrName]; } else { instanciateObject[attrName] = onInstanciationValue; } if (attr.on.create.transform) { instanciateObject[attrName] = await attr.on.create.transform(instanciateObject[attrName]); } }), ); const result = await mongoCreate(this.modelName, instanciateObject, this.beforeCreateTransformation, params); return result; } catch (err) { throw err; } } async updateObject(queryRead, jsonFieldsUpdated, params) { const queryUpdate = {}; try { // Execute in parallele but wait all finish await Promise.all( Object.keys(this.attributesSettings).map(async (key) => { if (key in jsonFieldsUpdated) { const attrSetting = this.attributesSettings[key]; if (!attrSetting) { throw new RigorousError(errorsMessages.DevelopperError); } const isUpdatable = attrSetting.on.update.updatable; if (isUpdatable) { let updateValue = jsonFieldsUpdated[key]; if (attrSetting.on.update.transform) { updateValue = await attrSetting.on.update.transform(updateValue); } queryUpdate[key] = updateValue; } else { throw new RigorousError(errorsMessages.OperationError.NotUpdatable); } } }), ); const result = await mongoUpdate(this.modelName, queryRead, queryUpdate, params); return result; } catch (err) { throw err; } } async deleteObject(queryDelete) { try { const result = await mongoDelete(this.modelName, queryDelete); return result; } catch (err) { throw err; } } async readObject(query, params) { try { const result = await mongoRead(this.modelName, query, params); return result; } catch (err) { throw err; } } async readManyObject(query, lastPaginateId, reverse, params) { try { const result = await mongoReadMany(this.modelName, query, this.paginateMaxResultPerPage, lastPaginateId, reverse, params); return result; } catch (err) { throw err; } } } module.exports = ObjectCrud;