fix: empty inArray no longer crashes postgres

This commit is contained in:
James
2023-10-15 17:28:13 -04:00
parent 1481ef97b5
commit c4ac341d75
7 changed files with 52 additions and 13 deletions

View File

@@ -59,6 +59,7 @@
"fs-extra": "10.1.0",
"get-port": "5.1.1",
"glob": "8.1.0",
"graphql-request": "6.1.0",
"husky": "^8.0.3",
"isomorphic-fetch": "3.0.0",
"jest": "29.6.4",

View File

@@ -33,7 +33,7 @@ export const findMany = async function find({
const db = adapter.sessions[req.transactionID]?.db || adapter.drizzle
const table = adapter.tables[tableName]
let limit = limitArg
let limit = limitArg ?? 10
let totalDocs: number
let totalPages: number
let hasPrevPage: boolean
@@ -119,7 +119,11 @@ export const findMany = async function find({
findManyArgs.where = inArray(adapter.tables[tableName].id, Object.keys(orderedIDMap))
} else {
findManyArgs.limit = limitArg === 0 ? undefined : limitArg
findManyArgs.offset = skip || (page - 1) * limitArg
const offset = skip || (page - 1) * limitArg
if (!Number.isNaN(offset)) findManyArgs.offset = offset
if (where) {
findManyArgs.where = where
}

View File

@@ -144,13 +144,19 @@ export async function parseParams({
break
}
const { operator: queryOperator, value: queryValue } = sanitizeQueryValue({
const sanitizedQueryValue = sanitizeQueryValue({
field,
operator,
relationOrPath,
val,
})
if (sanitizedQueryValue === null) {
break
}
const { operator: queryOperator, value: queryValue } = sanitizedQueryValue
if (queryOperator === 'not_equals' && queryValue !== null) {
constraints.push(
or(
@@ -159,7 +165,10 @@ export async function parseParams({
ne<any>(rawColumn || table[columnName], queryValue),
),
)
} else if (
break
}
if (
(field.type === 'relationship' || field.type === 'upload') &&
Array.isArray(queryValue) &&
operator === 'not_in'
@@ -170,7 +179,10 @@ export async function parseParams({
IS
NULL`,
)
} else {
break
}
constraints.push(
operatorMap[queryOperator](rawColumn || table[columnName], queryValue),
)
@@ -181,7 +193,6 @@ export async function parseParams({
}
}
}
}
if (constraints.length > 0) {
if (result) {
result = and(result, ...constraints)

View File

@@ -42,7 +42,8 @@ export const sanitizeQueryValue = ({
if (val.toLowerCase() === 'false') formattedValue = false
}
if (['all', 'in', 'not_in'].includes(operator) && typeof formattedValue === 'string') {
if (['all', 'in', 'not_in'].includes(operator)) {
if (formattedValue === 'string') {
formattedValue = createArrayFromCommaDelineated(formattedValue)
if (field.type === 'number') {
@@ -50,6 +51,11 @@ export const sanitizeQueryValue = ({
}
}
if (!Array.isArray(formattedValue) || formattedValue.length === 0) {
return null
}
}
if (field.type === 'number' && typeof formattedValue === 'string') {
formattedValue = Number(val)
}

View File

@@ -187,7 +187,6 @@
"file-loader": "6.2.0",
"form-data": "3.0.1",
"get-port": "5.1.1",
"graphql-request": "6.1.0",
"mini-css-extract-plugin": "1.6.2",
"node-fetch": "2.6.12",
"nodemon": "3.0.1",

3
pnpm-lock.yaml generated
View File

@@ -96,6 +96,9 @@ importers:
glob:
specifier: 8.1.0
version: 8.1.0
graphql-request:
specifier: 6.1.0
version: 6.1.0(graphql@16.8.1)
husky:
specifier: ^8.0.3
version: 8.0.3

View File

@@ -796,4 +796,19 @@ describe('Fields', () => {
expect(uploadElement.value.media.filename).toStrictEqual('payload.png')
})
})
describe('relationships', () => {
it('should not crash if querying with empty in operator', async () => {
const query = await payload.find({
collection: 'relationship-fields',
where: {
'relationship.value': {
in: [],
},
},
})
expect(query.docs).toBeDefined()
})
})
})