From 4c9ae36c5c1bf7a792cef3850fed6413c1e5edee Mon Sep 17 00:00:00 2001 From: Joey Baker Date: Tue, 6 Sep 2016 11:24:41 -0700 Subject: [PATCH] Refactor: move get-config-for-method to a file --- src/crud.js | 87 +---------------------------------- src/get-config-for-method.js | 88 ++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 86 deletions(-) create mode 100644 src/get-config-for-method.js diff --git a/src/crud.js b/src/crud.js index 0006b3b..64e44b7 100644 --- a/src/crud.js +++ b/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, diff --git a/src/get-config-for-method.js b/src/get-config-for-method.js new file mode 100644 index 0000000..de8c828 --- /dev/null +++ b/src/get-config-for-method.js @@ -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; +};