UNPKG

mongo-seeding

Version:

The ultimate Node.js library for populating your MongoDB database.

30 lines 987 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getObjectIds = exports.getObjectId = void 0; const mongodb_1 = require("mongodb"); const crypto_1 = require("crypto"); /** * Constructs an ObjectId instance based on entity name. * Helpful for database references between two collections. * * @param name Entity name */ const getObjectId = (name) => { if (name === '') { throw new Error('Name cannot be empty'); } const hash = (0, crypto_1.createHash)('sha1').update(name, 'utf8').digest('hex'); return new mongodb_1.ObjectId(hash.substring(0, 24)); }; exports.getObjectId = getObjectId; /** * Constructs ObjectId instances based on entity names. * Helpful for database references between two collections. * * @param names Array of object names */ const getObjectIds = (names) => { return names.map((name) => (0, exports.getObjectId)(name)); }; exports.getObjectIds = getObjectIds; //# sourceMappingURL=index.js.map