fix(db-mongodb): joins with singular collection name (#8933)

### What?
Properly specifies `$lookup.from` when the collection name is singular.

### Why?
MongoDB can pluralize the collection name and so can be different for
singular ones.

### How?
Uses the collection name from the driver directly
`adapter.collections[slug].collection.name` instead of just `slug`.
This commit is contained in:
Sasha
2024-10-30 18:06:03 +02:00
committed by GitHub
parent 123125185c
commit f4041ce6e2
6 changed files with 62 additions and 3 deletions

View File

@@ -5,7 +5,7 @@ import { getFileByPath } from 'payload'
import { fileURLToPath } from 'url'
import type { NextRESTClient } from '../helpers/NextRESTClient.js'
import type { Category, Config, Post } from './payload-types.js'
import type { Category, Config, Post, Singular } from './payload-types.js'
import { devUser } from '../credentials.js'
import { idToString } from '../helpers/idToString.js'
@@ -642,6 +642,8 @@ describe('Joins Field', () => {
}
}
}`
expect(true).toBeTruthy()
const response = await restClient
.GRAPHQL_POST({ body: JSON.stringify({ query }) })
.then((res) => res.json())
@@ -666,6 +668,21 @@ describe('Joins Field', () => {
expect(allCategories.totalDocs).toBe(allCategoriesByIds.totalDocs)
})
it('should join with singular collection name', async () => {
const {
docs: [category],
} = await payload.find({ collection: 'categories', limit: 1, depth: 0 })
const singular = await payload.create({
collection: 'singular',
data: { category: category.id },
})
const categoryWithJoins = await payload.findByID({ collection: 'categories', id: category.id })
expect((categoryWithJoins.singulars.docs[0] as Singular).id).toBe(singular.id)
})
})
async function createPost(overrides?: Partial<Post>, locale?: Config['locale']) {