fix(db-postgres): select hasMany inside arrays and blocks with versions (#10829)
Fixes https://github.com/payloadcms/payload/issues/10780 Previously, with enabled versions, nested select `hasMany: true` fields weren't working with SQL database adapters. This was due to wrongly passed `parent` to select rows data because we store arrays and blocks in versions a bit differently, using both, `id` and `_uuid` (which contains the normal Object ID) columns. And unlike with non versions `_uuid` column isn't actually applicable here as it's not unique, thus we need to save blocks/arrays first and then map their ObjectIDs to generated by the database IDs and use them for select fields `parent` data
This commit is contained in:
47
test/fields/collections/SelectVersions/index.ts
Normal file
47
test/fields/collections/SelectVersions/index.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { CollectionConfig } from 'payload'
|
||||
|
||||
import { selectVersionsFieldsSlug } from '../../slugs.js'
|
||||
|
||||
const SelectVersionsFields: CollectionConfig = {
|
||||
slug: selectVersionsFieldsSlug,
|
||||
versions: true,
|
||||
fields: [
|
||||
{
|
||||
type: 'select',
|
||||
hasMany: true,
|
||||
options: ['a', 'b', 'c'],
|
||||
name: 'hasMany',
|
||||
},
|
||||
{
|
||||
type: 'array',
|
||||
name: 'array',
|
||||
fields: [
|
||||
{
|
||||
type: 'select',
|
||||
hasMany: true,
|
||||
options: ['a', 'b', 'c'],
|
||||
name: 'hasManyArr',
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
type: 'blocks',
|
||||
name: 'blocks',
|
||||
blocks: [
|
||||
{
|
||||
slug: 'block',
|
||||
fields: [
|
||||
{
|
||||
type: 'select',
|
||||
hasMany: true,
|
||||
options: ['a', 'b', 'c'],
|
||||
name: 'hasManyBlocks',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
export default SelectVersionsFields
|
||||
@@ -33,6 +33,7 @@ import RelationshipFields from './collections/Relationship/index.js'
|
||||
import RichTextFields from './collections/RichText/index.js'
|
||||
import RowFields from './collections/Row/index.js'
|
||||
import SelectFields from './collections/Select/index.js'
|
||||
import SelectVersionsFields from './collections/SelectVersions/index.js'
|
||||
import TabsFields from './collections/Tabs/index.js'
|
||||
import { TabsFields2 } from './collections/Tabs2/index.js'
|
||||
import TextFields from './collections/Text/index.js'
|
||||
@@ -67,7 +68,7 @@ export const collectionSlugs: CollectionConfig[] = [
|
||||
],
|
||||
},
|
||||
LexicalInBlock,
|
||||
|
||||
SelectVersionsFields,
|
||||
ArrayFields,
|
||||
BlockFields,
|
||||
CheckboxFields,
|
||||
|
||||
@@ -678,6 +678,30 @@ describe('Fields', () => {
|
||||
|
||||
expect(upd.array[0].group.selectHasMany).toStrictEqual(['six'])
|
||||
})
|
||||
|
||||
it('should work with versions', async () => {
|
||||
const base = await payload.create({
|
||||
collection: 'select-versions-fields',
|
||||
data: { hasMany: ['a', 'b'] },
|
||||
})
|
||||
|
||||
expect(base.hasMany).toStrictEqual(['a', 'b'])
|
||||
|
||||
const array = await payload.create({
|
||||
collection: 'select-versions-fields',
|
||||
data: { array: [{ hasManyArr: ['a', 'b'] }] },
|
||||
draft: true,
|
||||
})
|
||||
|
||||
expect(array.array[0]?.hasManyArr).toStrictEqual(['a', 'b'])
|
||||
|
||||
const block = await payload.create({
|
||||
collection: 'select-versions-fields',
|
||||
data: { blocks: [{ blockType: 'block', hasManyBlocks: ['a', 'b'] }] },
|
||||
})
|
||||
|
||||
expect(block.blocks[0]?.hasManyBlocks).toStrictEqual(['a', 'b'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('number', () => {
|
||||
|
||||
@@ -34,6 +34,7 @@ export interface Config {
|
||||
lexicalObjectReferenceBug: LexicalObjectReferenceBug;
|
||||
users: User;
|
||||
LexicalInBlock: LexicalInBlock;
|
||||
'select-versions-fields': SelectVersionsField;
|
||||
'array-fields': ArrayField;
|
||||
'block-fields': BlockField;
|
||||
'checkbox-fields': CheckboxField;
|
||||
@@ -79,6 +80,7 @@ export interface Config {
|
||||
lexicalObjectReferenceBug: LexicalObjectReferenceBugSelect<false> | LexicalObjectReferenceBugSelect<true>;
|
||||
users: UsersSelect<false> | UsersSelect<true>;
|
||||
LexicalInBlock: LexicalInBlockSelect<false> | LexicalInBlockSelect<true>;
|
||||
'select-versions-fields': SelectVersionsFieldsSelect<false> | SelectVersionsFieldsSelect<true>;
|
||||
'array-fields': ArrayFieldsSelect<false> | ArrayFieldsSelect<true>;
|
||||
'block-fields': BlockFieldsSelect<false> | BlockFieldsSelect<true>;
|
||||
'checkbox-fields': CheckboxFieldsSelect<false> | CheckboxFieldsSelect<true>;
|
||||
@@ -452,6 +454,30 @@ export interface LexicalInBlock {
|
||||
updatedAt: string;
|
||||
createdAt: string;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "select-versions-fields".
|
||||
*/
|
||||
export interface SelectVersionsField {
|
||||
id: string;
|
||||
hasMany?: ('a' | 'b' | 'c')[] | null;
|
||||
array?:
|
||||
| {
|
||||
hasManyArr?: ('a' | 'b' | 'c')[] | null;
|
||||
id?: string | null;
|
||||
}[]
|
||||
| null;
|
||||
blocks?:
|
||||
| {
|
||||
hasManyArr?: ('a' | 'b' | 'c')[] | null;
|
||||
id?: string | null;
|
||||
blockName?: string | null;
|
||||
blockType: 'block';
|
||||
}[]
|
||||
| null;
|
||||
updatedAt: string;
|
||||
createdAt: string;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "array-fields".
|
||||
@@ -1804,6 +1830,10 @@ export interface PayloadLockedDocument {
|
||||
relationTo: 'LexicalInBlock';
|
||||
value: string | LexicalInBlock;
|
||||
} | null)
|
||||
| ({
|
||||
relationTo: 'select-versions-fields';
|
||||
value: string | SelectVersionsField;
|
||||
} | null)
|
||||
| ({
|
||||
relationTo: 'array-fields';
|
||||
value: string | ArrayField;
|
||||
@@ -2074,6 +2104,32 @@ export interface LexicalInBlockSelect<T extends boolean = true> {
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "select-versions-fields_select".
|
||||
*/
|
||||
export interface SelectVersionsFieldsSelect<T extends boolean = true> {
|
||||
hasMany?: T;
|
||||
array?:
|
||||
| T
|
||||
| {
|
||||
hasManyArr?: T;
|
||||
id?: T;
|
||||
};
|
||||
blocks?:
|
||||
| T
|
||||
| {
|
||||
block?:
|
||||
| T
|
||||
| {
|
||||
hasManyArr?: T;
|
||||
id?: T;
|
||||
blockName?: T;
|
||||
};
|
||||
};
|
||||
updatedAt?: T;
|
||||
createdAt?: T;
|
||||
}
|
||||
/**
|
||||
* This interface was referenced by `Config`'s JSON-Schema
|
||||
* via the `definition` "array-fields_select".
|
||||
|
||||
@@ -24,6 +24,7 @@ export const relationshipFieldsSlug = 'relationship-fields'
|
||||
export const richTextFieldsSlug = 'rich-text-fields'
|
||||
export const rowFieldsSlug = 'row-fields'
|
||||
export const selectFieldsSlug = 'select-fields'
|
||||
export const selectVersionsFieldsSlug = 'select-versions-fields'
|
||||
export const tabsFieldsSlug = 'tabs-fields'
|
||||
export const tabsFields2Slug = 'tabs-fields-2'
|
||||
export const textFieldsSlug = 'text-fields'
|
||||
|
||||
Reference in New Issue
Block a user