Skip to content

Commit 91ed882

Browse files
authored
perf: remove deepCopying in sanitizeJoinQuery, optimize flattenWhereToOperators (#10663)
1 parent 9f5ffed commit 91ed882

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
import type { Where, WhereField } from '../types/index.js'
22

3-
// Take a where query and flatten it to all top-level operators
4-
const flattenWhereToOperators = (query: Where): WhereField[] =>
5-
Object.entries(query).reduce((flattenedConstraints, [key, val]) => {
6-
if ((key === 'and' || key === 'or') && Array.isArray(val)) {
7-
return [
8-
...flattenedConstraints,
9-
...val.reduce((subVals, subVal) => {
10-
return [...subVals, ...flattenWhereToOperators(subVal)]
11-
}, []),
12-
]
13-
}
3+
/**
4+
* Take a where query and flatten it to all top-level operators
5+
*/
6+
export function flattenWhereToOperators(query: Where): WhereField[] {
7+
const result: WhereField[] = []
148

15-
return [...flattenedConstraints, val]
16-
}, [])
9+
for (const [key, value] of Object.entries(query)) {
10+
if ((key === 'and' || key === 'or') && Array.isArray(value)) {
11+
for (const subQuery of value) {
12+
const flattenedSub = flattenWhereToOperators(subQuery)
13+
result.push(...flattenedSub)
14+
}
15+
} else {
16+
result.push(value as WhereField)
17+
}
18+
}
1719

18-
export default flattenWhereToOperators
20+
return result
21+
}

packages/payload/src/database/queryValidation/validateQueryPaths.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,21 @@ type Args = {
2626
}
2727
)
2828

29-
const flattenWhere = (query: Where): WhereField[] =>
30-
Object.entries(query).reduce((flattenedConstraints, [key, val]) => {
29+
const flattenWhere = (query: Where): WhereField[] => {
30+
const flattenedConstraints: WhereField[] = []
31+
32+
for (const [key, val] of Object.entries(query)) {
3133
if ((key === 'and' || key === 'or') && Array.isArray(val)) {
32-
const subWhereConstraints: Where[] = val.reduce((acc, subVal) => {
33-
const subWhere = flattenWhere(subVal)
34-
return [...acc, ...subWhere]
35-
}, [])
36-
return [...flattenedConstraints, ...subWhereConstraints]
34+
for (const subVal of val) {
35+
flattenedConstraints.push(...flattenWhere(subVal))
36+
}
37+
} else {
38+
flattenedConstraints.push({ [key]: val })
3739
}
40+
}
3841

39-
return [...flattenedConstraints, { [key]: val }]
40-
}, [])
42+
return flattenedConstraints
43+
}
4144

4245
export async function validateQueryPaths({
4346
collectionConfig,

packages/payload/src/database/sanitizeJoinQuery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const sanitizeJoinQuery = async ({
7474
overrideAccess,
7575
req,
7676
// incoming where input, but we shouldn't validate generated from the access control.
77-
where: deepCopyObjectSimple(joinQuery.where),
77+
where: joinQuery.where,
7878
}),
7979
)
8080

packages/payload/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ export type * from './config/types.js'
10381038
export { combineQueries } from './database/combineQueries.js'
10391039
export { createDatabaseAdapter } from './database/createDatabaseAdapter.js'
10401040
export { defaultBeginTransaction } from './database/defaultBeginTransaction.js'
1041-
export { default as flattenWhereToOperators } from './database/flattenWhereToOperators.js'
1041+
export { flattenWhereToOperators } from './database/flattenWhereToOperators.js'
10421042
export { getLocalizedPaths } from './database/getLocalizedPaths.js'
10431043
export { createMigration } from './database/migrations/createMigration.js'
10441044
export { getMigrations } from './database/migrations/getMigrations.js'

0 commit comments

Comments
 (0)