diff --git a/src/utils.js b/src/utils.js index 82c059f..30d5fa4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,7 +1,6 @@ import { ObjectId } from 'mongodb'; -import { auditLogModel } from './schemas/management/auditlog.schema.js'; +import { auditLogModel } from './database/schemas/management/auditlog.schema.js'; import exifr from 'exifr'; -import { etcdServer } from './database/etcd.js'; import { natsServer } from './database/nats.js'; import log4js from 'log4js'; import dotenv from 'dotenv'; @@ -12,14 +11,6 @@ dotenv.config(); const logger = log4js.getLogger('Utils'); logger.level = process.env.LOG_LEVEL; -import { customAlphabet } from 'nanoid'; - -const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; -const generateId = () => { - // 10 characters - return customAlphabet(ALPHABET, 12); -}; - function buildWildcardRegexPattern(input) { // Escape all regex special chars except * (which we treat as a wildcard) const escaped = input.replace(/[.+?^${}()|[\]\\]/g, '\\$&'); @@ -434,6 +425,10 @@ async function distributeUpdate(value, id, type) { await natsServer.publish(`${type}s.${id}.object`, value); } +async function distributeStats(value, type) { + await natsServer.publish(`${type}s.stats`, value); +} + async function distributeNew(value, type) { await natsServer.publish(`${type}s.new`, value); } @@ -693,6 +688,21 @@ function jsonToCacheKey(obj) { return hash; } +export function getQueryToCacheKey({ model, id, populate }) { + const populateKey = []; + if (populate) { + const populateArray = Array.isArray(populate) ? populate : [populate]; + for (const pop of populateArray) { + if (typeof pop === 'string') { + populateKey.push(pop); + } else if (typeof pop === 'object' && pop.path) { + populateKey.push(pop.path); + } + } + } + return `${model}:${id?.toString()}-${populateKey.join(',')}`; +} + export { parseFilter, convertToCamelCase, @@ -703,6 +713,7 @@ export { flatternObjectIds, expandObjectIds, distributeUpdate, + distributeStats, distributeNew, distributeDelete, distributeChildUpdate, @@ -714,5 +725,4 @@ export { modelHasRef, getFieldsByRef, jsonToCacheKey, - generateId, };