UNPKG

dce-mango

Version:

Harvard DCE's Non-relational DB Wrapper.

145 lines 7.31 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); // Import Mongo const mongodb_1 = require("mongodb"); // Import dotenv const dotenv = __importStar(require("dotenv")); // Import state const dbState_1 = __importDefault(require("./dbState")); /*------------------------------------------------------------------------*/ /* Initialization */ /*------------------------------------------------------------------------*/ /** * Get a copy of the db collection class with * Mongo/Amazon DocDB wrapper with functions that are * equally compatible with both * @author Gabe Abrams * @param [opts] object containing all arguments * @param [opts.schemaVersion=1] the version of the schema. Whenever you * change your schema, increment this number and dce-mango will automatically * re-build and re-provision your collections * @param [dbName=env.DB_NAME ?? 'mango-store'] the name of the database * to set up/use * @param [dbInfo=env vars] either include an object * with a mongo-style url: { url } or include an object * with pieces of a mongo-style url: * { user, pass, host, [options] } where options * is optional. * Otherwise, dce-mango will use environment variables instead: * either env.DB_URL or env.DB_USER + DB_PASS + DB_HOST must * be included, optionally with an included DB_OPTIONS. You can also use * mongo env vars: env.MONGO_URL or env.MONGO_USER + MONGO_PASS + MONGO_HOST * and optionally MONGO_OPTIONS * @returns collection class that references the given database */ const initMango = (opts) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p; // Pull environment variables from .env file dotenv.config(); // TODO: should we have overwrite functionality? if (dbState_1.default.isInitialized) { return; } dbState_1.default.isInitialized = true; // Get opts and set defaults const { schemaVersion } = opts; const dbName = ((_a = opts.dbName) !== null && _a !== void 0 ? _a : ((_b = process.env.DB_NAME) !== null && _b !== void 0 ? _b : 'mango-store')); const dbInfo = ((_c = opts.dbInfo) !== null && _c !== void 0 ? _c : process.env); // Get db info as parts const url = ((_e = (_d = dbInfo.url) !== null && _d !== void 0 ? _d : dbInfo.DB_URL) !== null && _e !== void 0 ? _e : dbInfo.MONGO_URL); const user = ((_g = (_f = dbInfo.user) !== null && _f !== void 0 ? _f : dbInfo.DB_USER) !== null && _g !== void 0 ? _g : dbInfo.MONGO_USER); const pass = ((_j = (_h = dbInfo.pass) !== null && _h !== void 0 ? _h : dbInfo.DB_PASS) !== null && _j !== void 0 ? _j : dbInfo.MONGO_PASS); const host = ((_l = (_k = dbInfo.host) !== null && _k !== void 0 ? _k : dbInfo.DB_HOST) !== null && _l !== void 0 ? _l : dbInfo.MONGO_HOST); const options = ((_o = (_m = dbInfo.options) !== null && _m !== void 0 ? _m : dbInfo.DB_OPTIONS) !== null && _o !== void 0 ? _o : dbInfo.MONGO_OPTIONS); // Make sure we have enough info to get a db url if (!url && (!user || !pass || !host)) { // eslint-disable-next-line no-console console.log('DCE-MANGO: No database information included. Either include a database url or include all of the following: user, pass, host. Fatal error. Now exiting.'); process.exit(1); } // Get db url const dbURL = (url !== null && url !== void 0 ? url : `mongodb://${user}:${pass}@${host}/${dbName}${(options) ? '?' : ''}${(_p = (options)) !== null && _p !== void 0 ? _p : ''}`); // Create a schema version tag dbState_1.default.schemaVersionTag = `version_${schemaVersion}`; // Promise that resolves with collection names dbState_1.default.initDB = (() => __awaiter(void 0, void 0, void 0, function* () { // Connect the mongo client let client; try { const potentialClient = yield mongodb_1.MongoClient.connect(dbURL); // Make sure the client is defined if (!potentialClient) { throw new Error('Database returned an empty client'); } // Save the client client = potentialClient; } catch (err) { // This is a fatal error // eslint-disable-next-line no-console console.log('We could not connect to the database. This is a fatal error! Now exiting.'); // eslint-disable-next-line no-console console.log(err); process.exit(1); } // Get the database and its collections let collections; try { // Use client to get database dbState_1.default.db = client.db(dbName); dbState_1.default.client = client; // Get the list of collections collections = yield dbState_1.default.db.listCollections().toArray(); } catch (err) { // This is a fatal error // eslint-disable-next-line no-console console.log('We could not list the collections in the database. This is a fatal error! Now exiting.'); // eslint-disable-next-line no-console console.log(err); process.exit(1); } // Return the list of collection names return collections.map((collection) => { return collection.name; }); }))(); }; exports.default = initMango; //# sourceMappingURL=initMango.js.map