Skip to content

Commit

Permalink
⚡ Improve find query performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Dawidpol committed Nov 8, 2023
1 parent edc3555 commit 522309b
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/utils/convert-filter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import escape from 'escape-regexp'
import mongoose from 'mongoose'

const FIND_ONE_FIELDS = [
'_id',
'uuid',
] as const

/**
* Changes AdminJS's {@link Filter} to an object acceptible by a mongoose query.
*
Expand All @@ -11,6 +16,13 @@ export const convertFilter = (filter) => {
if (!filter) {
return {}
}

for (const field of FIND_ONE_FIELDS) {
if (field in filter && filter[field]) {
return { [field]: filter[field].value }
}
}

return filter.reduce((memo, filterProperty) => {
const { property, value } = filterProperty
switch (property.type()) {
Expand Down

0 comments on commit 522309b

Please sign in to comment.