This example application shows how you could inject a custom UUID field on certain content-types and automatically generate a random UUID during the create process for both the content and admin APIs.
For this part we need to do it in the register phase to ensure it's actually created in the database as well. You can see this on lines 13-22 of the index.js file.
strapi.contentTypes['api::test.test'].attributes['someUUID'] = {
type: 'string',
}
Once we have the field injected we can then use the beforeCreate lifecycle hook to generate the UUID. You can see this on lines 48-56 of the index.js file. This will programmatically add the model lifecycle without needed to hard code it for every content-type
const { randomUUID } = require('crypto');
strapi.db.lifecycles.subscribe({
models: ['api::test.test'],
beforeCreate(event) {
if (!event?.params?.data['someUUID']) {
event.params.data['someUUID'] = randomUUID();
}
}
})
Now that we have the field injected and the value automatically created we need to disable the editing of the field in the content-manager. These are saved into the core-store collection in the database. You can see this in action on lines 25-45 index.js file.
const view = await strapi.db.query('strapi::core-store').findOne({
where: {
key: `plugin_content_manager_configuration_content_types::api::test.test`,
},
});
let value = JSON.parse(view.value);
if (value?.metadatas['someUUID']) {
value.metadatas['someUUID'].edit.editable = false;
}
await strapi.db.query("strapi::core-store").update({
where: { id: view.id },
data: {
value: JSON.stringify(value) || value.toString(),
},
});