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 // delette old schemas
await Promise.all( await Promise.all(
contentTypesToDelete.map(uid => contentTypesToDelete.map(uid => service.deleteConfiguration({ uid }))
service.deleteConfiguration({ uid, source })
)
); );
// create new schemas // create new schemas

View File

@ -1,49 +1,21 @@
'use strict'; 'use strict';
const { createModelConfigurationSchema } = require('./validation'); const { createModelConfigurationSchema } = require('./validation');
const service = require('../services/ContentTypes');
const componentService = require('../services/Components');
module.exports = { module.exports = {
/** /**
* Returns the list of available content types * Returns the list of available content types
*/ */
listContentTypes(ctx) { listContentTypes(ctx) {
const service = strapi.plugins['content-manager'].services.contenttypes; const contentTypes = Object.keys(strapi.contentTypes).map(uid => {
return service.formatContentType(strapi.contentTypes[uid]);
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',
});
}); });
ctx.body = { data: [...userModels, ...pluginModels, ...adminModels] }; ctx.body = {
data: contentTypes,
};
}, },
/** /**
@ -56,29 +28,20 @@ module.exports = {
*/ */
async findContentType(ctx) { async findContentType(ctx) {
const { uid } = ctx.params; 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({ const contentType = strapi.contentTypes[uid];
uid,
source,
});
if (!contentType) { if (!contentType) {
return ctx.notFound('contentType.notFound'); return ctx.notFound('contentType.notFound');
} }
const contentTypeConfigurations = await service.getConfiguration({ const contentTypeConfigurations = await service.getConfiguration(
uid, contentType
source, );
});
const data = { const data = {
contentType: { contentType: {
uid, uid,
source,
schema: service.formatContentTypeSchema(contentType), schema: service.formatContentTypeSchema(contentType),
...contentTypeConfigurations, ...contentTypeConfigurations,
}, },
@ -97,15 +60,10 @@ module.exports = {
*/ */
async updateContentType(ctx) { async updateContentType(ctx) {
const { uid } = ctx.params; const { uid } = ctx.params;
const { source } = ctx.query;
const { body } = ctx.request; const { body } = ctx.request;
const service = strapi.plugins['content-manager'].services.contenttypes;
// try to find the model // try to find the model
const contentType = service.findContentTypeModel({ const contentType = strapi.contentTypes[uid];
uid,
source,
});
if (!contentType) { if (!contentType) {
return ctx.notFound('contentType.notFound'); 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({ const contentTypeConfigurations = await service.getConfiguration(
uid, contentType
source, );
});
const data = { const data = {
uid, uid,
source,
schema, schema,
...contentTypeConfigurations, ...contentTypeConfigurations,
}; };

View File

@ -94,16 +94,4 @@ module.exports = {
const storeKey = uidToStoreKey(uid); const storeKey = uidToStoreKey(uid);
return storeUtils.deleteKey(storeKey); 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 storeUtils = require('./utils/store');
const { pickSchemaFields } = require('./utils/schema'); const { pickSchemaFields } = require('./utils/schema');
const uidToStoreKey = ({ uid, source }) => { const uidToStoreKey = uid => {
const sourceKey = source ? `${source}.${uid}` : uid; return `content_types::${uid}`;
return `content_types::${sourceKey}`;
}; };
const formatContentTypeLabel = label => _.upperFirst(pluralize(label)); 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 = { module.exports = {
uidToStoreKey, uidToStoreKey,
getConfiguration({ uid, source }) { getConfiguration({ uid }) {
const storeKey = uidToStoreKey({ uid, source }); const storeKey = uidToStoreKey(uid);
return storeUtils.getModelConfiguration(storeKey); return storeUtils.getModelConfiguration(storeKey);
}, },
setConfiguration({ uid, source }, input) { setConfiguration({ uid }, input) {
const { settings, metadatas, layouts } = input; const { settings, metadatas, layouts } = input;
const storeKey = uidToStoreKey({ uid, source }); const storeKey = uidToStoreKey(uid);
return storeUtils.setModelConfiguration(storeKey, { return storeUtils.setModelConfiguration(storeKey, {
uid, uid,
source,
settings, settings,
metadatas, metadatas,
layouts, layouts,
}); });
}, },
deleteConfiguration({ uid, source }) { deleteConfiguration({ uid }) {
const storeKey = uidToStoreKey({ uid, source }); const storeKey = uidToStoreKey(uid);
return storeUtils.deleteKey(storeKey); return storeUtils.deleteKey(storeKey);
}, },
formatContentType(uid, contentType, opts = {}) { formatContentType(contentType) {
const { source = null, isDisplayed = true } = opts;
return { return {
uid, uid: contentType.uid,
name: uid, name: _.get(contentType, ['info', 'name']),
label: formatContentTypeLabel(_.get(contentType, ['info', 'name'], uid)), label: formatContentTypeLabel(
isDisplayed, _.get(contentType, ['info', 'name'], contentType.modelName)
source, ),
isDisplayed: HIDDEN_CONTENT_TYPES.includes(contentType.uid)
? false
: true,
schema: this.formatContentTypeSchema(contentType), schema: this.formatContentTypeSchema(contentType),
}; };
}, },
formatContentTypeSchema(contentType) { formatContentTypeSchema(contentType) {
const { associations, allAttributes } = contentType; const { associations, attributes } = contentType;
return { return {
...pickSchemaFields(contentType), ...pickSchemaFields(contentType),
attributes: { attributes: {
id: { id: {
type: contentType.primaryKeyType, type: contentType.primaryKeyType,
}, },
...Object.keys(allAttributes).reduce((acc, key) => { ...Object.keys(attributes).reduce((acc, key) => {
const attribute = allAttributes[key]; const attribute = attributes[key];
const assoc = associations.find(assoc => assoc.alias === key); const assoc = associations.find(assoc => assoc.alias === key);
if (assoc) { if (assoc) {
@ -84,38 +88,35 @@ module.exports = {
relationType: assoc.nature, relationType: assoc.nature,
}; };
} }
} else {
if (attribute.type === 'timestampUpdate') {
attribute.type = 'timestamp';
}
acc[key] = attribute; return acc;
} }
acc[key] = attribute;
return acc; 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',
},
};
}