Fixes https://github.com/payloadcms/payload/issues/8630 - Fixes `hasMany: true` and `localized: true` on the foreign field - Adds `limit` to the subquery instead of hardcoded `11`. - Adds the schema path `field.on` to the subquery, without this having 2 or more relationship fields to the same collection breaks joins - Properly checks if the field is `hasMany`
85 lines
1.8 KiB
TypeScript
85 lines
1.8 KiB
TypeScript
import type { CollectionConfig } from 'payload'
|
|
|
|
import { categoriesSlug, postsSlug } from '../shared.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,
|
|
on: 'category',
|
|
},
|
|
{
|
|
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',
|
|
},
|
|
],
|
|
},
|
|
],
|
|
}
|