Use full UIDs in CTM

This commit is contained in:
Alexandre Bodin 2019-11-14 12:05:01 +01:00
parent ab51bbebe4
commit 50ab4c88b9
4 changed files with 67 additions and 124 deletions

View File

@ -81,9 +81,7 @@ async function updateContentTypesScope(models, configurations, source) {
// delette old schemas
await Promise.all(
contentTypesToDelete.map(uid =>
service.deleteConfiguration({ uid, source })
)
contentTypesToDelete.map(uid => service.deleteConfiguration({ uid }))
);
// create new schemas

View File

@ -1,49 +1,21 @@
'use strict';
const { createModelConfigurationSchema } = require('./validation');
const service = require('../services/ContentTypes');
const componentService = require('../services/Components');
module.exports = {
/**
* Returns the list of available content types
*/
listContentTypes(ctx) {
const service = strapi.plugins['content-manager'].services.contenttypes;
const userModels = Object.keys(strapi.models)
.filter(key => key !== 'core_store')
.map(uid => {
return service.formatContentType(uid, strapi.models[uid]);
});
const shouldDisplayPluginModel = uid => {
if (['file', 'permission', 'role'].includes(uid)) {
return false;
}
return true;
};
const pluginModels = Object.keys(strapi.plugins)
.map(pluginKey => {
const plugin = strapi.plugins[pluginKey];
return Object.keys(plugin.models || {}).map(uid => {
return service.formatContentType(uid, plugin.models[uid], {
uid,
isDisplayed: shouldDisplayPluginModel(uid),
source: pluginKey,
});
});
})
.reduce((acc, models) => acc.concat(models), []);
const adminModels = Object.keys(strapi.admin.models).map(uid => {
return service.formatContentType(uid, strapi.admin.models[uid], {
isDisplayed: false,
source: 'admin',
});
const contentTypes = Object.keys(strapi.contentTypes).map(uid => {
return service.formatContentType(strapi.contentTypes[uid]);
});
ctx.body = { data: [...userModels, ...pluginModels, ...adminModels] };
ctx.body = {
data: contentTypes,
};
},
/**
@ -56,29 +28,20 @@ module.exports = {
*/
async findContentType(ctx) {
const { uid } = ctx.params;
const { source } = ctx.query;
const service = strapi.plugins['content-manager'].services.contenttypes;
const componentService =
strapi.plugins['content-manager'].services.components;
const contentType = service.findContentTypeModel({
uid,
source,
});
const contentType = strapi.contentTypes[uid];
if (!contentType) {
return ctx.notFound('contentType.notFound');
}
const contentTypeConfigurations = await service.getConfiguration({
uid,
source,
});
const contentTypeConfigurations = await service.getConfiguration(
contentType
);
const data = {
contentType: {
uid,
source,
schema: service.formatContentTypeSchema(contentType),
...contentTypeConfigurations,
},
@ -97,15 +60,10 @@ module.exports = {
*/
async updateContentType(ctx) {
const { uid } = ctx.params;
const { source } = ctx.query;
const { body } = ctx.request;
const service = strapi.plugins['content-manager'].services.contenttypes;
// try to find the model
const contentType = service.findContentTypeModel({
uid,
source,
});
const contentType = strapi.contentTypes[uid];
if (!contentType) {
return ctx.notFound('contentType.notFound');
@ -130,16 +88,14 @@ module.exports = {
});
}
await service.setConfiguration({ uid, source }, input);
await service.setConfiguration(contentType, input);
const contentTypeConfigurations = await service.getConfiguration({
uid,
source,
});
const contentTypeConfigurations = await service.getConfiguration(
contentType
);
const data = {
uid,
source,
schema,
...contentTypeConfigurations,
};

View File

@ -94,16 +94,4 @@ module.exports = {
const storeKey = uidToStoreKey(uid);
return storeUtils.deleteKey(storeKey);
},
async updateUID(oldUID, newUID) {
const oldKey = uidToStoreKey(oldUID);
const newKey = uidToStoreKey(newUID);
await storeUtils.setModelConfiguration(oldKey, {
uid: oldUID,
isComponent: true,
});
return storeUtils.moveKey(oldKey, newKey);
},
};

View File

@ -6,64 +6,68 @@ const pluralize = require('pluralize');
const storeUtils = require('./utils/store');
const { pickSchemaFields } = require('./utils/schema');
const uidToStoreKey = ({ uid, source }) => {
const sourceKey = source ? `${source}.${uid}` : uid;
return `content_types::${sourceKey}`;
const uidToStoreKey = uid => {
return `content_types::${uid}`;
};
const formatContentTypeLabel = label => _.upperFirst(pluralize(label));
const getModelIn = source => uid => _.get(source, ['models', uid], null);
const HIDDEN_CONTENT_TYPES = [
'strapi::admin',
'plugins::upload.file',
'plugins::users-permissions.permission',
'plugins::users-permissions.role',
];
module.exports = {
uidToStoreKey,
getConfiguration({ uid, source }) {
const storeKey = uidToStoreKey({ uid, source });
getConfiguration({ uid }) {
const storeKey = uidToStoreKey(uid);
return storeUtils.getModelConfiguration(storeKey);
},
setConfiguration({ uid, source }, input) {
setConfiguration({ uid }, input) {
const { settings, metadatas, layouts } = input;
const storeKey = uidToStoreKey({ uid, source });
const storeKey = uidToStoreKey(uid);
return storeUtils.setModelConfiguration(storeKey, {
uid,
source,
settings,
metadatas,
layouts,
});
},
deleteConfiguration({ uid, source }) {
const storeKey = uidToStoreKey({ uid, source });
deleteConfiguration({ uid }) {
const storeKey = uidToStoreKey(uid);
return storeUtils.deleteKey(storeKey);
},
formatContentType(uid, contentType, opts = {}) {
const { source = null, isDisplayed = true } = opts;
formatContentType(contentType) {
return {
uid,
name: uid,
label: formatContentTypeLabel(_.get(contentType, ['info', 'name'], uid)),
isDisplayed,
source,
uid: contentType.uid,
name: _.get(contentType, ['info', 'name']),
label: formatContentTypeLabel(
_.get(contentType, ['info', 'name'], contentType.modelName)
),
isDisplayed: HIDDEN_CONTENT_TYPES.includes(contentType.uid)
? false
: true,
schema: this.formatContentTypeSchema(contentType),
};
},
formatContentTypeSchema(contentType) {
const { associations, allAttributes } = contentType;
const { associations, attributes } = contentType;
return {
...pickSchemaFields(contentType),
attributes: {
id: {
type: contentType.primaryKeyType,
},
...Object.keys(allAttributes).reduce((acc, key) => {
const attribute = allAttributes[key];
...Object.keys(attributes).reduce((acc, key) => {
const attribute = attributes[key];
const assoc = associations.find(assoc => assoc.alias === key);
if (assoc) {
@ -84,38 +88,35 @@ module.exports = {
relationType: assoc.nature,
};
}
} else {
if (attribute.type === 'timestampUpdate') {
attribute.type = 'timestamp';
}
acc[key] = attribute;
return acc;
}
acc[key] = attribute;
return acc;
}, {}),
...addTimestamps(contentType),
},
};
},
findContentTypeModel({ uid, source }) {
const getModel = getModelIn(
!source
? strapi
: source === 'admin'
? strapi.admin
: strapi.plugins[source] || {}
);
return getModel(uid);
},
async updateUID({ oldUID, newUID, source }) {
const oldKey = uidToStoreKey({ uid: oldUID, source });
const newKey = uidToStoreKey({ uid: newUID, source });
await storeUtils.setModelConfiguration(oldKey, {
uid: oldUID,
});
return storeUtils.moveKey(oldKey, newKey);
},
};
function addTimestamps(contentType) {
if (_.get(contentType, 'options.timestamps', false) === false) {
return {};
}
const [createdAtAttribute, updatedAtAttribute] = _.get(contentType, [
'options',
'timestamps',
]);
return {
[createdAtAttribute]: {
type: 'timestamp',
},
[updatedAtAttribute]: {
type: 'timestamp',
},
};
}