Refactor: move get-config-for-method to a file
This commit is contained in:
parent
4558ad1327
commit
4c9ae36c5c
87
src/crud.js
87
src/crud.js
@ -5,92 +5,7 @@ import _ from 'lodash';
|
||||
import { parseInclude, parseWhere } from './utils';
|
||||
import { notFound } from 'boom';
|
||||
import * as associations from './associations/index';
|
||||
|
||||
const sequelizeOperators = {
|
||||
$and: joi.any(),
|
||||
$or: joi.any(),
|
||||
$gt: joi.any(),
|
||||
$gte: joi.any(),
|
||||
$lt: joi.any(),
|
||||
$lte: joi.any(),
|
||||
$ne: joi.any(),
|
||||
$eq: joi.any(),
|
||||
$not: joi.any(),
|
||||
$between: joi.any(),
|
||||
$notBetween: joi.any(),
|
||||
$in: joi.any(),
|
||||
$notIn: joi.any(),
|
||||
$like: joi.any(),
|
||||
$notLike: joi.any(),
|
||||
$iLike: joi.any(),
|
||||
$notILike: joi.any(),
|
||||
$overlap: joi.any(),
|
||||
$contains: joi.any(),
|
||||
$contained: joi.any(),
|
||||
$any: joi.any(),
|
||||
$col: joi.any(),
|
||||
};
|
||||
|
||||
const whereMethods = [
|
||||
'list',
|
||||
'get',
|
||||
'scope',
|
||||
'destroy',
|
||||
'destoryScope',
|
||||
'destroyAll',
|
||||
];
|
||||
|
||||
const includeMethods = [
|
||||
'list',
|
||||
'get',
|
||||
'scope',
|
||||
'destoryScope',
|
||||
];
|
||||
|
||||
const payloadMethods = [
|
||||
'create',
|
||||
'update',
|
||||
];
|
||||
|
||||
const getConfigForMethod = ({ method, attributeValidation, associationValidation, config }) => {
|
||||
const hasWhere = whereMethods.includes(method);
|
||||
const hasInclude = includeMethods.includes(method);
|
||||
const hasPayload = payloadMethods.includes(method);
|
||||
const methodConfig = { ...config };
|
||||
|
||||
if (hasWhere) {
|
||||
_.defaultsDeep(methodConfig, {
|
||||
validate: {
|
||||
query: {
|
||||
...attributeValidation,
|
||||
...sequelizeOperators,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (hasInclude) {
|
||||
_.defaultsDeep(methodConfig, {
|
||||
validate: {
|
||||
query: {
|
||||
...associationValidation,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (hasPayload) {
|
||||
_.defaultsDeep(methodConfig, {
|
||||
validate: {
|
||||
payload: {
|
||||
...attributeValidation,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return methodConfig;
|
||||
};
|
||||
import getConfigForMethod from './get-config-for-method.js';
|
||||
|
||||
const createAll = ({
|
||||
server,
|
||||
|
88
src/get-config-for-method.js
Normal file
88
src/get-config-for-method.js
Normal file
@ -0,0 +1,88 @@
|
||||
import { defaultsDeep } from 'lodash';
|
||||
import joi from 'joi';
|
||||
|
||||
export const sequelizeOperators = {
|
||||
$and: joi.any(),
|
||||
$or: joi.any(),
|
||||
$gt: joi.any(),
|
||||
$gte: joi.any(),
|
||||
$lt: joi.any(),
|
||||
$lte: joi.any(),
|
||||
$ne: joi.any(),
|
||||
$eq: joi.any(),
|
||||
$not: joi.any(),
|
||||
$between: joi.any(),
|
||||
$notBetween: joi.any(),
|
||||
$in: joi.any(),
|
||||
$notIn: joi.any(),
|
||||
$like: joi.any(),
|
||||
$notLike: joi.any(),
|
||||
$iLike: joi.any(),
|
||||
$notILike: joi.any(),
|
||||
$overlap: joi.any(),
|
||||
$contains: joi.any(),
|
||||
$contained: joi.any(),
|
||||
$any: joi.any(),
|
||||
$col: joi.any(),
|
||||
};
|
||||
|
||||
export const whereMethods = [
|
||||
'list',
|
||||
'get',
|
||||
'scope',
|
||||
'destroy',
|
||||
'destoryScope',
|
||||
'destroyAll',
|
||||
];
|
||||
|
||||
export const includeMethods = [
|
||||
'list',
|
||||
'get',
|
||||
'scope',
|
||||
'destoryScope',
|
||||
];
|
||||
|
||||
export const payloadMethods = [
|
||||
'create',
|
||||
'update',
|
||||
];
|
||||
|
||||
export default ({ method, attributeValidation, associationValidation, config = {} }) => {
|
||||
const hasWhere = whereMethods.includes(method);
|
||||
const hasInclude = includeMethods.includes(method);
|
||||
const hasPayload = payloadMethods.includes(method);
|
||||
const methodConfig = { ...config };
|
||||
|
||||
if (hasWhere) {
|
||||
defaultsDeep(methodConfig, {
|
||||
validate: {
|
||||
query: {
|
||||
...attributeValidation,
|
||||
...sequelizeOperators,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (hasInclude) {
|
||||
defaultsDeep(methodConfig, {
|
||||
validate: {
|
||||
query: {
|
||||
...associationValidation,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (hasPayload) {
|
||||
defaultsDeep(methodConfig, {
|
||||
validate: {
|
||||
payload: {
|
||||
...attributeValidation,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return methodConfig;
|
||||
};
|
Loading…
Reference in New Issue
Block a user