2016-09-28 21:17:16 -07:00
|
|
|
import { omit, identity, toNumber, isString, isUndefined } from 'lodash';
|
2016-09-03 18:48:03 -07:00
|
|
|
import { notImplemented } from 'boom';
|
2016-03-10 10:48:30 +03:30
|
|
|
|
2016-09-28 21:17:16 -07:00
|
|
|
const sequelizeKeys = ['include', 'order', 'limit', 'offset'];
|
|
|
|
|
2016-03-10 10:48:30 +03:30
|
|
|
export const parseInclude = request => {
|
2016-09-28 21:17:16 -07:00
|
|
|
const include = Array.isArray(request.query.include)
|
|
|
|
? request.query.include
|
|
|
|
: [request.query.include]
|
|
|
|
;
|
2016-03-10 10:48:30 +03:30
|
|
|
|
2016-07-05 18:33:03 -07:00
|
|
|
const noGetDb = typeof request.getDb !== 'function';
|
|
|
|
const noRequestModels = !request.models;
|
|
|
|
|
|
|
|
if (noGetDb && noRequestModels) {
|
2016-09-03 18:48:03 -07:00
|
|
|
return notImplemented('`request.getDb` or `request.models` are not defined.'
|
2016-07-09 09:41:23 +04:30
|
|
|
+ 'Be sure to load hapi-sequelize before hapi-sequelize-crud.');
|
2016-07-05 18:33:03 -07:00
|
|
|
}
|
|
|
|
|
2016-07-09 09:41:23 +04:30
|
|
|
const { models } = noGetDb ? request : request.getDb();
|
2016-07-05 18:33:03 -07:00
|
|
|
|
2016-03-10 10:48:30 +03:30
|
|
|
return include.map(a => {
|
2016-07-05 18:33:03 -07:00
|
|
|
if (typeof a === 'string') return models[a];
|
2016-03-10 10:48:30 +03:30
|
|
|
|
|
|
|
if (a && typeof a.model === 'string' && a.model.length) {
|
2016-07-05 18:33:03 -07:00
|
|
|
a.model = models[a.model];
|
2016-03-10 10:48:30 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
return a;
|
2016-07-05 18:33:03 -07:00
|
|
|
}).filter(identity);
|
2016-06-30 18:28:02 +04:30
|
|
|
};
|
2016-03-10 10:48:30 +03:30
|
|
|
|
|
|
|
export const parseWhere = request => {
|
2016-09-28 21:17:16 -07:00
|
|
|
const where = omit(request.query, sequelizeKeys);
|
2016-03-10 10:48:30 +03:30
|
|
|
|
|
|
|
for (const key of Object.keys(where)) {
|
|
|
|
try {
|
|
|
|
where[key] = JSON.parse(where[key]);
|
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return where;
|
2016-06-30 18:28:02 +04:30
|
|
|
};
|
2016-03-10 10:48:30 +03:30
|
|
|
|
2016-09-28 21:17:16 -07:00
|
|
|
export const parseLimitAndOffset = (request) => {
|
|
|
|
const { limit, offset } = request.query;
|
|
|
|
const out = {};
|
|
|
|
if (!isUndefined(limit)) {
|
|
|
|
out.limit = toNumber(limit);
|
|
|
|
}
|
|
|
|
if (!isUndefined(offset)) {
|
|
|
|
out.offset = toNumber(offset);
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const parseOrder = (request) => {
|
|
|
|
const { order } = request.query;
|
|
|
|
|
|
|
|
if (!order) return null;
|
|
|
|
|
|
|
|
// transform to an array so sequelize will escape the input for us and
|
|
|
|
// maintain security. See http://docs.sequelizejs.com/en/latest/docs/querying/#ordering
|
|
|
|
if (isString(order)) return order.split(' ');
|
|
|
|
|
|
|
|
for (const key of Object.keys(order)) {
|
|
|
|
try {
|
|
|
|
order[key] = JSON.parse(order[key]);
|
|
|
|
} catch (e) {
|
|
|
|
//
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return order;
|
|
|
|
};
|
|
|
|
|
2016-03-10 10:48:30 +03:30
|
|
|
export const getMethod = (model, association, plural = true, method = 'get') => {
|
|
|
|
const a = plural ? association.original.plural : association.original.singular;
|
|
|
|
const b = plural ? association.original.singular : association.original.plural; // alternative
|
|
|
|
const fn = model[`${method}${a}`] || model[`${method}${b}`];
|
|
|
|
if (fn) return fn.bind(model);
|
|
|
|
|
|
|
|
return false;
|
2016-06-30 18:28:02 +04:30
|
|
|
};
|