Files
payloadcms/test/joins/collections/Categories.ts
Sasha f4041ce6e2 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`.
2024-10-30 12:06:03 -04:00

95 lines
2.0 KiB
TypeScript

import type { CollectionConfig } from 'payload'
import { categoriesSlug, postsSlug } from '../shared.js'
import { singularSlug } from './Singular.js'
export const Categories: CollectionConfig = {
slug: categoriesSlug,
admin: {
useAsTitle: 'name',
},
fields: [
{
name: 'name',
type: 'text',
},
// Alternative tabs usage
// {
// type: 'tabs',
// tabs: [
// {
// label: 'Unnamed tab',
// fields: [
// {
// name: 'relatedPosts',
// label: 'Related Posts',
// type: 'join',
// collection: postsSlug,
// on: 'category',
// },
// ],
// },
// {
// name: 'group',
// fields: [
// {
// name: 'relatedPosts',
// label: 'Related Posts (Group)',
// type: 'join',
// collection: postsSlug,
// on: 'group.category',
// },
// ],
// },
// ],
// },
{
name: 'relatedPosts',
label: 'Related Posts',
type: 'join',
collection: postsSlug,
defaultSort: '-title',
defaultLimit: 5,
on: 'category',
maxDepth: 1,
},
{
name: 'hasManyPosts',
type: 'join',
collection: postsSlug,
on: 'categories',
},
{
name: 'hasManyPostsLocalized',
type: 'join',
collection: postsSlug,
on: 'categoriesLocalized',
},
{
name: 'group',
type: 'group',
fields: [
{
name: 'relatedPosts',
label: 'Related Posts (Group)',
type: 'join',
collection: postsSlug,
on: 'group.category',
},
{
name: 'camelCasePosts',
type: 'join',
collection: postsSlug,
on: 'group.camelCaseCategory',
},
],
},
{
name: 'singulars',
type: 'join',
collection: singularSlug,
on: 'category',
},
],
}