UNPKG

short-objectid

Version:

The idea of this project is make your details route easier 🥰 ..

57 lines (56 loc) • 2.28 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const cache_1 = __importDefault(require("./cache")); const utils_1 = require("./utils"); // ################################ // # short-objectid # // ################################ /** * The idea of this project is make your details * route easier by convert objectid to number 🥰 .. * * @param {MongoDB_ObjectId} _id: MongoDB Object ID .. * @param {UserConfig} config: Custom User Configuration .. */ function shortObjectId(_id, config, opts) { // TODO: remove in next release ... if ((opts === null || opts === void 0 ? void 0 : opts.cache) && opts.keepOriginal) { throw new Error(`Should you use one option. \n In the next release, the option keepOriginal will be overridden by the cache options.`).message; } const [status, msg] = utils_1.isObjectId(_id); if (!status) { throw new Error(msg).message; } const shortObjectId = utils_1.shortObjectIdCore(_id, utils_1.setConfig(config)); if (opts === null || opts === void 0 ? void 0 : opts.cache) { // subscribe for all update without duplicate data .. cache_1.default.getInstance().subscribe({ shortObjectId, oid: _id.$oid }); return { shortObjectId, getFullObjectId: (shortObjectId) => cache_1.default.getInstance().getFullObjectId(shortObjectId), getShortObjectId: (fullObjectId) => cache_1.default.getInstance().getShortObjectId(fullObjectId) }; } if (opts === null || opts === void 0 ? void 0 : opts.keepOriginal) { return [shortObjectId, _id.$oid]; } return shortObjectId; } exports.default = shortObjectId; exports.shortObjectId = shortObjectId; ; /** * Cache access ....... * */ const shortObjectIdCache = (function () { return cache_1.default.getInstance(); })(); exports.shortObjectIdCache = shortObjectIdCache; // For CommonJS module.exports.default = shortObjectId; module.exports.shortObjectId = shortObjectId; module.exports.shortObjectIdCache = shortObjectIdCache;