fix(db-postgres, db-sqlite): hasMany text, number, poly relationship, blocks, arrays within localized fields (#7900)

## Description

In Postgres, localized blocks or arrays that contain other array / block
/ relationship fields were not properly storing locales in the database.

Now they are! Need to check a few things yet:

- Ensure test coverage is sufficient
- Test localized array, with non-localized array inside of it
- Test localized block with relationship field within it
- Ensure `_rels` table gets the `locale` column added if a single
non-localized relationship exists within a localized array / block

Fixes step 6 as identified in #7805
This commit is contained in:
James Mikrut
2024-08-28 13:43:12 -04:00
committed by GitHub
parent 3d9051ad34
commit 18b0806b5b
17 changed files with 990 additions and 96 deletions

View File

@@ -1395,6 +1395,328 @@ describe('Localization', () => {
expect(docEs.deep.blocks[0].title).toBe('hello es')
})
})
describe('nested blocks', () => {
let id
it('should allow creating nested blocks per locale', async () => {
const doc = await payload.create({
collection: 'blocks-fields',
data: {
content: [
{
blockType: 'blockInsideBlock',
content: [
{
blockType: 'textBlock',
text: 'hello',
},
],
},
],
},
})
id = doc.id
await payload.update({
collection: 'blocks-fields',
id,
locale: 'es',
data: {
content: [
{
blockType: 'blockInsideBlock',
content: [
{
blockType: 'textBlock',
text: 'hola',
},
],
},
],
},
})
const retrieved = await payload.findByID({
collection: 'blocks-fields',
id,
locale: 'all',
})
expect(retrieved.content.en[0].content).toHaveLength(1)
expect(retrieved.content.es[0].content).toHaveLength(1)
})
})
describe('nested arrays', () => {
it('should not duplicate block rows for blocks within localized array fields', 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 blocksWithinArrayEN = [
{
blockName: '1',
blockType: 'someBlock',
relationWithinBlock: randomDoc.id,
},
{
blockName: '2',
blockType: 'someBlock',
relationWithinBlock: randomDoc.id,
},
{
blockName: '3',
blockType: 'someBlock',
relationWithinBlock: randomDoc.id,
},
]
const blocksWithinArrayES = [
{
blockName: '1',
blockType: 'someBlock',
relationWithinBlock: randomDoc2.id,
},
{
blockName: '2',
blockType: 'someBlock',
relationWithinBlock: randomDoc2.id,
},
{
blockName: '3',
blockType: 'someBlock',
relationWithinBlock: randomDoc2.id,
},
]
const createdEnDoc = await payload.create({
collection: 'nested-arrays',
locale: 'en',
depth: 0,
data: {
arrayWithBlocks: [
{
blocksWithinArray: blocksWithinArrayEN as any,
},
],
},
})
const updatedEsDoc = await payload.update({
collection: 'nested-arrays',
id: createdEnDoc.id,
depth: 0,
locale: 'es',
data: {
arrayWithBlocks: [
{
blocksWithinArray: blocksWithinArrayES as any,
},
],
},
})
const esArrayBlocks = updatedEsDoc.arrayWithBlocks[0].blocksWithinArray
// recursively remove any id field within esArrayRow
const removeId = (obj) => {
if (obj instanceof Object) {
delete obj.id
Object.values(obj).forEach(removeId)
}
}
removeId(esArrayBlocks)
removeId(createdEnDoc.arrayWithBlocks[0].blocksWithinArray)
expect(esArrayBlocks).toEqual(blocksWithinArrayES)
expect(createdEnDoc.arrayWithBlocks[0].blocksWithinArray).toEqual(blocksWithinArrayEN)
// pull enDoc again and make sure the update of esDoc did not mess with the data of enDoc
const enDoc2 = await payload.findByID({
id: createdEnDoc.id,
collection: 'nested-arrays',
locale: 'en',
depth: 0,
})
removeId(enDoc2.arrayWithBlocks[0].blocksWithinArray)
expect(enDoc2.arrayWithBlocks[0].blocksWithinArray).toEqual(blocksWithinArrayEN)
})
it('should update localized relation within unLocalized array', async () => {
const randomTextDoc = (
await payload.find({
collection: 'localized-posts',
depth: 0,
})
).docs[0]
const randomTextDoc2 = (
await payload.find({
collection: 'localized-posts',
depth: 0,
})
).docs[1]
const createdEnDoc = await payload.create({
collection: 'nested-arrays',
locale: 'en',
depth: 0,
data: {
arrayWithLocalizedRelation: [
{
localizedRelation: randomTextDoc.id,
},
],
},
})
const updatedEsDoc = await payload.update({
collection: 'nested-arrays',
id: createdEnDoc.id,
depth: 0,
locale: 'es',
data: {
arrayWithLocalizedRelation: [
{
id: createdEnDoc.arrayWithLocalizedRelation[0].id,
localizedRelation: randomTextDoc2.id,
},
],
},
})
expect(updatedEsDoc.arrayWithLocalizedRelation).toHaveLength(1)
expect(updatedEsDoc.arrayWithLocalizedRelation[0].localizedRelation).toBe(randomTextDoc2.id)
expect(createdEnDoc.arrayWithLocalizedRelation).toHaveLength(1)
expect(createdEnDoc.arrayWithLocalizedRelation[0].localizedRelation).toBe(randomTextDoc.id)
// pull enDoc again and make sure the update of esDoc did not mess with the data of enDoc
const enDoc2 = await payload.findByID({
id: createdEnDoc.id,
collection: 'nested-arrays',
locale: 'en',
depth: 0,
})
expect(enDoc2.arrayWithLocalizedRelation).toHaveLength(1)
expect(enDoc2.arrayWithLocalizedRelation[0].localizedRelation).toBe(randomTextDoc.id)
})
})
describe('nested fields', () => {
it('should allow for fields which could contain new tables within localized arrays 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 newDoc = await payload.create({
collection: 'nested-field-tables',
data: {
array: [
{
relation: {
value: randomDoc.id,
relationTo: 'localized-posts',
},
hasManyRelation: [randomDoc.id, randomDoc2.id],
hasManyPolyRelation: [
{
relationTo: 'localized-posts',
value: randomDoc.id,
},
{
relationTo: 'localized-posts',
value: randomDoc2.id,
},
],
number: [1, 2],
text: ['hello', 'goodbye'],
select: ['one'],
},
],
},
})
await payload.update({
collection: 'nested-field-tables',
id: newDoc.id,
locale: 'es',
data: {
array: [
{
relation: {
value: randomDoc2.id,
relationTo: 'localized-posts',
},
hasManyRelation: [randomDoc2.id, randomDoc.id],
hasManyPolyRelation: [
{
relationTo: 'localized-posts',
value: randomDoc2.id,
},
{
relationTo: 'localized-posts',
value: randomDoc.id,
},
],
select: ['two', 'three'],
text: ['hola', 'adios'],
number: [3, 4],
},
],
},
})
const retrieved = await payload.findByID({
collection: 'nested-field-tables',
id: newDoc.id,
depth: 0,
locale: 'all',
})
expect(retrieved.array.en[0].relation.value).toStrictEqual(randomDoc.id)
expect(retrieved.array.es[0].relation.value).toStrictEqual(randomDoc2.id)
expect(retrieved.array.en[0].hasManyRelation).toEqual([randomDoc.id, randomDoc2.id])
expect(retrieved.array.es[0].hasManyRelation).toEqual([randomDoc2.id, randomDoc.id])
expect(retrieved.array.en[0].hasManyPolyRelation).toEqual([
{ value: randomDoc.id, relationTo: 'localized-posts' },
{ value: randomDoc2.id, relationTo: 'localized-posts' },
])
expect(retrieved.array.es[0].hasManyPolyRelation).toEqual([
{ value: randomDoc2.id, relationTo: 'localized-posts' },
{ value: randomDoc.id, relationTo: 'localized-posts' },
])
expect(retrieved.array.en[0].number).toEqual([1, 2])
expect(retrieved.array.es[0].number).toEqual([3, 4])
expect(retrieved.array.en[0].select).toEqual(['one'])
expect(retrieved.array.es[0].select).toEqual(['two', 'three'])
expect(retrieved.array.en[0].text).toEqual(['hello', 'goodbye'])
expect(retrieved.array.es[0].text).toEqual(['hola', 'adios'])
})
})
})
async function createLocalizedPost(data: {