fix(drizzle): select hasMany nested to array + tab/group (#8737)

Fixes https://github.com/payloadcms/payload/issues/8732
This commit is contained in:
Sasha
2024-10-17 04:52:48 +03:00
committed by GitHub
parent 99b4359e89
commit 872b205acc
4 changed files with 173 additions and 2 deletions

View File

@@ -212,6 +212,9 @@ export const traverseFields = ({
if (typeof data[field.name] === 'object' && data[field.name] !== null) {
if (field.localized) {
Object.entries(data[field.name]).forEach(([localeKey, localeData]) => {
// preserve array ID if there is
localeData._uuid = data.id || data._uuid
traverseFields({
adapter,
arrays,
@@ -237,6 +240,10 @@ export const traverseFields = ({
})
})
} else {
// preserve array ID if there is
const groupData = data[field.name] as Record<string, unknown>
groupData._uuid = data.id || data._uuid
traverseFields({
adapter,
arrays,
@@ -244,7 +251,7 @@ export const traverseFields = ({
blocks,
blocksToDelete,
columnPrefix: `${columnName}_`,
data: data[field.name] as Record<string, unknown>,
data: groupData,
existingLocales,
fieldPrefix: `${fieldName}_`,
fields: field.fields,
@@ -275,6 +282,9 @@ export const traverseFields = ({
if (typeof data[tab.name] === 'object' && data[tab.name] !== null) {
if (tab.localized) {
Object.entries(data[tab.name]).forEach(([localeKey, localeData]) => {
// preserve array ID if there is
localeData._uuid = data.id || data._uuid
traverseFields({
adapter,
arrays,
@@ -300,6 +310,10 @@ export const traverseFields = ({
})
})
} else {
const tabData = data[tab.name] as Record<string, unknown>
// preserve array ID if there is
tabData._uuid = data.id || data._uuid
traverseFields({
adapter,
arrays,
@@ -307,7 +321,7 @@ export const traverseFields = ({
blocks,
blocksToDelete,
columnPrefix: `${columnPrefix || ''}${toSnakeCase(tab.name)}_`,
data: data[tab.name] as Record<string, unknown>,
data: tabData,
existingLocales,
fieldPrefix: `${fieldPrefix || ''}${tab.name}_`,
fields: tab.fields,

View File

@@ -82,6 +82,88 @@ const SelectFields: CollectionConfig = {
},
],
},
{
name: 'array',
type: 'array',
fields: [
{
name: 'selectHasMany',
hasMany: true,
type: 'select',
admin: {
isClearable: true,
isSortable: true,
},
options: [
{
label: 'Value One',
value: 'one',
},
{
label: 'Value Two',
value: 'two',
},
{
label: 'Value Three',
value: 'three',
},
{
label: 'Value Four',
value: 'four',
},
{
label: 'Value Five',
value: 'five',
},
{
label: 'Value Six',
value: 'six',
},
],
},
{
name: 'group',
type: 'group',
fields: [
{
name: 'selectHasMany',
hasMany: true,
type: 'select',
admin: {
isClearable: true,
isSortable: true,
},
options: [
{
label: 'Value One',
value: 'one',
},
{
label: 'Value Two',
value: 'two',
},
{
label: 'Value Three',
value: 'three',
},
{
label: 'Value Four',
value: 'four',
},
{
label: 'Value Five',
value: 'five',
},
{
label: 'Value Six',
value: 'six',
},
],
},
],
},
],
},
{
name: 'selectHasManyLocalized',
type: 'select',

View File

@@ -552,6 +552,54 @@ describe('Fields', () => {
expect(hitResult).toBeDefined()
expect(missResult).toBeFalsy()
})
it('should CRUD within array hasMany', async () => {
const doc = await payload.create({
collection: 'select-fields',
data: { array: [{ selectHasMany: ['one', 'two'] }] },
})
expect(doc.array[0].selectHasMany).toStrictEqual(['one', 'two'])
const upd = await payload.update({
collection: 'select-fields',
id: doc.id,
data: {
array: [
{
id: doc.array[0].id,
selectHasMany: ['six'],
},
],
},
})
expect(upd.array[0].selectHasMany).toStrictEqual(['six'])
})
it('should CRUD within array + group hasMany', async () => {
const doc = await payload.create({
collection: 'select-fields',
data: { array: [{ group: { selectHasMany: ['one', 'two'] } }] },
})
expect(doc.array[0].group.selectHasMany).toStrictEqual(['one', 'two'])
const upd = await payload.update({
collection: 'select-fields',
id: doc.id,
data: {
array: [
{
id: doc.array[0].id,
group: { selectHasMany: ['six'] },
},
],
},
})
expect(upd.array[0].group.selectHasMany).toStrictEqual(['six'])
})
})
describe('number', () => {

View File

@@ -60,6 +60,7 @@ export interface Config {
'uploads-multi': UploadsMulti;
'uploads-poly': UploadsPoly;
'uploads-multi-poly': UploadsMultiPoly;
'uploads-restricted': UploadsRestricted;
'ui-fields': UiField;
'payload-locked-documents': PayloadLockedDocument;
'payload-preferences': PayloadPreference;
@@ -1328,6 +1329,15 @@ export interface SelectField {
select?: ('one' | 'two' | 'three') | null;
selectReadOnly?: ('one' | 'two' | 'three') | null;
selectHasMany?: ('one' | 'two' | 'three' | 'four' | 'five' | 'six')[] | null;
array?:
| {
selectHasMany?: ('one' | 'two' | 'three' | 'four' | 'five' | 'six')[] | null;
group?: {
selectHasMany?: ('one' | 'two' | 'three' | 'four' | 'five' | 'six')[] | null;
};
id?: string | null;
}[]
| null;
selectHasManyLocalized?: ('one' | 'two')[] | null;
selectI18n?: ('one' | 'two' | 'three') | null;
simple?: ('One' | 'Two' | 'Three') | null;
@@ -1619,6 +1629,19 @@ export interface UploadsMultiPoly {
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "uploads-restricted".
*/
export interface UploadsRestricted {
id: string;
text?: string | null;
uploadWithoutRestriction?: (string | null) | Upload;
uploadWithAllowCreateFalse?: (string | null) | Upload;
uploadMultipleWithAllowCreateFalse?: (string | Upload)[] | null;
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "ui-fields".
@@ -1764,6 +1787,10 @@ export interface PayloadLockedDocument {
relationTo: 'uploads-multi-poly';
value: string | UploadsMultiPoly;
} | null)
| ({
relationTo: 'uploads-restricted';
value: string | UploadsRestricted;
} | null)
| ({
relationTo: 'ui-fields';
value: string | UiField;