Skip to content

Commit 613d3b0

Browse files
authored
fix(drizzle): hasMany / poly relationships nested to localized fields / nested blocks to localized fields (#8456)
fixes #8455 and #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!
1 parent fb60344 commit 613d3b0

File tree

9 files changed

+677
-6
lines changed

9 files changed

+677
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ export const traverseFields = ({
711711
rootTableIDColType,
712712
rootTableName,
713713
versions,
714-
withinLocalizedArrayOrBlock,
714+
withinLocalizedArrayOrBlock: withinLocalizedArrayOrBlock || field.localized,
715715
})
716716

717717
if (groupHasLocalizedField) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -721,7 +721,7 @@ export const traverseFields = ({
721721
rootTableIDColType,
722722
rootTableName,
723723
versions,
724-
withinLocalizedArrayOrBlock,
724+
withinLocalizedArrayOrBlock: withinLocalizedArrayOrBlock || field.localized,
725725
})
726726

727727
if (groupHasLocalizedField) {

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

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,13 @@ export const traverseFields = <T extends Record<string, unknown>>({
237237

238238
if (field.type === 'blocks') {
239239
const blockFieldPath = `${sanitizedPath}${field.name}`
240+
const blocksByPath = blocks[blockFieldPath]
240241

241-
if (Array.isArray(blocks[blockFieldPath])) {
242+
if (Array.isArray(blocksByPath)) {
242243
if (field.localized) {
243244
result[field.name] = {}
244245

245-
blocks[blockFieldPath].forEach((row) => {
246+
blocksByPath.forEach((row) => {
246247
if (row._uuid) {
247248
row.id = row._uuid
248249
delete row._uuid
@@ -285,7 +286,23 @@ export const traverseFields = <T extends Record<string, unknown>>({
285286
})
286287
})
287288
} else {
288-
result[field.name] = blocks[blockFieldPath].reduce((acc, row, i) => {
289+
// Add locale-specific index to have a proper blockFieldPath for current locale
290+
// because blocks can be in the same array for different locales!
291+
if (withinArrayOrBlockLocale && config.localization) {
292+
for (const locale of config.localization.localeCodes) {
293+
let localeIndex = 0
294+
295+
for (let i = 0; i < blocksByPath.length; i++) {
296+
const row = blocksByPath[i]
297+
if (row._locale === locale) {
298+
row._index = localeIndex
299+
localeIndex++
300+
}
301+
}
302+
}
303+
}
304+
305+
result[field.name] = blocksByPath.reduce((acc, row, i) => {
289306
delete row._order
290307
if (row._uuid) {
291308
row.id = row._uuid
@@ -301,6 +318,10 @@ export const traverseFields = <T extends Record<string, unknown>>({
301318
if (row._locale) {
302319
delete row._locale
303320
}
321+
if (typeof row._index === 'number') {
322+
i = row._index
323+
delete row._index
324+
}
304325

305326
acc.push(
306327
traverseFields<T>({
@@ -350,6 +371,7 @@ export const traverseFields = <T extends Record<string, unknown>>({
350371
}
351372
} else {
352373
const relationPathMatch = relationships[`${sanitizedPath}${field.name}`]
374+
353375
if (!relationPathMatch) {
354376
if ('hasMany' in field && field.hasMany) {
355377
if (field.localized && config.localization && config.localization.locales) {

test/fields/collections/Group/index.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,44 @@ const GroupFields: CollectionConfig = {
263263
},
264264
],
265265
},
266+
{
267+
name: 'localizedGroupManyRel',
268+
type: 'group',
269+
localized: true,
270+
fields: [
271+
{
272+
type: 'relationship',
273+
relationTo: 'email-fields',
274+
name: 'email',
275+
hasMany: true,
276+
},
277+
],
278+
},
279+
{
280+
name: 'localizedGroupPolyRel',
281+
type: 'group',
282+
localized: true,
283+
fields: [
284+
{
285+
type: 'relationship',
286+
relationTo: ['email-fields'],
287+
name: 'email',
288+
},
289+
],
290+
},
291+
{
292+
name: 'localizedGroupPolyHasManyRel',
293+
type: 'group',
294+
localized: true,
295+
fields: [
296+
{
297+
type: 'relationship',
298+
relationTo: ['email-fields'],
299+
name: 'email',
300+
hasMany: true,
301+
},
302+
],
303+
},
266304
],
267305
}
268306

test/fields/int.spec.ts

Lines changed: 195 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ describe('Fields', () => {
6262
slug: 'users',
6363
credentials: devUser,
6464
})
65-
6665
user = await payload.login({
6766
collection: 'users',
6867
data: {
@@ -1459,6 +1458,201 @@ describe('Fields', () => {
14591458
expect(docAll.localizedGroupRel.en.email).toBe(rel_1.id)
14601459
expect(docAll.localizedGroupRel.es.email).toBe(rel_2.id)
14611460
})
1461+
1462+
it('should insert/update/read localized group with hasMany relationship inside', async () => {
1463+
const rel_1 = await payload.create({
1464+
collection: 'email-fields',
1465+
data: { email: 'pro123@gmail.com' },
1466+
})
1467+
1468+
const rel_2 = await payload.create({
1469+
collection: 'email-fields',
1470+
data: { email: 'frank@gmail.com' },
1471+
})
1472+
1473+
const doc = await payload.create({
1474+
collection: 'group-fields',
1475+
depth: 0,
1476+
data: {
1477+
group: { text: 'requireddd' },
1478+
localizedGroupManyRel: {
1479+
email: [rel_1.id],
1480+
},
1481+
},
1482+
})
1483+
1484+
expect(doc.localizedGroupManyRel.email).toStrictEqual([rel_1.id])
1485+
1486+
const upd = await payload.update({
1487+
collection: 'group-fields',
1488+
depth: 0,
1489+
id: doc.id,
1490+
locale: 'es',
1491+
data: {
1492+
localizedGroupManyRel: {
1493+
email: [rel_2.id],
1494+
},
1495+
},
1496+
})
1497+
1498+
expect(upd.localizedGroupManyRel.email).toStrictEqual([rel_2.id])
1499+
1500+
const docAll = await payload.findByID({
1501+
collection: 'group-fields',
1502+
id: doc.id,
1503+
locale: 'all',
1504+
depth: 0,
1505+
})
1506+
1507+
expect(docAll.localizedGroupManyRel.en.email).toStrictEqual([rel_1.id])
1508+
expect(docAll.localizedGroupManyRel.es.email).toStrictEqual([rel_2.id])
1509+
})
1510+
1511+
it('should insert/update/read localized group with poly relationship inside', async () => {
1512+
const rel_1 = await payload.create({
1513+
collection: 'email-fields',
1514+
data: { email: 'pro123@gmail.com' },
1515+
})
1516+
1517+
const rel_2 = await payload.create({
1518+
collection: 'email-fields',
1519+
data: { email: 'frank@gmail.com' },
1520+
})
1521+
1522+
const doc = await payload.create({
1523+
collection: 'group-fields',
1524+
depth: 0,
1525+
data: {
1526+
group: { text: 'requireddd' },
1527+
localizedGroupPolyRel: {
1528+
email: {
1529+
relationTo: 'email-fields',
1530+
value: rel_1.id,
1531+
},
1532+
},
1533+
},
1534+
})
1535+
1536+
expect(doc.localizedGroupPolyRel.email).toStrictEqual({
1537+
relationTo: 'email-fields',
1538+
value: rel_1.id,
1539+
})
1540+
1541+
const upd = await payload.update({
1542+
collection: 'group-fields',
1543+
depth: 0,
1544+
id: doc.id,
1545+
locale: 'es',
1546+
data: {
1547+
localizedGroupPolyRel: {
1548+
email: {
1549+
value: rel_2.id,
1550+
relationTo: 'email-fields',
1551+
},
1552+
},
1553+
},
1554+
})
1555+
1556+
expect(upd.localizedGroupPolyRel.email).toStrictEqual({
1557+
value: rel_2.id,
1558+
relationTo: 'email-fields',
1559+
})
1560+
1561+
const docAll = await payload.findByID({
1562+
collection: 'group-fields',
1563+
id: doc.id,
1564+
locale: 'all',
1565+
depth: 0,
1566+
})
1567+
1568+
expect(docAll.localizedGroupPolyRel.en.email).toStrictEqual({
1569+
value: rel_1.id,
1570+
relationTo: 'email-fields',
1571+
})
1572+
expect(docAll.localizedGroupPolyRel.es.email).toStrictEqual({
1573+
value: rel_2.id,
1574+
relationTo: 'email-fields',
1575+
})
1576+
})
1577+
1578+
it('should insert/update/read localized group with poly hasMany relationship inside', async () => {
1579+
const rel_1 = await payload.create({
1580+
collection: 'email-fields',
1581+
data: { email: 'pro123@gmail.com' },
1582+
})
1583+
1584+
const rel_2 = await payload.create({
1585+
collection: 'email-fields',
1586+
data: { email: 'frank@gmail.com' },
1587+
})
1588+
1589+
const doc = await payload.create({
1590+
collection: 'group-fields',
1591+
depth: 0,
1592+
data: {
1593+
group: { text: 'requireddd' },
1594+
localizedGroupPolyHasManyRel: {
1595+
email: [
1596+
{
1597+
relationTo: 'email-fields',
1598+
value: rel_1.id,
1599+
},
1600+
],
1601+
},
1602+
},
1603+
})
1604+
1605+
expect(doc.localizedGroupPolyHasManyRel.email).toStrictEqual([
1606+
{
1607+
relationTo: 'email-fields',
1608+
value: rel_1.id,
1609+
},
1610+
])
1611+
1612+
const upd = await payload.update({
1613+
collection: 'group-fields',
1614+
depth: 0,
1615+
id: doc.id,
1616+
locale: 'es',
1617+
data: {
1618+
localizedGroupPolyHasManyRel: {
1619+
email: [
1620+
{
1621+
value: rel_2.id,
1622+
relationTo: 'email-fields',
1623+
},
1624+
],
1625+
},
1626+
},
1627+
})
1628+
1629+
expect(upd.localizedGroupPolyHasManyRel.email).toStrictEqual([
1630+
{
1631+
value: rel_2.id,
1632+
relationTo: 'email-fields',
1633+
},
1634+
])
1635+
1636+
const docAll = await payload.findByID({
1637+
collection: 'group-fields',
1638+
id: doc.id,
1639+
locale: 'all',
1640+
depth: 0,
1641+
})
1642+
1643+
expect(docAll.localizedGroupPolyHasManyRel.en.email).toStrictEqual([
1644+
{
1645+
value: rel_1.id,
1646+
relationTo: 'email-fields',
1647+
},
1648+
])
1649+
expect(docAll.localizedGroupPolyHasManyRel.es.email).toStrictEqual([
1650+
{
1651+
value: rel_2.id,
1652+
relationTo: 'email-fields',
1653+
},
1654+
])
1655+
})
14621656
})
14631657

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

test/fields/payload-types.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,23 @@ export interface GroupField {
971971
localizedGroupRel?: {
972972
email?: (string | null) | EmailField;
973973
};
974+
localizedGroupManyRel?: {
975+
email?: (string | EmailField)[] | null;
976+
};
977+
localizedGroupPolyRel?: {
978+
email?: {
979+
relationTo: 'email-fields';
980+
value: string | EmailField;
981+
} | null;
982+
};
983+
localizedGroupPolyHasManyRel?: {
984+
email?:
985+
| {
986+
relationTo: 'email-fields';
987+
value: string | EmailField;
988+
}[]
989+
| null;
990+
};
974991
updatedAt: string;
975992
createdAt: string;
976993
}

0 commit comments

Comments
 (0)