2019-07-23 10:54:44 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const NON_SORTABLES = ['group', 'json', 'relation'];
|
|
|
|
const isSortable = (schema, name) => {
|
|
|
|
if (!_.has(schema.attributes, name)) {
|
2019-07-23 10:54:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const attribute = schema.attributes[name];
|
2019-07-23 10:54:44 +02:00
|
|
|
if (NON_SORTABLES.includes(attribute.type)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const isSearchable = (schema, name) => {
|
|
|
|
return isSortable(schema, name);
|
2019-07-23 10:54:44 +02:00
|
|
|
};
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const isVisible = (schema, name) => {
|
|
|
|
if (!_.has(schema.attributes, name)) {
|
2019-07-23 10:54:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
if (isTimestamp(schema, name) || name === 'id') {
|
2019-07-23 10:54:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const isTimestamp = (schema, name) => {
|
|
|
|
if (!_.has(schema.attributes, name)) {
|
2019-07-23 10:54:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const timestampsOpt = _.get(schema, ['options', 'timestamps']);
|
|
|
|
if (!timestampsOpt || !Array.isArray(timestampsOpt)) {
|
2019-07-23 10:54:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
if (timestampsOpt.includes(name)) {
|
|
|
|
return true;
|
2019-07-23 10:54:44 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-07-24 11:51:35 +02:00
|
|
|
const isRelation = attribute => attribute.type === 'relation';
|
|
|
|
|
2019-07-23 10:54:44 +02:00
|
|
|
module.exports = {
|
|
|
|
isSortable,
|
2019-07-24 11:51:35 +02:00
|
|
|
isVisible,
|
|
|
|
isSearchable,
|
|
|
|
isRelation,
|
2019-07-23 10:54:44 +02:00
|
|
|
};
|