fix(db-postgres): in query with null (#12661)

Previously, this was possible in MongoDB but not in Postgres/SQLite
(having `null` in an `in` query)
```
const { docs } = await payload.find({
  collection: 'posts',
  where: { text: { in: ['text-1', 'text-3', null] } },
})
```
This PR fixes that behavior
This commit is contained in:
Sasha
2025-06-04 03:56:10 +03:00
committed by GitHub
parent cbc37d84bd
commit c08cdff498
2 changed files with 48 additions and 1 deletions

View File

@@ -367,7 +367,25 @@ export function parseParams({
break
}
constraints.push(adapter.operators[queryOperator](resolvedColumn, queryValue))
const orConditions: SQL<unknown>[] = []
let resolvedQueryValue = queryValue
if (
operator === 'in' &&
Array.isArray(queryValue) &&
queryValue.some((v) => v === null)
) {
orConditions.push(isNull(resolvedColumn))
resolvedQueryValue = queryValue.filter((v) => v !== null)
}
let constraint = adapter.operators[queryOperator](
resolvedColumn,
resolvedQueryValue,
)
if (orConditions.length) {
orConditions.push(constraint)
constraint = or(...orConditions)
}
constraints.push(constraint)
}
}
}

View File

@@ -2619,4 +2619,33 @@ describe('database', () => {
expect(res.testBlocks[0]?.text).toBe('text')
expect(res.testBlocksLocalized[0]?.text).toBe('text-localized')
})
it('should support in with null', async () => {
await payload.delete({ collection: 'posts', where: {} })
const post_1 = await payload.create({
collection: 'posts',
data: { title: 'a', text: 'text-1' },
})
const post_2 = await payload.create({
collection: 'posts',
data: { title: 'a', text: 'text-2' },
})
const post_3 = await payload.create({
collection: 'posts',
data: { title: 'a', text: 'text-3' },
})
const post_null = await payload.create({
collection: 'posts',
data: { title: 'a', text: null },
})
const { docs } = await payload.find({
collection: 'posts',
where: { text: { in: ['text-1', 'text-3', null] } },
})
expect(docs).toHaveLength(3)
expect(docs[0].id).toBe(post_null.id)
expect(docs[1].id).toBe(post_3.id)
expect(docs[2].id).toBe(post_1.id)
})
})