nodejs-rigorous
Version:
Rigorous Framework
147 lines (96 loc) • 4.56 kB
JavaScript
const { RigorousError, errorTypes } = require('../../factory/RigorousError/index');
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(collectionName, attributesSettings, paginateMaxResultPerPage, beforeCreateTransformation) {
this.collectionName = collectionName;
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.collectionName, 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(errorTypes.RESPONSE_ERROR_DEVELOPER); }
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(errorTypes.RESPONSE_ERROR_OPERATION_NOT_UPDATABLE);
}
}
}),
);
const result = await mongoUpdate(this.collectionName, queryRead, queryUpdate, params);
return result;
} catch (err) {
throw err;
}
}
async deleteObject(queryDelete) {
try {
const result = await mongoDelete(this.collectionName, queryDelete);
return result;
} catch (err) {
throw err;
}
}
async readObject(mongoose, query, params) {
try {
const result = await mongoRead(mongoose, this.collectionName, query, params);
return result;
} catch (err) {
throw err;
}
}
async readManyObject(query, lastPaginateId, reverse, params) {
try {
const result = await mongoReadMany(this.collectionName, query, this.paginateMaxResultPerPage, lastPaginateId, reverse, params);
return result;
} catch (err) {
throw err;
}
}
}
module.exports = ObjectCrud;