mirror of
https://github.com/strapi/strapi.git
synced 2025-09-26 17:00:55 +00:00
feat: getSortableAttributes
This commit is contained in:
parent
4a7a82fb1b
commit
aa1247f557
@ -1,9 +1,12 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
|
const { intersection } = require('lodash/fp');
|
||||||
const { contentTypes: contentTypesUtils } = require('@strapi/utils');
|
const { contentTypes: contentTypesUtils } = require('@strapi/utils');
|
||||||
|
|
||||||
const { PUBLISHED_AT_ATTRIBUTE } = contentTypesUtils.constants;
|
const { getNonVisibleAttributes, getWritableAttributes } = contentTypesUtils;
|
||||||
|
const { PUBLISHED_AT_ATTRIBUTE, CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE } =
|
||||||
|
contentTypesUtils.constants;
|
||||||
|
|
||||||
const NON_SORTABLES = ['component', 'json', 'media', 'richtext', 'dynamiczone'];
|
const NON_SORTABLES = ['component', 'json', 'media', 'richtext', 'dynamiczone'];
|
||||||
const SORTABLE_RELATIONS = ['oneToOne', 'manyToOne'];
|
const SORTABLE_RELATIONS = ['oneToOne', 'manyToOne'];
|
||||||
@ -151,6 +154,30 @@ const findFirstStringAttribute = (schema) => {
|
|||||||
|
|
||||||
const getDefaultMainField = (schema) => findFirstStringAttribute(schema) || 'id';
|
const getDefaultMainField = (schema) => findFirstStringAttribute(schema) || 'id';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns list of all sortable attributes for a given content type schema
|
||||||
|
* TODO V5: Refactor non visible fields to be a part of content-manager schema so we can use isSortable instead
|
||||||
|
* @param {*} schema
|
||||||
|
* @returns
|
||||||
|
*/
|
||||||
|
const getSortableAttributes = (schema) => {
|
||||||
|
const validAttributes = Object.keys(schema.attributes).filter((key) => isListable(schema, key));
|
||||||
|
|
||||||
|
const model = strapi.getModel(schema.uid);
|
||||||
|
const nonVisibleWritableAttributes = intersection(
|
||||||
|
getNonVisibleAttributes(model),
|
||||||
|
getWritableAttributes(model)
|
||||||
|
);
|
||||||
|
|
||||||
|
return [
|
||||||
|
'id',
|
||||||
|
...validAttributes,
|
||||||
|
...nonVisibleWritableAttributes,
|
||||||
|
CREATED_BY_ATTRIBUTE,
|
||||||
|
UPDATED_BY_ATTRIBUTE,
|
||||||
|
];
|
||||||
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
isSortable,
|
isSortable,
|
||||||
isVisible,
|
isVisible,
|
||||||
@ -160,4 +187,5 @@ module.exports = {
|
|||||||
hasEditableAttribute,
|
hasEditableAttribute,
|
||||||
hasRelationAttribute,
|
hasRelationAttribute,
|
||||||
getDefaultMainField,
|
getDefaultMainField,
|
||||||
|
getSortableAttributes,
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user