schema and populate

This commit is contained in:
Julian Krauser 2024-11-01 14:14:55 +01:00
parent 1943224f33
commit 9c8037277f
4 changed files with 35 additions and 20 deletions

View file

@ -45,7 +45,7 @@
} }
}, },
"component": "shared.hero", "component": "shared.hero",
"required": true "required": false
}, },
"content": { "content": {
"pluginOptions": { "pluginOptions": {

View file

@ -14,7 +14,7 @@
"name": "Apache 2.0", "name": "Apache 2.0",
"url": "https://www.apache.org/licenses/LICENSE-2.0.html" "url": "https://www.apache.org/licenses/LICENSE-2.0.html"
}, },
"x-generation-date": "2024-11-01T10:19:23.797Z" "x-generation-date": "2024-11-01T12:34:04.379Z"
}, },
"x-strapi-config": { "x-strapi-config": {
"plugins": [ "plugins": [
@ -9083,8 +9083,7 @@
"data": { "data": {
"required": [ "required": [
"identifier", "identifier",
"slug", "slug"
"Hero"
], ],
"type": "object", "type": "object",
"properties": { "properties": {
@ -9197,8 +9196,7 @@
"type": "object", "type": "object",
"required": [ "required": [
"identifier", "identifier",
"slug", "slug"
"Hero"
], ],
"properties": { "properties": {
"id": { "id": {

View file

@ -1,26 +1,41 @@
/** /**
* `deepPopulate` middleware *deepPopulate middleware
*/ */
import type { Core } from '@strapi/strapi'; import type { Core } from '@strapi/strapi';
import { UID } from '@strapi/types'; import { UID } from '@strapi/types';
import { contentTypes } from '@strapi/utils'; import { contentTypes } from '@strapi/utils';
import pluralize from 'pluralize'; import pluralize from 'pluralize';
interface Options { interface Options {
/** /**
* Fields to select when populating relations * Fields to select when populating relations
*/ */
relationalFields?: string[]; relationalFields?: string[];
} }
const { CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE } = contentTypes.constants; const { CREATED_BY_ATTRIBUTE, UPDATED_BY_ATTRIBUTE } = contentTypes.constants;
const extractPathSegment = (url: string) => url.match(/\/([^/?]+)(?:\?|$)/)?.[1] || ''; const extractPathSegment = (url: string) => url.match(/\/([^/?]+)(?:\?|$)/)?.[1] || '';
const getModelUID = (path: string): UID.Schema => {
// Special handling for users-permissions routes
if (path === 'users' || path === 'me') {
return 'plugin::users-permissions.user';
}
// Logic for regular content types
const singular = pluralize.singular(path);
return `api::${singular}.${singular}` as UID.Schema;
};
const getDeepPopulate = (uid: UID.Schema, opts: Options = {}) => { const getDeepPopulate = (uid: UID.Schema, opts: Options = {}) => {
const model = strapi.getModel(uid); const model = strapi.getModel(uid);
// Add safety check
if (!model || !model.attributes) {
console.warn(`Model not found for UID: ${uid}`);
return {};
}
const attributes = Object.entries(model.attributes); const attributes = Object.entries(model.attributes);
return attributes.reduce((acc: any, [attributeName, attribute]) => { return attributes.reduce((acc: any, [attributeName, attribute]) => {
@ -84,14 +99,17 @@ export default (config, { strapi }: { strapi: Core.Strapi }) => {
if (ctx.request.url.startsWith('/api/') && ctx.request.method === 'GET' && !ctx.query.populate) { if (ctx.request.url.startsWith('/api/') && ctx.request.method === 'GET' && !ctx.query.populate) {
strapi.log.info('Using custom Dynamic-Zone population Middleware...'); strapi.log.info('Using custom Dynamic-Zone population Middleware...');
const contentType = extractPathSegment(ctx.request.url); const pathSegment = extractPathSegment(ctx.request.url);
const singular = pluralize.singular(contentType) const uid = getModelUID(pathSegment);
const uid = `api::${singular}.${singular}`;
// @ts-ignores // @ts-ignores
try {
ctx.query.populate = getDeepPopulate(uid); ctx.query.populate = getDeepPopulate(uid);
} catch (error) {
console.error(`Error in deepPopulate middleware for path ${pathSegment}:`, error);
// Continue without population if there's an error
}
} }
await next(); await next();
}; };
}; };

View file

@ -690,7 +690,6 @@ export interface ApiPagePage extends Struct.CollectionTypeSchema {
createdBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> & createdBy: Schema.Attribute.Relation<'oneToOne', 'admin::user'> &
Schema.Attribute.Private; Schema.Attribute.Private;
Hero: Schema.Attribute.Component<'shared.hero', false> & Hero: Schema.Attribute.Component<'shared.hero', false> &
Schema.Attribute.Required &
Schema.Attribute.SetPluginOptions<{ Schema.Attribute.SetPluginOptions<{
i18n: { i18n: {
localized: true; localized: true;