UNPKG

993 BJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getObjectIds = exports.getObjectId = void 0;
4const mongodb_1 = require("mongodb");
5const crypto_1 = require("crypto");
6/**
7 * Constructs an ObjectId instance based on entity name.
8 * Helpful for database references between two collections.
9 *
10 * @param name Entity name
11 */
12const getObjectId = (name) => {
13 if (name === '') {
14 throw new Error('Name cannot be empty');
15 }
16 const hash = crypto_1.createHash('sha1')
17 .update(name, 'utf8')
18 .digest('hex');
19 return new mongodb_1.ObjectId(hash.substring(0, 24));
20};
21exports.getObjectId = getObjectId;
22/**
23 * Constructs ObjectId instances based on entity names.
24 * Helpful for database references between two collections.
25 *
26 * @param names Array of object names
27 */
28const getObjectIds = (names) => {
29 return names.map(name => exports.getObjectId(name));
30};
31exports.getObjectIds = getObjectIds;
32//# sourceMappingURL=index.js.map
\No newline at end of file