90 lines
1.7 KiB
JavaScript
Raw Normal View History

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