UNPKG

@overture-stack/lyric

Version:
28 lines (27 loc) 1.05 kB
import { customAlphabet } from 'nanoid'; import { NotImplemented } from '../utils/errors.js'; import { uniqueCharacters } from '../utils/formatUtils.js'; const systemIdGenerator = (dependencies) => { const LOG_MODULE = 'ID_GENERATOR'; const { idService, logger } = dependencies; // custom alphabet const alphabet = uniqueCharacters(idService.customAlphabet) || 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // custom size const size = idService.customSize || 21; const nanoid = customAlphabet(alphabet, size); return { /** * Generate unique ID on system database * @returns {string} generated ID */ generateIdentifier: (entityName, _dataRecord) => { if (idService.useLocal) { const id = nanoid(); logger.debug(LOG_MODULE, `System ID '${id}' generated for entity '${entityName}'`); return id; } throw new NotImplemented('ID Service not configured'); }, }; }; export default systemIdGenerator;