fix(drizzle): hasMany / poly relationships nested to localized fields / nested blocks to localized fields (#8456)

fixes https://github.com/payloadcms/payload/issues/8455 and
https://github.com/payloadcms/payload/issues/8462

- Builds the `_locale` column for the `_rels` when it's inside of a
localized group / tab
- Properly builds `sanitizedPath` for blocks in the transform-read
function when it's inside of a localized field. This fixes with fields
inside that have its own table (like `_rels`, select `hasMany: true`
etc)

Adds _more_ tests!
This commit is contained in:
Sasha
2024-09-28 17:33:50 +03:00
committed by GitHub
parent fb603448d8
commit 613d3b090e
9 changed files with 677 additions and 6 deletions

View File

@@ -43,5 +43,44 @@ export const NestedFields: CollectionConfig = {
},
],
},
{
name: 'blocks',
type: 'blocks',
localized: true,
blocks: [
{
slug: 'block',
fields: [
{
name: 'nestedBlocks',
type: 'blocks',
blocks: [
{
slug: 'content',
fields: [
{
name: 'relation',
type: 'relationship',
relationTo: ['localized-posts'],
},
],
},
],
},
{
name: 'array',
type: 'array',
fields: [
{
name: 'relation',
type: 'relationship',
relationTo: ['localized-posts'],
},
],
},
],
},
],
},
],
}

View File

@@ -1975,6 +1975,260 @@ describe('Localization', () => {
expect(retrieved.array.en[0].text).toEqual(['hello', 'goodbye'])
expect(retrieved.array.es[0].text).toEqual(['hola', 'adios'])
})
it('should allow for relationship in new tables within blocks inside of localized blocks to be stored', async () => {
const randomDoc = (
await payload.find({
collection: 'localized-posts',
depth: 0,
})
).docs[0]
const randomDoc2 = (
await payload.find({
collection: 'localized-posts',
depth: 0,
})
).docs[1]
const docEn = await payload.create({
collection: 'nested-field-tables',
depth: 0,
data: {
blocks: [
{
blockType: 'block',
nestedBlocks: [
{
blockType: 'content',
relation: {
relationTo: 'localized-posts',
value: randomDoc.id,
},
},
],
},
{
blockType: 'block',
nestedBlocks: [
{
blockType: 'content',
relation: {
relationTo: 'localized-posts',
value: randomDoc.id,
},
},
],
},
{
blockType: 'block',
nestedBlocks: [
{
blockType: 'content',
relation: {
relationTo: 'localized-posts',
value: randomDoc.id,
},
},
],
},
],
},
})
expect(docEn.blocks[0].nestedBlocks[0].relation.value).toBe(randomDoc.id)
expect(docEn.blocks[1].nestedBlocks[0].relation.value).toBe(randomDoc.id)
expect(docEn.blocks[2].nestedBlocks[0].relation.value).toBe(randomDoc.id)
const docEs = await payload.update({
id: docEn.id,
depth: 0,
locale: 'es',
collection: 'nested-field-tables',
data: {
blocks: [
{
blockType: 'block',
nestedBlocks: [
{
blockType: 'content',
relation: {
relationTo: 'localized-posts',
value: randomDoc2.id,
},
},
],
},
{
blockType: 'block',
nestedBlocks: [
{
blockType: 'content',
relation: {
relationTo: 'localized-posts',
value: randomDoc2.id,
},
},
],
},
{
blockType: 'block',
nestedBlocks: [
{
blockType: 'content',
relation: {
relationTo: 'localized-posts',
value: randomDoc2.id,
},
},
],
},
],
},
})
expect(docEs.blocks[0].nestedBlocks[0].relation.value).toBe(randomDoc2.id)
expect(docEs.blocks[1].nestedBlocks[0].relation.value).toBe(randomDoc2.id)
expect(docEs.blocks[2].nestedBlocks[0].relation.value).toBe(randomDoc2.id)
const docAll = await payload.findByID({
collection: 'nested-field-tables',
id: docEn.id,
locale: 'all',
depth: 0,
})
expect(docAll.blocks.en[0].nestedBlocks[0].relation.value).toBe(randomDoc.id)
expect(docAll.blocks.en[1].nestedBlocks[0].relation.value).toBe(randomDoc.id)
expect(docAll.blocks.en[2].nestedBlocks[0].relation.value).toBe(randomDoc.id)
expect(docAll.blocks.es[0].nestedBlocks[0].relation.value).toBe(randomDoc2.id)
expect(docAll.blocks.es[1].nestedBlocks[0].relation.value).toBe(randomDoc2.id)
expect(docAll.blocks.es[2].nestedBlocks[0].relation.value).toBe(randomDoc2.id)
})
it('should allow for relationship in new tables within arrays inside of localized blocks to be stored', async () => {
const randomDoc = (
await payload.find({
collection: 'localized-posts',
depth: 0,
})
).docs[0]
const randomDoc2 = (
await payload.find({
collection: 'localized-posts',
depth: 0,
})
).docs[1]
const docEn = await payload.create({
collection: 'nested-field-tables',
depth: 0,
data: {
blocks: [
{
blockType: 'block',
array: [
{
relation: {
relationTo: 'localized-posts',
value: randomDoc.id,
},
},
],
},
{
blockType: 'block',
array: [
{
relation: {
relationTo: 'localized-posts',
value: randomDoc.id,
},
},
],
},
{
blockType: 'block',
array: [
{
relation: {
relationTo: 'localized-posts',
value: randomDoc.id,
},
},
],
},
],
},
})
expect(docEn.blocks[0].array[0].relation.value).toBe(randomDoc.id)
expect(docEn.blocks[1].array[0].relation.value).toBe(randomDoc.id)
expect(docEn.blocks[2].array[0].relation.value).toBe(randomDoc.id)
const docEs = await payload.update({
id: docEn.id,
depth: 0,
locale: 'es',
collection: 'nested-field-tables',
data: {
blocks: [
{
blockType: 'block',
array: [
{
relation: {
relationTo: 'localized-posts',
value: randomDoc2.id,
},
},
],
},
{
blockType: 'block',
array: [
{
relation: {
relationTo: 'localized-posts',
value: randomDoc2.id,
},
},
],
},
{
blockType: 'block',
array: [
{
relation: {
relationTo: 'localized-posts',
value: randomDoc2.id,
},
},
],
},
],
},
})
expect(docEs.blocks[0].array[0].relation.value).toBe(randomDoc2.id)
expect(docEs.blocks[1].array[0].relation.value).toBe(randomDoc2.id)
expect(docEs.blocks[2].array[0].relation.value).toBe(randomDoc2.id)
const docAll = await payload.findByID({
collection: 'nested-field-tables',
id: docEn.id,
locale: 'all',
depth: 0,
})
expect(docAll.blocks.en[0].array[0].relation.value).toBe(randomDoc.id)
expect(docAll.blocks.en[1].array[0].relation.value).toBe(randomDoc.id)
expect(docAll.blocks.en[2].array[0].relation.value).toBe(randomDoc.id)
expect(docAll.blocks.es[0].array[0].relation.value).toBe(randomDoc2.id)
expect(docAll.blocks.es[1].array[0].relation.value).toBe(randomDoc2.id)
expect(docAll.blocks.es[2].array[0].relation.value).toBe(randomDoc2.id)
})
})
describe('localized with unique', () => {

View File

@@ -27,6 +27,7 @@ export interface Config {
'localized-sort': LocalizedSort;
'blocks-same-name': BlocksSameName;
'localized-within-localized': LocalizedWithinLocalized;
'payload-locked-documents': PayloadLockedDocument;
'payload-preferences': PayloadPreference;
'payload-migrations': PayloadMigration;
};
@@ -166,6 +167,33 @@ export interface NestedFieldTable {
id?: string | null;
}[]
| null;
blocks?:
| {
nestedBlocks?:
| {
relation?: {
relationTo: 'localized-posts';
value: string | LocalizedPost;
} | null;
id?: string | null;
blockName?: string | null;
blockType: 'content';
}[]
| null;
array?:
| {
relation?: {
relationTo: 'localized-posts';
value: string | LocalizedPost;
} | null;
id?: string | null;
}[]
| null;
id?: string | null;
blockName?: string | null;
blockType: 'block';
}[]
| null;
updatedAt: string;
createdAt: string;
}
@@ -492,6 +520,85 @@ export interface LocalizedWithinLocalized {
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-locked-documents".
*/
export interface PayloadLockedDocument {
id: string;
document?:
| ({
relationTo: 'blocks-fields';
value: string | BlocksField;
} | null)
| ({
relationTo: 'nested-arrays';
value: string | NestedArray;
} | null)
| ({
relationTo: 'nested-field-tables';
value: string | NestedFieldTable;
} | null)
| ({
relationTo: 'users';
value: string | User;
} | null)
| ({
relationTo: 'localized-posts';
value: string | LocalizedPost;
} | null)
| ({
relationTo: 'array-fields';
value: string | ArrayField;
} | null)
| ({
relationTo: 'localized-required';
value: string | LocalizedRequired;
} | null)
| ({
relationTo: 'with-localized-relationship';
value: string | WithLocalizedRelationship;
} | null)
| ({
relationTo: 'relationship-localized';
value: string | RelationshipLocalized;
} | null)
| ({
relationTo: 'dummy';
value: string | Dummy;
} | null)
| ({
relationTo: 'nested';
value: string | Nested;
} | null)
| ({
relationTo: 'groups';
value: string | Group;
} | null)
| ({
relationTo: 'tabs';
value: string | Tab;
} | null)
| ({
relationTo: 'localized-sort';
value: string | LocalizedSort;
} | null)
| ({
relationTo: 'blocks-same-name';
value: string | BlocksSameName;
} | null)
| ({
relationTo: 'localized-within-localized';
value: string | LocalizedWithinLocalized;
} | null);
globalSlug?: string | null;
user: {
relationTo: 'users';
value: string | User;
};
updatedAt: string;
createdAt: string;
}
/**
* This interface was referenced by `Config`'s JSON-Schema
* via the `definition` "payload-preferences".