strapi/packages/core/utils/src/relations.ts

27 lines
1.0 KiB
TypeScript
Raw Normal View History

2023-04-27 23:18:48 +02:00
import { Attribute, Model } from './types';
2020-12-01 16:38:47 +01:00
2023-04-27 23:18:48 +02:00
import { isRelationalAttribute } from './content-types';
2022-10-03 18:54:13 +02:00
2021-08-06 10:51:34 +02:00
const MANY_RELATIONS = ['oneToMany', 'manyToMany'];
2020-12-18 11:24:52 +01:00
2023-04-27 23:18:48 +02:00
const getRelationalFields = (contentType: Model) => {
2022-08-08 23:33:39 +02:00
return Object.keys(contentType.attributes).filter((attributeName) => {
2021-07-08 18:15:32 +02:00
return contentType.attributes[attributeName].type === 'relation';
});
2020-12-18 11:24:52 +01:00
};
2020-12-01 16:38:47 +01:00
2023-04-27 23:18:48 +02:00
const isOneToAny = (attribute: Attribute) =>
2022-10-03 18:54:13 +02:00
isRelationalAttribute(attribute) && ['oneToOne', 'oneToMany'].includes(attribute.relation);
2023-04-27 23:18:48 +02:00
const isManyToAny = (attribute: Attribute) =>
2022-10-03 18:54:13 +02:00
isRelationalAttribute(attribute) && ['manyToMany', 'manyToOne'].includes(attribute.relation);
2023-04-27 23:18:48 +02:00
const isAnyToOne = (attribute: Attribute) =>
2022-10-03 18:54:13 +02:00
isRelationalAttribute(attribute) && ['oneToOne', 'manyToOne'].includes(attribute.relation);
2023-04-27 23:18:48 +02:00
const isAnyToMany = (attribute: Attribute) =>
2022-10-03 18:54:13 +02:00
isRelationalAttribute(attribute) && ['oneToMany', 'manyToMany'].includes(attribute.relation);
2023-04-27 23:18:48 +02:00
export const constants = {
MANY_RELATIONS,
2020-12-01 16:38:47 +01:00
};
2023-04-27 23:18:48 +02:00
export { getRelationalFields, isOneToAny, isManyToAny, isAnyToOne, isAnyToMany };