Files
payload/test/relationships/config.ts
Sasha a0a1e20193 fix(drizzle): polymorphic querying of different ID types (#8191)
This PR fixes querying by a relationship field that has custom IDs in
`relationTo` with different types.
Now, in this case, we do cast the ID value in the database.

Example of the config / int test that reproduced the issue:

```ts
{
  slug: 'posts-a',
  fields: [],
},
{
  slug: 'posts-b',
  fields: [],
},
{
  slug: 'posts-custom-id',
  fields: [{ name: 'id', type: 'text' }],
},
{
  slug: 'roots',
  fields: [
    {
      name: 'rel',
      relationTo: ['posts-a', 'posts-b', 'posts-custom-id'],
      type: 'relationship',
    },
  ],
},
```

```ts
const postA = await payload.create({ collection: 'posts-a', data: {} })
const postB = await payload.create({ collection: 'posts-b', data: {} })
const postC = await payload.create({
  collection: 'posts-custom-id',
  data: { id: crypto.randomUUID() },
})

const root_1 = await payload.create({
  collection: 'roots',
  data: {
    rel: {
      value: postC.id,
      relationTo: 'posts-custom-id',
    },
  },
})

const res_1 = await payload.find({
  collection: 'roots',
  where: {
    'rel.value': { equals: postC.id },
  },
})

// COALESCE types integer and character varying cannot be matched

expect(res_1.totalDocs).toBe(1)
```

<!--

For external contributors, please include:

- A summary of the pull request and any related issues it fixes.
- Reasoning for the changes made or any additional context that may be
useful.

Ensure you have read and understand the
[CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md)
document in this repository.

 -->
2024-09-16 10:39:55 -04:00

441 lines
8.7 KiB
TypeScript

import { fileURLToPath } from 'node:url'
import path from 'path'
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
import type { CollectionConfig } from 'payload'
import { buildConfigWithDefaults } from '../buildConfigWithDefaults.js'
import { devUser } from '../credentials.js'
import {
chainedRelSlug,
customIdNumberSlug,
customIdSlug,
defaultAccessRelSlug,
polymorphicRelationshipsSlug,
relationSlug,
slug,
slugWithLocalizedRel,
treeSlug,
} from './shared.js'
const openAccess = {
create: () => true,
read: () => true,
update: () => true,
delete: () => true,
}
const defaultAccess = ({ req: { user } }) => Boolean(user)
const collectionWithName = (collectionSlug: string): CollectionConfig => {
return {
slug: collectionSlug,
access: openAccess,
admin: {
useAsTitle: 'name',
},
fields: [
{
name: 'name',
type: 'text',
},
{
name: 'disableRelation', // used filteredRelation
type: 'checkbox',
required: true,
admin: {
position: 'sidebar',
},
},
],
}
}
export default buildConfigWithDefaults({
admin: {
importMap: {
baseDir: path.resolve(dirname),
},
},
localization: {
locales: ['en', 'de'],
defaultLocale: 'en',
},
collections: [
{
slug,
access: openAccess,
fields: [
{
name: 'title',
type: 'text',
},
{
name: 'description',
type: 'text',
},
{
name: 'number',
type: 'number',
},
// Relationship
{
name: 'relationField',
type: 'relationship',
relationTo: relationSlug,
},
// Relationship w/ default access
{
name: 'defaultAccessRelation',
type: 'relationship',
relationTo: defaultAccessRelSlug,
},
{
name: 'chainedRelation',
type: 'relationship',
relationTo: chainedRelSlug,
},
{
name: 'maxDepthRelation',
maxDepth: 0,
type: 'relationship',
relationTo: relationSlug,
},
{
name: 'customIdRelation',
type: 'relationship',
relationTo: customIdSlug,
},
{
name: 'customIdNumberRelation',
type: 'relationship',
relationTo: customIdNumberSlug,
},
{
name: 'filteredRelation',
type: 'relationship',
relationTo: relationSlug,
filterOptions: {
disableRelation: {
not_equals: true,
},
},
},
],
},
{
slug: slugWithLocalizedRel,
access: openAccess,
fields: [
{
name: 'title',
type: 'text',
},
// Relationship
{
name: 'relationField',
type: 'relationship',
relationTo: relationSlug,
localized: true,
},
],
},
collectionWithName(relationSlug),
{
...collectionWithName(defaultAccessRelSlug),
access: {
create: defaultAccess,
read: defaultAccess,
update: defaultAccess,
delete: defaultAccess,
},
},
{
slug: chainedRelSlug,
access: openAccess,
fields: [
{
name: 'name',
type: 'text',
},
{
name: 'relation',
type: 'relationship',
relationTo: chainedRelSlug,
},
],
},
{
slug: customIdSlug,
fields: [
{
name: 'id',
type: 'text',
},
{
name: 'name',
type: 'text',
},
],
},
{
slug: customIdNumberSlug,
fields: [
{
name: 'id',
type: 'number',
},
{
name: 'name',
type: 'text',
},
],
},
{
slug: 'screenings',
fields: [
{
name: 'name',
type: 'text',
},
{
name: 'movie',
type: 'relationship',
relationTo: 'movies',
},
],
},
{
slug: 'movies',
fields: [
{
name: 'name',
type: 'text',
},
{
name: 'director',
type: 'relationship',
relationTo: 'directors',
},
],
},
{
slug: 'directors',
fields: [
{
name: 'name',
type: 'text',
},
{
name: 'movies',
type: 'relationship',
relationTo: 'movies',
hasMany: true,
},
],
},
{
slug: 'movieReviews',
fields: [
{
name: 'movieReviewer',
relationTo: 'users',
required: true,
type: 'relationship',
},
{
name: 'likes',
hasMany: true,
relationTo: 'users',
type: 'relationship',
},
{
name: 'visibility',
options: [
{
label: 'followers',
value: 'followers',
},
{
label: 'public',
value: 'public',
},
],
required: true,
type: 'radio',
},
],
},
{
slug: polymorphicRelationshipsSlug,
fields: [
{
type: 'relationship',
name: 'polymorphic',
relationTo: ['movies'],
},
],
},
{
slug: treeSlug,
fields: [
{
name: 'text',
type: 'text',
},
{
name: 'parent',
type: 'relationship',
relationTo: 'tree',
},
],
},
{
slug: 'pages',
fields: [
{
type: 'array',
name: 'menu',
fields: [
{
name: 'label',
type: 'text',
},
],
},
],
},
{
slug: 'rels-to-pages',
fields: [
{
name: 'page',
type: 'relationship',
relationTo: 'pages',
},
],
},
{
slug: 'rels-to-pages-and-custom-text-ids',
fields: [
{
name: 'rel',
type: 'relationship',
relationTo: ['pages', 'custom-id', 'custom-id-number'],
},
],
},
],
onInit: async (payload) => {
await payload.create({
collection: 'users',
data: {
email: devUser.email,
password: devUser.password,
},
})
const rel1 = await payload.create({
collection: relationSlug,
data: {
name: 'name',
},
})
const filteredRelation = await payload.create({
collection: relationSlug,
data: {
name: 'filtered',
},
})
const defaultAccessRelation = await payload.create({
collection: defaultAccessRelSlug,
data: {
name: 'name',
},
})
const chained3 = await payload.create({
collection: chainedRelSlug,
data: {
name: 'chain3',
},
})
const chained2 = await payload.create({
collection: chainedRelSlug,
data: {
name: 'chain2',
relation: chained3.id,
},
})
const chained = await payload.create({
collection: chainedRelSlug,
data: {
name: 'chain1',
relation: chained2.id,
},
})
await payload.update({
collection: chainedRelSlug,
id: chained3.id,
data: {
name: 'chain3',
relation: chained.id,
},
})
const customIdRelation = await payload.create({
collection: customIdSlug,
data: {
id: 'custommmm',
name: 'custom-id',
},
})
const customIdNumberRelation = await payload.create({
collection: customIdNumberSlug,
data: {
id: 908234892340,
name: 'custom-id',
},
})
// Relationship
await payload.create({
collection: slug,
data: {
title: 'with relationship',
relationField: rel1.id,
defaultAccessRelation: defaultAccessRelation.id,
chainedRelation: chained.id,
maxDepthRelation: rel1.id,
customIdRelation: customIdRelation.id,
customIdNumberRelation: customIdNumberRelation.id,
filteredRelation: filteredRelation.id,
},
})
const root = await payload.create({
collection: 'tree',
data: {
text: 'root',
},
})
await payload.create({
collection: 'tree',
data: {
text: 'sub',
parent: root.id,
},
})
},
typescript: {
outputFile: path.resolve(dirname, 'payload-types.ts'),
},
})