mirror of
https://github.com/strapi/strapi.git
synced 2025-12-25 06:04:29 +00:00
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:
parent
90e96c2f49
commit
d6fc84a403
@ -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;
|
||||
});
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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 = {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user