fix: ability to query relationships not equal to ID (#6555)
This commit is contained in:
@@ -193,17 +193,19 @@ export async function buildSearchParam({
|
||||
|
||||
if (field.type === 'relationship' || field.type === 'upload') {
|
||||
let hasNumberIDRelation
|
||||
let multiIDCondition = '$or'
|
||||
if (operatorKey === '$ne') multiIDCondition = '$and'
|
||||
|
||||
const result = {
|
||||
value: {
|
||||
$or: [{ [path]: { [operatorKey]: formattedValue } }],
|
||||
[multiIDCondition]: [{ [path]: { [operatorKey]: formattedValue } }],
|
||||
},
|
||||
}
|
||||
|
||||
if (typeof formattedValue === 'string') {
|
||||
if (mongoose.Types.ObjectId.isValid(formattedValue)) {
|
||||
result.value.$or.push({
|
||||
[path]: { [operatorKey]: new ObjectId(formattedValue) },
|
||||
result.value[multiIDCondition].push({
|
||||
[path]: { [operatorKey]: ObjectId(formattedValue) },
|
||||
})
|
||||
} else {
|
||||
;(Array.isArray(field.relationTo) ? field.relationTo : [field.relationTo]).forEach(
|
||||
@@ -218,11 +220,13 @@ export async function buildSearchParam({
|
||||
)
|
||||
|
||||
if (hasNumberIDRelation)
|
||||
result.value.$or.push({ [path]: { [operatorKey]: parseFloat(formattedValue) } })
|
||||
result.value[multiIDCondition].push({
|
||||
[path]: { [operatorKey]: parseFloat(formattedValue) },
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if (result.value.$or.length > 1) {
|
||||
if (result.value[multiIDCondition].length > 1) {
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
@@ -618,6 +618,30 @@ describe('collections-rest', () => {
|
||||
})
|
||||
})
|
||||
|
||||
it('should query relationships by not_equals', async () => {
|
||||
const ogPost = await createPost({
|
||||
relationMultiRelationTo: { relationTo: relationSlug, value: relation.id },
|
||||
})
|
||||
await createPost()
|
||||
|
||||
const response = await restClient.GET(`/${slug}`, {
|
||||
query: {
|
||||
where: {
|
||||
and: [
|
||||
{
|
||||
'relationMultiRelationTo.value': { not_equals: relation.id },
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
})
|
||||
const result = await response.json()
|
||||
|
||||
expect(response.status).toEqual(200)
|
||||
const foundExcludedDoc = result.docs.some((doc) => ogPost.id === doc.id)
|
||||
expect(foundExcludedDoc).toBe(false)
|
||||
})
|
||||
|
||||
describe('relationTo multi hasMany', () => {
|
||||
it('nested by id', async () => {
|
||||
const post1 = await createPost({
|
||||
|
||||
Reference in New Issue
Block a user