diff --git a/src/database/utils.js b/src/database/utils.js index e4d1057..a23da52 100644 --- a/src/database/utils.js +++ b/src/database/utils.js @@ -2,7 +2,6 @@ import { ObjectId } from 'mongodb'; import { auditLogModel } from './schemas/management/auditlog.schema.js'; import { notificationModel } from './schemas/misc/notification.schema.js'; import { userNotifierModel } from './schemas/misc/usernotifier.schema.js'; -import { models } from './schemas/models.js'; import { natsServer } from './nats.js'; import { customAlphabet } from 'nanoid'; @@ -21,8 +20,13 @@ function omitSensitive(obj) { return result; } -function getModelEntryByType(parentType) { - return Object.values(models).find( +let modelsCache = null; + +async function getModelEntryByType(parentType) { + if (!modelsCache) { + modelsCache = (await import('./schemas/models.js')).models; + } + return Object.values(modelsCache).find( entry => entry.type === parentType || entry.model?.modelName === parentType ); } @@ -496,7 +500,7 @@ async function editNotification( ) { if (NOTIFICATION_EXCLUDED_MODELS.includes(parentType)) return; - const modelEntry = getModelEntryByType(parentType); + const modelEntry = await getModelEntryByType(parentType); const user = notificationUserFromOwner(owner, ownerType); const objectName = oldValue?.name ?? newValue?.name ?? modelEntry?.label ?? parentType;