Skip to content

Commit 338c93a

Browse files
authored
fix(drizzle): array/relationship/select hasMany in localized field (#8355)
This PR addresses these issues with localized groups / tabs with Postgres / SQLite: - Array fields inside of localized groups. Fixes #8322 - Select fields with `hasMany: true` inside of localized groups. Related to 1, but still needed its own additional logic. - Relationship (non-polymorphic / non has-many) inside of localized groups. Previously, even just trying to define them in the config led to a crash. Fixes #8308 Ensures test coverage for localized groups.
1 parent 36ba6d4 commit 338c93a

File tree

6 files changed

+192
-3
lines changed

6 files changed

+192
-3
lines changed

packages/db-sqlite/src/schema/traverseFields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ export const traverseFields = ({
879879
// add relationship to table
880880
relationsToBuild.set(fieldName, {
881881
type: 'one',
882-
localized: adapter.payload.config.localization && field.localized,
882+
localized: adapter.payload.config.localization && (field.localized || forceLocalized),
883883
target: tableName,
884884
})
885885

packages/drizzle/src/postgres/schema/traverseFields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ export const traverseFields = ({
886886
// add relationship to table
887887
relationsToBuild.set(fieldName, {
888888
type: 'one',
889-
localized: adapter.payload.config.localization && field.localized,
889+
localized: adapter.payload.config.localization && (field.localized || forceLocalized),
890890
target: tableName,
891891
})
892892

packages/drizzle/src/transform/read/traverseFields.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,13 +527,23 @@ export const traverseFields = <T extends Record<string, unknown>>({
527527
return selectResult
528528
}, {})
529529
} else {
530-
result[field.name] = fieldData.map(({ value }) => value)
530+
let selectData = fieldData
531+
if (withinArrayOrBlockLocale) {
532+
selectData = selectData.filter(({ locale }) => locale === withinArrayOrBlockLocale)
533+
}
534+
result[field.name] = selectData.map(({ value }) => value)
531535
}
532536
}
533537
return result
534538
}
535539

536540
if (field.localized && Array.isArray(table._locales)) {
541+
if (!table._locales.length && adapter.payload.config.localization) {
542+
adapter.payload.config.localization.localeCodes.forEach((_locale) =>
543+
(table._locales as unknown[]).push({ _locale }),
544+
)
545+
}
546+
537547
table._locales.forEach((localeRow) => {
538548
valuesToTransform.push({
539549
ref: localizedFieldData,

test/fields/collections/Group/index.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,48 @@ const GroupFields: CollectionConfig = {
221221
},
222222
],
223223
},
224+
{
225+
name: 'localizedGroupArr',
226+
type: 'group',
227+
localized: true,
228+
fields: [
229+
{
230+
name: 'array',
231+
type: 'array',
232+
fields: [
233+
{
234+
type: 'text',
235+
name: 'text',
236+
},
237+
],
238+
},
239+
],
240+
},
241+
{
242+
name: 'localizedGroupSelect',
243+
type: 'group',
244+
localized: true,
245+
fields: [
246+
{
247+
type: 'select',
248+
hasMany: true,
249+
options: ['one', 'two'],
250+
name: 'select',
251+
},
252+
],
253+
},
254+
{
255+
name: 'localizedGroupRel',
256+
type: 'group',
257+
localized: true,
258+
fields: [
259+
{
260+
type: 'relationship',
261+
relationTo: 'email-fields',
262+
name: 'email',
263+
},
264+
],
265+
},
224266
],
225267
}
226268

test/fields/int.spec.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,129 @@ describe('Fields', () => {
13361336
expect(res.camelCaseGroup.array[0].text).toBe('text')
13371337
expect(res.camelCaseGroup.array[0].array[0].text).toBe('nested')
13381338
})
1339+
1340+
it('should insert/update/read localized group with array inside', async () => {
1341+
const doc = await payload.create({
1342+
collection: 'group-fields',
1343+
locale: 'en',
1344+
data: {
1345+
group: { text: 'req' },
1346+
localizedGroupArr: {
1347+
array: [{ text: 'text-en' }],
1348+
},
1349+
},
1350+
})
1351+
1352+
expect(doc.localizedGroupArr.array[0].text).toBe('text-en')
1353+
1354+
const esDoc = await payload.update({
1355+
collection: 'group-fields',
1356+
locale: 'es',
1357+
id: doc.id,
1358+
data: {
1359+
localizedGroupArr: {
1360+
array: [{ text: 'text-es' }],
1361+
},
1362+
},
1363+
})
1364+
1365+
expect(esDoc.localizedGroupArr.array[0].text).toBe('text-es')
1366+
1367+
const allDoc = await payload.findByID({
1368+
collection: 'group-fields',
1369+
id: doc.id,
1370+
locale: 'all',
1371+
})
1372+
1373+
expect(allDoc.localizedGroupArr.en.array[0].text).toBe('text-en')
1374+
expect(allDoc.localizedGroupArr.es.array[0].text).toBe('text-es')
1375+
})
1376+
1377+
it('should insert/update/read localized group with select hasMany inside', async () => {
1378+
const doc = await payload.create({
1379+
collection: 'group-fields',
1380+
locale: 'en',
1381+
data: {
1382+
group: { text: 'req' },
1383+
localizedGroupSelect: {
1384+
select: ['one', 'two'],
1385+
},
1386+
},
1387+
})
1388+
1389+
expect(doc.localizedGroupSelect.select).toStrictEqual(['one', 'two'])
1390+
1391+
const esDoc = await payload.update({
1392+
collection: 'group-fields',
1393+
locale: 'es',
1394+
id: doc.id,
1395+
data: {
1396+
localizedGroupSelect: {
1397+
select: ['one'],
1398+
},
1399+
},
1400+
})
1401+
1402+
expect(esDoc.localizedGroupSelect.select).toStrictEqual(['one'])
1403+
1404+
const allDoc = await payload.findByID({
1405+
collection: 'group-fields',
1406+
id: doc.id,
1407+
locale: 'all',
1408+
})
1409+
1410+
expect(allDoc.localizedGroupSelect.en.select).toStrictEqual(['one', 'two'])
1411+
expect(allDoc.localizedGroupSelect.es.select).toStrictEqual(['one'])
1412+
})
1413+
1414+
it('should insert/update/read localized group with relationship inside', async () => {
1415+
const rel_1 = await payload.create({
1416+
collection: 'email-fields',
1417+
data: { email: 'pro123@gmail.com' },
1418+
})
1419+
1420+
const rel_2 = await payload.create({
1421+
collection: 'email-fields',
1422+
data: { email: 'frank@gmail.com' },
1423+
})
1424+
1425+
const doc = await payload.create({
1426+
collection: 'group-fields',
1427+
depth: 0,
1428+
data: {
1429+
group: { text: 'requireddd' },
1430+
localizedGroupRel: {
1431+
email: rel_1.id,
1432+
},
1433+
},
1434+
})
1435+
1436+
expect(doc.localizedGroupRel.email).toBe(rel_1.id)
1437+
1438+
const upd = await payload.update({
1439+
collection: 'group-fields',
1440+
depth: 0,
1441+
id: doc.id,
1442+
locale: 'es',
1443+
data: {
1444+
localizedGroupRel: {
1445+
email: rel_2.id,
1446+
},
1447+
},
1448+
})
1449+
1450+
expect(upd.localizedGroupRel.email).toBe(rel_2.id)
1451+
1452+
const docAll = await payload.findByID({
1453+
collection: 'group-fields',
1454+
id: doc.id,
1455+
locale: 'all',
1456+
depth: 0,
1457+
})
1458+
1459+
expect(docAll.localizedGroupRel.en.email).toBe(rel_1.id)
1460+
expect(docAll.localizedGroupRel.es.email).toBe(rel_2.id)
1461+
})
13391462
})
13401463

13411464
describe('tabs', () => {

test/fields/payload-types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,20 @@ export interface GroupField {
957957
}[]
958958
| null;
959959
};
960+
localizedGroupArr?: {
961+
array?:
962+
| {
963+
text?: string | null;
964+
id?: string | null;
965+
}[]
966+
| null;
967+
};
968+
localizedGroupSelect?: {
969+
select?: ('one' | 'two')[] | null;
970+
};
971+
localizedGroupRel?: {
972+
email?: (string | null) | EmailField;
973+
};
960974
updatedAt: string;
961975
createdAt: string;
962976
}

0 commit comments

Comments
 (0)