fix(db-postgres): joins with versions and hasMany relationship (#9370)

Fixes errors when having joins with versions +drafts on `hasMany: true`
relationships.
Removes `joinQuery` overhead if we don't need it for the current
operation. Right now, in all adapters we support joins only for `find`,
`findOne`, and `queryDrafts`.


Fixes https://github.com/payloadcms/payload/issues/9369
This commit is contained in:
Sasha
2024-11-21 17:42:05 +02:00
committed by GitHub
parent 3d0424bc77
commit d499de1e0f
11 changed files with 60 additions and 11 deletions

View File

@@ -13,6 +13,12 @@ export const CategoriesVersions: CollectionConfig = {
collection: versionsSlug,
on: 'categoryVersion',
},
{
name: 'relatedVersionsMany',
type: 'join',
collection: versionsSlug,
on: 'categoryVersions',
},
],
versions: {
drafts: true,

View File

@@ -15,6 +15,12 @@ export const Versions: CollectionConfig = {
relationTo: 'categories-versions',
type: 'relationship',
},
{
name: 'categoryVersions',
relationTo: 'categories-versions',
type: 'relationship',
hasMany: true,
},
],
versions: {
drafts: true,

View File

@@ -480,7 +480,20 @@ describe('Joins Field', () => {
expect(res.docs[0].relatedVersions.docs[0].id).toBe(version.id)
})
it('should populate joins when versions on both sides draft true payload.db.queryDrafts', async () => {
it('should populate joins with hasMany relationships when versions on both sides draft false', async () => {
const category = await payload.create({ collection: 'categories-versions', data: {} })
const version = await payload.create({
collection: 'versions',
data: { categoryVersions: [category.id] },
})
const res = await payload.find({ collection: 'categories-versions', draft: false })
expect(res.docs[0].relatedVersionsMany.docs[0].id).toBe(version.id)
})
it('should populate joins with hasMany relationships when versions on both sides draft true payload.db.queryDrafts', async () => {
const category = await payload.create({ collection: 'categories-versions', data: {} })
const version = await payload.create({
@@ -495,6 +508,22 @@ describe('Joins Field', () => {
expect(res.docs[0].relatedVersions.docs[0].id).toBe(version.id)
})
it('should populate joins when versions on both sides draft true payload.db.queryDrafts', async () => {
const category = await payload.create({ collection: 'categories-versions', data: {} })
const version = await payload.create({
collection: 'versions',
data: { categoryVersions: [category.id] },
})
const res = await payload.find({
collection: 'categories-versions',
draft: true,
})
expect(res.docs[0].relatedVersionsMany.docs[0].id).toBe(version.id)
})
})
describe('REST', () => {

View File

@@ -41,6 +41,7 @@ export interface Config {
};
'categories-versions': {
relatedVersions: 'versions';
relatedVersionsMany: 'versions';
};
'localized-categories': {
relatedPosts: 'localized-posts';
@@ -197,6 +198,7 @@ export interface Version {
id: string;
category?: (string | null) | Category;
categoryVersion?: (string | null) | CategoriesVersion;
categoryVersions?: (string | CategoriesVersion)[] | null;
updatedAt: string;
createdAt: string;
_status?: ('draft' | 'published') | null;
@@ -211,6 +213,10 @@ export interface CategoriesVersion {
docs?: (string | Version)[] | null;
hasNextPage?: boolean | null;
} | null;
relatedVersionsMany?: {
docs?: (string | Version)[] | null;
hasNextPage?: boolean | null;
} | null;
updatedAt: string;
createdAt: string;
_status?: ('draft' | 'published') | null;
@@ -442,6 +448,7 @@ export interface UploadsSelect<T extends boolean = true> {
export interface VersionsSelect<T extends boolean = true> {
category?: T;
categoryVersion?: T;
categoryVersions?: T;
updatedAt?: T;
createdAt?: T;
_status?: T;
@@ -452,6 +459,7 @@ export interface VersionsSelect<T extends boolean = true> {
*/
export interface CategoriesVersionsSelect<T extends boolean = true> {
relatedVersions?: T;
relatedVersionsMany?: T;
updatedAt?: T;
createdAt?: T;
_status?: T;