fix(db-postgres): querying by localised relations postgres (#5686)
This commit is contained in:
@@ -359,6 +359,24 @@ export const getTableColumnFromPath = ({
|
|||||||
aliasRelationshipTableName,
|
aliasRelationshipTableName,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Join in the relationships table
|
||||||
|
if (locale && field.localized && adapter.payload.config.localization) {
|
||||||
|
joinAliases.push({
|
||||||
|
condition: and(
|
||||||
|
eq((aliasTable || adapter.tables[rootTableName]).id, aliasRelationshipTable.parent),
|
||||||
|
eq(aliasRelationshipTable.locale, locale),
|
||||||
|
like(aliasRelationshipTable.path, `${constraintPath}${field.name}`),
|
||||||
|
),
|
||||||
|
table: aliasRelationshipTable,
|
||||||
|
})
|
||||||
|
if (locale !== 'all') {
|
||||||
|
constraints.push({
|
||||||
|
columnName: 'locale',
|
||||||
|
table: aliasRelationshipTable,
|
||||||
|
value: locale,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// Join in the relationships table
|
// Join in the relationships table
|
||||||
joinAliases.push({
|
joinAliases.push({
|
||||||
condition: and(
|
condition: and(
|
||||||
@@ -367,6 +385,7 @@ export const getTableColumnFromPath = ({
|
|||||||
),
|
),
|
||||||
table: aliasRelationshipTable,
|
table: aliasRelationshipTable,
|
||||||
})
|
})
|
||||||
|
}
|
||||||
|
|
||||||
selectFields[`${relationTableName}.path`] = aliasRelationshipTable.path
|
selectFields[`${relationTableName}.path`] = aliasRelationshipTable.path
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
polymorphicRelationshipsSlug,
|
polymorphicRelationshipsSlug,
|
||||||
relationSlug,
|
relationSlug,
|
||||||
slug,
|
slug,
|
||||||
|
slugWithLocalizedRel,
|
||||||
treeSlug,
|
treeSlug,
|
||||||
} from './shared.js'
|
} from './shared.js'
|
||||||
|
|
||||||
@@ -47,6 +48,10 @@ const collectionWithName = (collectionSlug: string): CollectionConfig => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default buildConfigWithDefaults({
|
export default buildConfigWithDefaults({
|
||||||
|
localization: {
|
||||||
|
locales: ['en', 'de'],
|
||||||
|
defaultLocale: 'en',
|
||||||
|
},
|
||||||
collections: [
|
collections: [
|
||||||
{
|
{
|
||||||
slug,
|
slug,
|
||||||
@@ -109,6 +114,23 @@ export default buildConfigWithDefaults({
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
slug: slugWithLocalizedRel,
|
||||||
|
access: openAccess,
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'title',
|
||||||
|
type: 'text',
|
||||||
|
},
|
||||||
|
// Relationship
|
||||||
|
{
|
||||||
|
name: 'relationField',
|
||||||
|
type: 'relationship',
|
||||||
|
relationTo: relationSlug,
|
||||||
|
localized: true,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
collectionWithName(relationSlug),
|
collectionWithName(relationSlug),
|
||||||
{
|
{
|
||||||
...collectionWithName(defaultAccessRelSlug),
|
...collectionWithName(defaultAccessRelSlug),
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import type {
|
|||||||
CustomIdRelation,
|
CustomIdRelation,
|
||||||
Director,
|
Director,
|
||||||
Post,
|
Post,
|
||||||
|
PostsLocalized,
|
||||||
Relation,
|
Relation,
|
||||||
} from './payload-types.js'
|
} from './payload-types.js'
|
||||||
|
|
||||||
@@ -23,6 +24,7 @@ import {
|
|||||||
polymorphicRelationshipsSlug,
|
polymorphicRelationshipsSlug,
|
||||||
relationSlug,
|
relationSlug,
|
||||||
slug,
|
slug,
|
||||||
|
slugWithLocalizedRel,
|
||||||
treeSlug,
|
treeSlug,
|
||||||
usersSlug,
|
usersSlug,
|
||||||
} from './shared.js'
|
} from './shared.js'
|
||||||
@@ -517,6 +519,85 @@ describe('Relationships', () => {
|
|||||||
expect(doc?.relationField).toMatchObject({ id: relation.id, name: relation.name })
|
expect(doc?.relationField).toMatchObject({ id: relation.id, name: relation.name })
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('with localization', () => {
|
||||||
|
let relation1: Relation
|
||||||
|
let relation2: Relation
|
||||||
|
let localizedPost1: PostsLocalized
|
||||||
|
let localizedPost2: PostsLocalized
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
relation1 = await payload.create<Relation>({
|
||||||
|
collection: relationSlug,
|
||||||
|
data: {
|
||||||
|
name: 'english',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
relation2 = await payload.create<Relation>({
|
||||||
|
collection: relationSlug,
|
||||||
|
data: {
|
||||||
|
name: 'german',
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
localizedPost1 = await payload.create<'postsLocalized'>({
|
||||||
|
collection: slugWithLocalizedRel,
|
||||||
|
data: {
|
||||||
|
title: 'english',
|
||||||
|
relationField: relation1.id,
|
||||||
|
},
|
||||||
|
locale: 'en',
|
||||||
|
})
|
||||||
|
|
||||||
|
await payload.update({
|
||||||
|
id: localizedPost1.id,
|
||||||
|
collection: slugWithLocalizedRel,
|
||||||
|
locale: 'de',
|
||||||
|
data: {
|
||||||
|
relationField: relation2.id,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
localizedPost2 = await payload.create({
|
||||||
|
collection: slugWithLocalizedRel,
|
||||||
|
data: {
|
||||||
|
title: 'german',
|
||||||
|
relationField: relation2.id,
|
||||||
|
},
|
||||||
|
locale: 'de',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
it('should find two docs for german locale', async () => {
|
||||||
|
const { docs } = await payload.find<PostsLocalized>({
|
||||||
|
collection: slugWithLocalizedRel,
|
||||||
|
locale: 'de',
|
||||||
|
where: {
|
||||||
|
relationField: {
|
||||||
|
equals: relation2.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const mappedIds = docs.map((doc) => doc?.id)
|
||||||
|
expect(mappedIds).toContain(localizedPost1.id)
|
||||||
|
expect(mappedIds).toContain(localizedPost2.id)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("shouldn't find a relationship query outside of the specified locale", async () => {
|
||||||
|
const { docs } = await payload.find<PostsLocalized>({
|
||||||
|
collection: slugWithLocalizedRel,
|
||||||
|
locale: 'en',
|
||||||
|
where: {
|
||||||
|
relationField: {
|
||||||
|
equals: relation2.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
expect(docs.map((doc) => doc?.id)).not.toContain(localizedPost2.id)
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Nested Querying', () => {
|
describe('Nested Querying', () => {
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
export interface Config {
|
export interface Config {
|
||||||
collections: {
|
collections: {
|
||||||
posts: Post
|
posts: Post
|
||||||
|
postsLocalized: PostsLocalized
|
||||||
relation: Relation
|
relation: Relation
|
||||||
'strict-access': StrictAccess
|
'strict-access': StrictAccess
|
||||||
'chained-relation': ChainedRelation
|
'chained-relation': ChainedRelation
|
||||||
@@ -35,6 +36,13 @@ export interface Post {
|
|||||||
updatedAt: string
|
updatedAt: string
|
||||||
createdAt: string
|
createdAt: string
|
||||||
}
|
}
|
||||||
|
export interface PostsLocalized {
|
||||||
|
id: string
|
||||||
|
title?: string | null
|
||||||
|
relationField?: (string | null) | Relation
|
||||||
|
updatedAt: string
|
||||||
|
createdAt: string
|
||||||
|
}
|
||||||
export interface Relation {
|
export interface Relation {
|
||||||
id: string
|
id: string
|
||||||
name?: string
|
name?: string
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
export const usersSlug = 'users'
|
export const usersSlug = 'users'
|
||||||
export const slug = 'posts'
|
export const slug = 'posts'
|
||||||
|
export const slugWithLocalizedRel = 'postsLocalized'
|
||||||
export const relationSlug = 'relation'
|
export const relationSlug = 'relation'
|
||||||
export const defaultAccessRelSlug = 'strict-access'
|
export const defaultAccessRelSlug = 'strict-access'
|
||||||
export const chainedRelSlug = 'chained'
|
export const chainedRelSlug = 'chained'
|
||||||
|
|||||||
Reference in New Issue
Block a user