Moved schema id generation.

This commit is contained in:
Tom Butcher 2025-12-13 21:13:17 +00:00
parent 09eba639d0
commit 528e57dd9a

View File

@ -1,7 +1,6 @@
import { ObjectId } from 'mongodb'; 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 exifr from 'exifr';
import { etcdServer } from './database/etcd.js';
import { natsServer } from './database/nats.js'; import { natsServer } from './database/nats.js';
import log4js from 'log4js'; import log4js from 'log4js';
import dotenv from 'dotenv'; import dotenv from 'dotenv';
@ -12,14 +11,6 @@ dotenv.config();
const logger = log4js.getLogger('Utils'); const logger = log4js.getLogger('Utils');
logger.level = process.env.LOG_LEVEL; logger.level = process.env.LOG_LEVEL;
import { customAlphabet } from 'nanoid';
const ALPHABET = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
const generateId = () => {
// 10 characters
return customAlphabet(ALPHABET, 12);
};
function buildWildcardRegexPattern(input) { function buildWildcardRegexPattern(input) {
// Escape all regex special chars except * (which we treat as a wildcard) // Escape all regex special chars except * (which we treat as a wildcard)
const escaped = input.replace(/[.+?^${}()|[\]\\]/g, '\\$&'); const escaped = input.replace(/[.+?^${}()|[\]\\]/g, '\\$&');
@ -434,6 +425,10 @@ async function distributeUpdate(value, id, type) {
await natsServer.publish(`${type}s.${id}.object`, value); 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) { async function distributeNew(value, type) {
await natsServer.publish(`${type}s.new`, value); await natsServer.publish(`${type}s.new`, value);
} }
@ -693,6 +688,21 @@ function jsonToCacheKey(obj) {
return hash; 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 { export {
parseFilter, parseFilter,
convertToCamelCase, convertToCamelCase,
@ -703,6 +713,7 @@ export {
flatternObjectIds, flatternObjectIds,
expandObjectIds, expandObjectIds,
distributeUpdate, distributeUpdate,
distributeStats,
distributeNew, distributeNew,
distributeDelete, distributeDelete,
distributeChildUpdate, distributeChildUpdate,
@ -714,5 +725,4 @@ export {
modelHasRef, modelHasRef,
getFieldsByRef, getFieldsByRef,
jsonToCacheKey, jsonToCacheKey,
generateId,
}; };