From 522309bc0c71a9e9bb1886b997b230c94a5caf6d Mon Sep 17 00:00:00 2001 From: Dawid Kisielewski Date: Wed, 8 Nov 2023 10:52:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Improve=20find=20query=20performanc?= =?UTF-8?q?e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This code is [taken from admin repo](https://github.com/reedsy/reedsy-editor-admin/blob/a3ebb5d13d0adf7441d324928ad84804edd3ac0e/src/services/adminjs/resources/utilities/patch-find-one.ts#L1-L48) and simplified --- src/utils/convert-filter.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/utils/convert-filter.ts b/src/utils/convert-filter.ts index 5d52269..4b20688 100644 --- a/src/utils/convert-filter.ts +++ b/src/utils/convert-filter.ts @@ -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. * @@ -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()) {