fix: loading relations without DP fails after migrating to v5 (#21501)

* fix: loading entries without DP

* fix: time format

* fix: unit test

* fix: set published at with db query
This commit is contained in:
Marc Roig 2024-10-09 11:22:11 +02:00 committed by GitHub
parent 90e96c2f49
commit d6fc84a403
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View File

@ -27,6 +27,9 @@ describe('Extract document ids from relation data', () => {
db: {
query: jest.fn((uid) => ({ findMany: findManyQueries[uid] })),
},
getModel: () => ({
options: { draftAndPublish: true },
}),
} as unknown as Core.Strapi;
});

View File

@ -1,5 +1,10 @@
import { Core, Data } from '@strapi/types';
import { async } from '@strapi/utils';
import { Core, Data, UID } from '@strapi/types';
import { async, contentTypes } from '@strapi/utils';
const hasDraftAndPublish = (uid: UID.CollectionType) => {
const model = strapi.getModel(uid);
return contentTypes.hasDraftAndPublish(model);
};
/**
* TODO: Find a better way to encode keys than this
@ -12,6 +17,11 @@ import { async } from '@strapi/utils';
* ^ "a:::1&&b:::2"
*/
const encodeKey = (obj: any) => {
// Ignore status field for models without draft and publish
if (!hasDraftAndPublish(obj.uid)) {
delete obj.status;
}
// Sort keys to always keep the same order when encoding
const keys = Object.keys(obj).sort();
return keys.map((key) => `${key}:::${obj[key]}`).join('&&');
@ -84,10 +94,13 @@ const createIdMap = ({ strapi }: { strapi: Core.Strapi }): IdMap => {
where: {
documentId: { $in: documentIds },
locale: locale || null,
publishedAt: status === 'draft' ? null : { $ne: null },
},
} as any;
if (hasDraftAndPublish(uid)) {
findParams.where.publishedAt = status === 'draft' ? null : { $ne: null };
}
const result = await strapi?.db?.query(uid).findMany(findParams);
// 3. Store result in loadedIds

View File

@ -13,6 +13,9 @@ const createPublishedAtColumn = async (db: Knex, tableName: string) => {
await db.schema.alterTable(tableName, (table) => {
table.string('published_at');
});
// Non DP content types should have their `published_at` column set to a date
await db(tableName).update({ published_at: new Date() });
};
export const createdPublishedAt: Migration = {