@tasolutions/express-core
Version:
All libs for express
166 lines (153 loc) • 8.24 kB
JavaScript
//crud.js
const express = require('express');
const _ = require('lodash');
const actionsDefault = ['CREATE', 'LIST', 'DETAIL', 'UPDATE', 'DELETE', 'DELETE_PERMANENTLY', 'RESTORE', 'COUNT', 'EXPORT'];
const actionList = require('../utils/actionList');
const modelList = require('../utils/modelList');
const { readMany } = require('../core/crud/readMany.crud');
const { readOne } = require('../core/crud/readOne.crud');
const { update } = require('../core/crud/update.crud');
const { create } = require('../core/crud/create.crud');
const { remove } = require('../core/crud/remove.crud');
const { getByIds } = require('../core/crud/getByIds.crud');
const { count } = require('../core/crud/count.crud');
const { exportToExcel } = require('../core/crud/exportToExcel.crud');
// const { getSchema } = require('../core/crud/getSchema.crud');
// const { getAllSchemaAuth } = require('../core/crud/getAllSchemaAuth.crud');
// const { getSchemaInterface } = require('../core/crud/getSchemaInterface.crud');
const { removePermanently } = require('../core/crud/removePermanently.crud');
const { restore } = require('../core/crud/restore.crud');
const { getAllSchema } = require('../core/crud/getAllSchema.crud');
const modelNamesSet = new Set();
module.exports = (Collection, auth = false, configs = { actions: actionsDefault, includeFields: [], scope: false }) => {
modelNamesSet.add(Collection.modelName);
// ======
// Routes
// ======
let router = express.Router();
const actions = configs.actions ? configs.actions : actionsDefault;
const path = configs.path;
if (configs.scope) addPathModelToList(Collection, path);
// addActionToList(Collection, 'GET_SCHEMA', 'GET', `${path}/schema`);
// router.get('/schema', setAction(Collection, 'GET_SCHEMA'), (req, res) => getSchema(req, res, Collection));
// addActionToList(Collection, 'GET_SCHEMA_INTERFACE', 'GET', `${path}/schema-interface`);
// router.get('/schema-interface', setAction(Collection, 'GET_SCHEMA_INTERFACE'), (req, res) => getSchemaInterface(req, res, Collection, path));
addActionToList(Collection, 'SCHEMAS', 'GET', `${path}/schemas`);
router.get('/schemas', setAction(Collection, 'SCHEMAS'), (req, res) => getAllSchema(req, res, Collection, path));
if (auth && actions) {
if (actions.includes('CREATE')) {
addActionToList(Collection, 'CREATE', 'POST', path);
router.post('', setAction(Collection, 'CREATE'), auth, (req, res) => create(req, res, Collection, configs));
}
if (actions.includes('LIST')) {
addActionToList(Collection, 'LIST', 'GET', path);
router.get('', setAction(Collection, 'LIST'), auth, (req, res) => readMany(req, res, Collection, configs));
}
if (actions.includes('UPDATE')) {
addActionToList(Collection, 'UPDATE', 'PUT', `${path}/:_id`);
router.put('/:_id', setAction(Collection, 'UPDATE'), auth, (req, res) => update(req, res, Collection, configs));
}
if (actions.includes('DETAIL')) {
addActionToList(Collection, 'DETAIL', 'POST', `${path}/find-in-ids`);
router.post('/find-in-ids', setAction(Collection, 'DETAIL'), auth, (req, res) => getByIds(req, res, Collection, configs));
addActionToList(Collection, 'DETAIL', 'GET', `${path}/:_id`);
router.get('/:_id', setAction(Collection, 'DETAIL'), auth, (req, res) => readOne(req, res, Collection, configs));
}
if (actions.includes('DELETE')) {
addActionToList(Collection, 'DELETE', 'DELETE', `${path}/:_id`);
router.delete('/:_id', setAction(Collection, 'DELETE'), auth, (req, res) => remove(req, res, Collection, configs));
}
if (actions.includes('DELETE_PERMANENTLY')) {
addActionToList(Collection, 'DELETE_PERMANENTLY', 'DELETE', `${path}/:_id/permanently`);
router.delete('/:_id/permanently', setAction(Collection, 'DELETE_PERMANENTLY'), auth, (req, res) => removePermanently(req, res, Collection, configs));
}
if (actions.includes('RESTORE')) {
addActionToList(Collection, 'RESTORE', 'PUT', `${path}/:_id/restore`);
router.put('/:_id/restore', setAction(Collection, 'RESTORE'), auth, (req, res) => restore(req, res, Collection, configs));
}
if (actions.includes('COUNT')) {
addActionToList(Collection, 'COUNT', 'GET', `${path}/count`);
router.get('/count', setAction(Collection, 'COUNT'), auth, (req, res) => count(req, res, Collection, configs));
}
if (actions.includes('EXPORT')) {
addActionToList(Collection, 'EXPORT', 'GET', `${path}/export`);
router.get('/export', setAction(Collection, 'EXPORT'), auth, (req, res) => exportToExcel(req, res, Collection, configs));
}
} else if (actions) {
if (actions.includes('CREATE')) {
addActionToList(Collection, 'CREATE', 'POST', path);
router.post('', (req, res) => create(req, res, Collection, configs));
}
if (actions.includes('UPDATE')) {
addActionToList(Collection, 'UPDATE', 'PUT', `${path}/:_id`);
router.put('/:_id', (req, res) => update(req, res, Collection, configs));
}
if (actions.includes('LIST')) {
addActionToList(Collection, 'LIST', 'GET', path);
router.get('', (req, res) => readMany(req, res, Collection, configs));
}
if (actions.includes('DETAIL')) {
addActionToList(Collection, 'DETAIL', 'POST', `${path}/find-in-ids`);
router.post('/find-in-ids', (req, res) => getByIds(req, res, Collection, configs));
addActionToList(Collection, 'DETAIL', 'GET', `${path}/:_id`);
router.get('/:_id', (req, res) => readOne(req, res, Collection, configs));
}
if (actions.includes('DELETE')) {
addActionToList(Collection, 'DELETE', 'DELETE', `${path}/:_id`);
router.delete('/:_id', (req, res) => remove(req, res, Collection, configs));
}
if (actions.includes('DELETE_PERMANENTLY')) {
addActionToList(Collection, 'DELETE_PERMANENTLY', 'DELETE', `${path}/:_id/permanently`);
router.delete('/:_id/permanently', (req, res) => removePermanently(req, res, Collection, configs));
}
if (actions.includes('RESTORE')) {
addActionToList(Collection, 'RESTORE', 'PUT', `${path}/:_id/restore`);
router.put('/:_id/restore', (req, res) => restore(req, res, Collection, configs));
}
if (actions.includes('COUNT')) {
addActionToList(Collection, 'COUNT', 'GET', `${path}/count`);
router.get('/count', (req, res) => count(req, res, Collection, configs));
}
if (actions.includes('EXPORT')) {
addActionToList(Collection, 'EXPORT', 'GET', `${path}/export`);
router.get('/export', (req, res) => exportToExcel(req, res, Collection, configs));
}
}
return router;
};
module.exports.getModels = () => {
return Array.from(modelNamesSet).map(modelName => ({
name: _.startCase(modelName),
key: modelName.toLowerCase()
}));
};
const setAction = (Collection, action) => {
return async (req, res, next) => {
req.key = `${Collection.modelName}_${action}`.toUpperCase();
next();
};
};
const addActionToList = (Collection, action, method, path) => {
const controllerKey = Collection.modelName.toUpperCase();
actionList.push({
collection_key: controllerKey.toLowerCase(),
controller: controllerKey,
controller_name: _.startCase(controllerKey.toLowerCase()),
key: `${Collection.modelName}_${action}`.toUpperCase(),
name: _.startCase(`${Collection.modelName} ${action}`.toLocaleLowerCase()),
path,
method,
type: action,
is_extend: false,
includes: [],
});
};
const addPathModelToList = (Collection, path) => {
if (path) {
modelList.push({
model_name: Collection.modelName,
name: _.startCase(`${Collection.modelName}`.toLocaleLowerCase()),
path
});
}
};