From 9a28dbcc02bde8bb4d714fa423d75c57cfb60fda Mon Sep 17 00:00:00 2001 From: Ruben Paz Date: Sun, 5 Jun 2016 14:44:24 +0100 Subject: [PATCH 1/2] Added defaultConfig parameter to the options --- src/associations/associate.js | 6 +++++- src/associations/one-to-many.js | 26 ++++++++++++++++--------- src/associations/one-to-one.js | 18 +++++++++++++---- src/crud.js | 34 ++++++++++++++++++++++----------- 4 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/associations/associate.js b/src/associations/associate.js index 92667b5..5a19a22 100644 --- a/src/associations/associate.js +++ b/src/associations/associate.js @@ -4,9 +4,11 @@ import { capitalize } from 'lodash/string'; import { getMethod } from '../utils'; let prefix; +let defaultConfig; export default (server, a, b, names, options) => { prefix = options.prefix; + defaultConfig = options.defaultConfig; server.route({ method: 'GET', @@ -31,6 +33,8 @@ export default (server, a, b, names, options) => { await fn(instanceb); reply([instancea, instanceb]); - } + }, + + config: defaultConfig }) } diff --git a/src/associations/one-to-many.js b/src/associations/one-to-many.js index 70b8edd..89256eb 100644 --- a/src/associations/one-to-many.js +++ b/src/associations/one-to-many.js @@ -4,9 +4,11 @@ import _ from 'lodash'; import { parseInclude, parseWhere, getMethod } from '../utils'; let prefix; +let defaultConfig; export default (server, a, b, names, options) => { prefix = options.prefix; + defaultConfig = options.defaultConfig; get(server, a, b, names); list(server, a, b, names); @@ -38,7 +40,9 @@ export const get = (server, a, b, names) => { }, include }); reply(list); - } + }, + + config: defaultConfig }) } @@ -62,7 +66,9 @@ export const list = (server, a, b, names) => { const list = await method({ where, include }); reply(list); - } + }, + + config: defaultConfig }) } @@ -94,14 +100,14 @@ export const scope = (server, a, b, names) => { reply(list); }, - config: { + config: _.defaults({ validate: { params: joi.object().keys({ scope: joi.string().valid(...scopes), aid: joi.number().integer().required() }) } - } + }, defaultConfig) }) } @@ -130,14 +136,14 @@ export const scopeScope = (server, a, b, names) => { reply(list); }, - config: { + config: _.defaults({ validate: { params: joi.object().keys({ scopea: joi.string().valid(...scopes.a), scopeb: joi.string().valid(...scopes.b) }) } - } + }, defaultConfig) }) } @@ -199,14 +205,14 @@ export const destroyScope = (server, a, b, names) => { reply(list); }, - config: { + config: _.defaults({ validate: { params: joi.object().keys({ scope: joi.string().valid(...scopes), aid: joi.number().integer().required() }) } - } + }, defaultConfig) }); } @@ -232,6 +238,8 @@ export const update = (server, a, b, names) => { await* list.map(instance => instance.update(request.payload)); reply(list); - } + }, + + config: defaultConfig }) } diff --git a/src/associations/one-to-one.js b/src/associations/one-to-one.js index eb7f564..8dfbc4a 100644 --- a/src/associations/one-to-one.js +++ b/src/associations/one-to-one.js @@ -4,9 +4,11 @@ import _ from 'lodash'; import { parseInclude, parseWhere, getMethod } from '../utils'; let prefix; +let defaultConfig; export default (server, a, b, names, options) => { prefix = options.prefix; + defaultConfig = options.defaultConfig; get(server, a, b, names); create(server, a, b, names); @@ -38,7 +40,9 @@ export const get = (server, a, b, names) => { } else { reply(list); } - } + }, + + config: defaultConfig }) } @@ -59,7 +63,9 @@ export const create = (server, a, b, names) => { const instance = await method(request.payload); reply(instance); - } + }, + + config: defaultConfig }) } @@ -84,7 +90,9 @@ export const destroy = (server, a, b, names) => { await instance.destroy(); reply(instance); - } + }, + + config: defaultConfig }) } @@ -110,6 +118,8 @@ export const update = (server, a, b, names) => { await instance.update(request.payload); reply(instance); - } + }, + + config: defaultConfig }) } diff --git a/src/crud.js b/src/crud.js index 0ee8dc4..4ba5d54 100644 --- a/src/crud.js +++ b/src/crud.js @@ -4,9 +4,11 @@ import _ from 'lodash'; import { parseInclude, parseWhere } from './utils'; let prefix; +let defaultConfig; export default (server, model, options) => { prefix = options.prefix; + defaultConfig = options.defaultConfig; list(server, model); get(server, model); @@ -33,7 +35,9 @@ export const list = (server, model) => { }); reply(list); - } + }, + + config: defaultConfig }); } @@ -52,13 +56,13 @@ export const get = (server, model) => { reply(instance); }, - config: { + config: _.defaults({ validate: { params: joi.object().keys({ id: joi.number().integer() }) } - } + }, defaultConfig) }) } @@ -78,13 +82,13 @@ export const scope = (server, model) => { reply(list); }, - config: { + config: _.defaults({ validate: { params: joi.object().keys({ scope: joi.string().valid(...scopes) }) } - } + }, defaultConfig) }); } @@ -98,7 +102,9 @@ export const create = (server, model) => { const instance = await model.create(request.payload); reply(instance); - } + }, + + config: defaultConfig }) } @@ -118,7 +124,9 @@ export const destroy = (server, model) => { await* list.map(instance => instance.destroy()); reply(list.length === 1 ? list[0] : list); - } + }, + + config: defaultConfig }) } @@ -137,7 +145,9 @@ export const destroyAll = (server, model) => { await* list.map(instance => instance.destroy()); reply(list.length === 1 ? list[0] : list); - } + }, + + config: defaultConfig }) } @@ -159,13 +169,13 @@ export const destroyScope = (server, model) => { reply(list); }, - config: { + config: _.defaults({ validate: { params: joi.object().keys({ scope: joi.string().valid(...scopes) }) } - } + }, defaultConfig) }); } @@ -185,7 +195,9 @@ export const update = (server, model) => { await instance.update(request.payload); reply(instance); - } + }, + + config: defaultConfig }) } From 2fd7fa09b06033a31baca0e24baec0b7d3d44039 Mon Sep 17 00:00:00 2001 From: Ruben Paz Date: Sun, 5 Jun 2016 17:51:54 +0100 Subject: [PATCH 2/2] Replace defaults by defaultsDeep --- src/associations/one-to-many.js | 6 +++--- src/crud.js | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/associations/one-to-many.js b/src/associations/one-to-many.js index 89256eb..dfcd391 100644 --- a/src/associations/one-to-many.js +++ b/src/associations/one-to-many.js @@ -100,7 +100,7 @@ export const scope = (server, a, b, names) => { reply(list); }, - config: _.defaults({ + config: _.defaultsDeep({ validate: { params: joi.object().keys({ scope: joi.string().valid(...scopes), @@ -136,7 +136,7 @@ export const scopeScope = (server, a, b, names) => { reply(list); }, - config: _.defaults({ + config: _.defaultsDeep({ validate: { params: joi.object().keys({ scopea: joi.string().valid(...scopes.a), @@ -205,7 +205,7 @@ export const destroyScope = (server, a, b, names) => { reply(list); }, - config: _.defaults({ + config: _.defaultsDeep({ validate: { params: joi.object().keys({ scope: joi.string().valid(...scopes), diff --git a/src/crud.js b/src/crud.js index 4ba5d54..5af5d2a 100644 --- a/src/crud.js +++ b/src/crud.js @@ -56,7 +56,7 @@ export const get = (server, model) => { reply(instance); }, - config: _.defaults({ + config: _.defaultsDeep({ validate: { params: joi.object().keys({ id: joi.number().integer() @@ -82,7 +82,7 @@ export const scope = (server, model) => { reply(list); }, - config: _.defaults({ + config: _.defaultsDeep({ validate: { params: joi.object().keys({ scope: joi.string().valid(...scopes) @@ -169,7 +169,7 @@ export const destroyScope = (server, model) => { reply(list); }, - config: _.defaults({ + config: _.defaultsDeep({ validate: { params: joi.object().keys({ scope: joi.string().valid(...scopes)