diff --git a/packages/db-postgres/src/transform/write/traverseFields.ts b/packages/db-postgres/src/transform/write/traverseFields.ts index bfe53d936c..421c39a83f 100644 --- a/packages/db-postgres/src/transform/write/traverseFields.ts +++ b/packages/db-postgres/src/transform/write/traverseFields.ts @@ -60,8 +60,22 @@ export const traverseFields = ({ if (field.localized) { if (typeof data[field.name] === 'object' && data[field.name] !== null) { - // loop over each locale - console.log(data[field.name]); + Object.entries(data[field.name]).forEach(([localeKey, localeData]) => { + if (Array.isArray(localeData)) { + const newRows = transformArray({ + arrayTableName, + blocks, + columnName, + data: localeData, + field, + locale: localeKey, + path, + relationships, + }); + + arrays[arrayTableName] = arrays[arrayTableName].concat(newRows); + } + }); } } else { const newRows = transformArray({ diff --git a/packages/db-postgres/src/upsertRow/deleteExistingArrayRows.ts b/packages/db-postgres/src/upsertRow/deleteExistingArrayRows.ts index 833fb105f6..bc9d2646dc 100644 --- a/packages/db-postgres/src/upsertRow/deleteExistingArrayRows.ts +++ b/packages/db-postgres/src/upsertRow/deleteExistingArrayRows.ts @@ -3,18 +3,13 @@ import { PostgresAdapter } from '../types'; type Args = { adapter: PostgresAdapter - locale?: string - localeColumnName?: string - parentColumnName?: string parentID: unknown - pathColumnName?: string - newRows: Record[] + tableName: string } export const deleteExistingArrayRows = async ({ adapter, - locale, parentID, tableName, }: Args): Promise => { @@ -24,14 +19,6 @@ export const deleteExistingArrayRows = async ({ eq(table._parentID, parentID), ]; - // If table has a _locale column, - // match on only the locale being updated - if (typeof table._locale !== 'undefined') { - whereConstraints.push( - eq(table._locale, locale), - ); - } - await adapter.db.delete(table) .where( and(...whereConstraints), diff --git a/packages/db-postgres/src/upsertRow/index.ts b/packages/db-postgres/src/upsertRow/index.ts index 291b43dec5..53013d3695 100644 --- a/packages/db-postgres/src/upsertRow/index.ts +++ b/packages/db-postgres/src/upsertRow/index.ts @@ -200,9 +200,7 @@ export const upsertRow = async ({ await Promise.all(Object.entries(rowToInsert.arrays).map(async ([arrayTableName, tableRows]) => { await deleteExistingArrayRows({ adapter, - locale, parentID: insertedRow.id, - newRows: tableRows.map(({ row }) => row), tableName: arrayTableName, }); })); diff --git a/test/postgres/int.spec.ts b/test/postgres/int.spec.ts index 3b14bc5cad..ea1f44ccf1 100644 --- a/test/postgres/int.spec.ts +++ b/test/postgres/int.spec.ts @@ -7,277 +7,336 @@ describe('Postgres', () => { await initPayloadTest({ __dirname, init: { local: false } }); }); - let post; - let page1; - let page2; - let person1; - let person2; + describe('complex docs', () => { + let post; + let page1; + let page2; + let person1; + let person2; - it('creates a complex doc', async () => { - page1 = await payload.create({ - collection: 'pages', - data: { - slug: 'first', - }, - }); + it('creates a complex doc', async () => { + page1 = await payload.create({ + collection: 'pages', + data: { + slug: 'first', + }, + }); - page2 = await payload.create({ - collection: 'pages', - data: { - slug: 'second', - }, - }); + page2 = await payload.create({ + collection: 'pages', + data: { + slug: 'second', + }, + }); - person1 = await payload.create({ - collection: 'people', - data: { - fullName: 'Dan Ribbens', - }, - }); + person1 = await payload.create({ + collection: 'people', + data: { + fullName: 'Dan Ribbens', + }, + }); - person2 = await payload.create({ - collection: 'people', - data: { - fullName: 'Elliot DeNolf', - }, - }); + person2 = await payload.create({ + collection: 'people', + data: { + fullName: 'Elliot DeNolf', + }, + }); - const postTitleEN = 'hello'; + const postTitleEN = 'hello'; - post = await payload.create({ - collection: 'posts', - data: { - title: postTitleEN, - number: 1337, - myGroup: { - subField: 'hello', - subFieldLocalized: 'hello in english', - subGroup: { - subSubField: 'sub hello', - subSubFieldLocalized: 'sub hello in english', + post = await payload.create({ + collection: 'posts', + data: { + title: postTitleEN, + number: 1337, + myGroup: { + subField: 'hello', + subFieldLocalized: 'hello in english', + subGroup: { + subSubField: 'sub hello', + subSubFieldLocalized: 'sub hello in english', + }, + groupArray: [ + { + groupArrayText: 'hello 1', + }, + { + groupArrayText: 'hello 2', + }, + ], }, - groupArray: [ - { - groupArrayText: 'hello 1', - }, - { - groupArrayText: 'hello 2', - }, - ], - }, - relationHasOne: page1.id, - relationHasOnePoly: { - relationTo: 'people', - value: person1.id, - }, - relationHasMany: [page1.id, page2.id], - relationHasManyPoly: [ - { + relationHasOne: page1.id, + relationHasOnePoly: { relationTo: 'people', value: person1.id, }, - { - relationTo: 'pages', - value: page2.id, - }, - ], - myArray: [ - { - subField: 'hello 1', - mySubArray: [ - { - subSubField: 'row 1 subrow 1', - }, - { - subSubField: 'row 1 subrow 2', - }, - ], - }, - { - subField: 'hello 2', - mySubArray: [ - { - subSubField: 'row 2 subrow 1', - }, - { - subSubField: 'row 2 subrow 2', - }, - ], - }, - ], - myBlocks: [ - { - blockType: 'block1', - nonLocalizedText: 'hello', - localizedText: 'hello in english', - }, - { - blockType: 'block2', - number: 123, - blockArray: [ - { - subBlockArray: 'row 1', - }, - { - subBlockArray: 'row 2', - }, - ], - }, - ], - }, - }); - - expect(post.title).toEqual(postTitleEN); - expect(post.myBlocks[0].localizedText).toStrictEqual('hello in english'); - }); - - it('adds locale to existing doc', async () => { - const titleES = 'hello es'; - const arrayTitle1 = 'hello 1 spanish'; - const arrayTitle2 = 'hello 2 spanish'; - const blockLocalizedText = 'my block in spanish'; - - const updatedPost = await payload.update({ - collection: 'posts', - id: post.id, - locale: 'es', - data: { - title: titleES, - number: 1000, - myArray: [ - { - id: post.myArray[0].id, - subField: arrayTitle1, - }, - { - id: post.myArray[1].id, - subField: arrayTitle2, - }, - ], - myBlocks: [ - { - id: post.myBlocks[0].id, - localizedText: blockLocalizedText, - }, - ], - }, - }); - - expect(updatedPost.title).toStrictEqual(titleES); - expect(updatedPost.number).toStrictEqual(1000); - expect(updatedPost.myArray[0].subField).toStrictEqual(arrayTitle1); - expect(updatedPost.myArray[1].subField).toStrictEqual(arrayTitle2); - expect(updatedPost.myBlocks[0].localizedText).toStrictEqual(blockLocalizedText); - }); - - it('updates original locale', async () => { - const updatedTitle = 'hello 3'; - - const updatedPost = await payload.update({ - collection: 'posts', - id: post.id, - data: { - title: 'hello 3', - number: 1338, - myGroup: { - subFieldLocalized: 'hello in english updated', - subGroup: { - subSubField: 'sub hello updated', - subSubFieldLocalized: 'sub hello in english updated', - }, - groupArray: [ + relationHasMany: [page1.id, page2.id], + relationHasManyPoly: [ { - groupArrayText: 'hello 1 updated', + relationTo: 'people', + value: person1.id, }, { - groupArrayText: 'hello 2 updated', + relationTo: 'pages', + value: page2.id, + }, + ], + myArray: [ + { + subField: 'hello 1', + mySubArray: [ + { + subSubField: 'row 1 subrow 1', + }, + { + subSubField: 'row 1 subrow 2', + }, + ], + }, + { + subField: 'hello 2', + mySubArray: [ + { + subSubField: 'row 2 subrow 1', + }, + { + subSubField: 'row 2 subrow 2', + }, + ], + }, + ], + myBlocks: [ + { + blockType: 'block1', + nonLocalizedText: 'hello', + localizedText: 'hello in english', + }, + { + blockType: 'block2', + number: 123, + blockArray: [ + { + subBlockArray: 'row 1', + }, + { + subBlockArray: 'row 2', + }, + ], }, ], }, - relationHasOne: page2.id, - relationHasOnePoly: { - relationTo: 'people', - value: person2.id, - }, - relationHasMany: [page2.id, page1.id], - relationHasManyPoly: [ - { - relationTo: 'pages', - value: page2.id, - }, - { - relationTo: 'people', - value: person1.id, - }, - ], - myArray: [ - { - id: post.myArray[0].id, - subField: 'hello 1 updated', - mySubArray: [ - { - subSubField: 'row 1 subrow 1 updated', - }, - { - subSubField: 'row 1 subrow 2 updated', - }, - ], - }, - { - id: post.myArray[1].id, - subField: 'hello 2 updated', - mySubArray: [ - { - subSubField: 'row 2 subrow 1 updated', - }, - { - subSubField: 'row 2 subrow 2 updated', - }, - ], - }, - ], - myBlocks: [ - { - id: post.myBlocks[0].id, - blockType: 'block1', - nonLocalizedText: 'hello updated', - localizedText: 'hello in english updated', - }, - { - id: post.myBlocks[1].id, - blockType: 'block2', - number: 1234, - blockArray: [ - { - subBlockArray: 'row 1 updated', - }, - { - subBlockArray: 'row 2 updated', - }, - ], - }, - ], - }, + }); + + expect(post.title).toEqual(postTitleEN); + expect(post.myBlocks[0].localizedText).toStrictEqual('hello in english'); }); - expect(updatedPost.title).toStrictEqual(updatedTitle); - expect(updatedPost.myArray[0].subField).toStrictEqual('hello 1 updated'); - expect(updatedPost.myArray[1].subField).toStrictEqual('hello 2 updated'); - expect(updatedPost.myBlocks[0].localizedText).toStrictEqual('hello in english updated'); + it('adds locale to existing doc', async () => { + const titleES = 'hello es'; + const arrayTitle1 = 'hello 1 spanish'; + const arrayTitle2 = 'hello 2 spanish'; + const blockLocalizedText = 'my block in spanish'; + + const updatedPost = await payload.update({ + collection: 'posts', + id: post.id, + locale: 'es', + data: { + title: titleES, + number: 1000, + myArray: [ + { + id: post.myArray[0].id, + subField: arrayTitle1, + }, + { + id: post.myArray[1].id, + subField: arrayTitle2, + }, + ], + myBlocks: [ + { + id: post.myBlocks[0].id, + localizedText: blockLocalizedText, + }, + ], + }, + }); + + expect(updatedPost.title).toStrictEqual(titleES); + expect(updatedPost.number).toStrictEqual(1000); + expect(updatedPost.myArray[0].subField).toStrictEqual(arrayTitle1); + expect(updatedPost.myArray[1].subField).toStrictEqual(arrayTitle2); + expect(updatedPost.myBlocks[0].localizedText).toStrictEqual(blockLocalizedText); + }); + + it('updates original locale', async () => { + const updatedTitle = 'hello 3'; + + const updatedPost = await payload.update({ + collection: 'posts', + id: post.id, + data: { + title: 'hello 3', + number: 1338, + myGroup: { + subFieldLocalized: 'hello in english updated', + subGroup: { + subSubField: 'sub hello updated', + subSubFieldLocalized: 'sub hello in english updated', + }, + groupArray: [ + { + groupArrayText: 'hello 1 updated', + }, + { + groupArrayText: 'hello 2 updated', + }, + ], + }, + relationHasOne: page2.id, + relationHasOnePoly: { + relationTo: 'people', + value: person2.id, + }, + relationHasMany: [page2.id, page1.id], + relationHasManyPoly: [ + { + relationTo: 'pages', + value: page2.id, + }, + { + relationTo: 'people', + value: person1.id, + }, + ], + myArray: [ + { + id: post.myArray[0].id, + subField: 'hello 1 updated', + mySubArray: [ + { + subSubField: 'row 1 subrow 1 updated', + }, + { + subSubField: 'row 1 subrow 2 updated', + }, + ], + }, + { + id: post.myArray[1].id, + subField: 'hello 2 updated', + mySubArray: [ + { + subSubField: 'row 2 subrow 1 updated', + }, + { + subSubField: 'row 2 subrow 2 updated', + }, + ], + }, + ], + myBlocks: [ + { + id: post.myBlocks[0].id, + blockType: 'block1', + nonLocalizedText: 'hello updated', + localizedText: 'hello in english updated', + }, + { + id: post.myBlocks[1].id, + blockType: 'block2', + number: 1234, + blockArray: [ + { + subBlockArray: 'row 1 updated', + }, + { + subBlockArray: 'row 2 updated', + }, + ], + }, + ], + }, + }); + + expect(updatedPost.title).toStrictEqual(updatedTitle); + expect(updatedPost.myArray[0].subField).toStrictEqual('hello 1 updated'); + expect(updatedPost.myArray[1].subField).toStrictEqual('hello 2 updated'); + expect(updatedPost.myBlocks[0].localizedText).toStrictEqual('hello in english updated'); + }); + + it('retrieves doc in all locales', async () => { + const postAllLocales = await payload.findByID({ + collection: 'posts', + id: post.id, + locale: 'all', + }); + + expect(postAllLocales.title.en).toStrictEqual('hello 3'); + expect(postAllLocales.title.es).toStrictEqual('hello es'); + expect(postAllLocales.number.en).toStrictEqual(1338); + expect(postAllLocales.number.es).toStrictEqual(1000); + expect(postAllLocales.myBlocks[0].localizedText.en).toStrictEqual('hello in english updated'); + expect(postAllLocales.myArray[0].subField.es).toStrictEqual('hello 1 spanish'); + }); }); - it('retrieves doc in all locales', async () => { - const postAllLocales = await payload.findByID({ - collection: 'posts', - id: post.id, - locale: 'all', + describe('localized arrays', () => { + let localizedArray; + + it('creates localized array', async () => { + localizedArray = await payload.create({ + collection: 'localized-arrays', + data: { + array: [ + { + text: 'hello in english', + }, + { + text: 'hello 2 in english', + }, + ], + }, + }); + + expect(localizedArray.array[0].text).toStrictEqual('hello in english'); + expect(localizedArray.array[1].text).toStrictEqual('hello 2 in english'); }); - expect(postAllLocales.title.en).toStrictEqual('hello 3'); - expect(postAllLocales.title.es).toStrictEqual('hello es'); - expect(postAllLocales.number.en).toStrictEqual(1338); - expect(postAllLocales.number.es).toStrictEqual(1000); - expect(postAllLocales.myBlocks[0].localizedText.en).toStrictEqual('hello in english updated'); - expect(postAllLocales.myArray[0].subField.es).toStrictEqual('hello 1 spanish'); + it('adds localized rows', async () => { + const updated = await payload.update({ + collection: 'localized-arrays', + id: localizedArray.id, + locale: 'es', + data: { + array: [ + { + text: 'hello in spanish', + }, + { + text: 'hello 2 in spanish', + }, + ], + }, + }); + + expect(updated.array[0].text).toStrictEqual('hello in spanish'); + expect(updated.array[1].text).toStrictEqual('hello 2 in spanish'); + }); + + it('retrieves array field in all locales', async () => { + const retrievedArray = await payload.findByID({ + collection: 'localized-arrays', + id: localizedArray.id, + locale: 'all', + }); + + expect(retrievedArray.array.en[0].text).toStrictEqual('hello in english'); + expect(retrievedArray.array.en[1].text).toStrictEqual('hello 2 in english'); + expect(retrievedArray.array.es[0].text).toStrictEqual('hello in spanish'); + expect(retrievedArray.array.es[1].text).toStrictEqual('hello 2 in spanish'); + }); }); }); diff --git a/test/postgres/seed/complexDocs.ts b/test/postgres/seed/complexDocs.ts deleted file mode 100644 index b21b092a7e..0000000000 --- a/test/postgres/seed/complexDocs.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { Payload } from '../../../src'; - -export const seedComplexDocs = async (payload: Payload) => { - // const findResult = await payload.find({ - // collection: 'pages', - // where: { slug: { equals: 'second' } }, - // }); - - // const findOneResult = await payload.findByID({ - // collection: 'pages', - // id: page1.id, - // }); - - - console.log(postAllLocales); -}; diff --git a/test/postgres/seed/localizedArrays.ts b/test/postgres/seed/localizedArrays.ts deleted file mode 100644 index d83f005365..0000000000 --- a/test/postgres/seed/localizedArrays.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { Payload } from '../../../src'; - -export const seedLocalizedArrays = async (payload: Payload) => { - const localizedArray = await payload.create({ - collection: 'localized-arrays', - data: { - array: [ - { - text: 'hello in english', - }, - { - text: 'hello 2 in english', - }, - ], - }, - }); - - await payload.update({ - collection: 'localized-arrays', - id: localizedArray.id, - locale: 'es', - data: { - array: [ - { - text: 'hello in spanny', - }, - { - text: 'hello 2 in spanny', - }, - ], - }, - }); - - const retrievedArray = await payload.findByID({ - collection: 'localized-arrays', - id: localizedArray.id, - locale: 'all', - }); - - console.log(retrievedArray); -};