UNPKG

dce-mango

Version:

Harvard DCE's Non-relational DB Wrapper.

136 lines 5.43 kB
"use strict"; 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 dbState const dbState_1 = __importDefault(require("../dbState")); /** * Initialize a collection. * @author Gabe Abrams * @param collectionName the name of the collection * @param options the options tied to the collection * @returns a Promise which resolves to the Mongo collection */ const initCollection = (collectionName, options) => __awaiter(void 0, void 0, void 0, function* () { var _a; // Get the collection names const collectionNames = yield dbState_1.default.initDB; // Variable for the collection let collection; // Create collection if it doesn't exist if (collectionNames.indexOf(collectionName) < 0) { // Collection doesn't exist. Create it try { // Create collection yield dbState_1.default.db.createCollection(collectionName); // Get the collection collection = dbState_1.default.db.collection(collectionName); } catch (err) { // eslint-disable-next-line no-console console.log('DCE-MANGO: An error occurred while creating collections. Fatal error. Now exiting.'); // eslint-disable-next-line no-console console.log(err); process.exit(1); } } else { // Collection exists. Get it collection = dbState_1.default.db.collection(collectionName); } // Get the indexes const existingIndexes = yield collection.indexes(); // If any of the indexes are old, drop them all const dropAllIndexes = (existingIndexes .some((index) => { // Ignore invalid indexes if (!index || !index.name || typeof index.name !== 'string') { return false; } // Ignore ttl index if (index.name === 'ttl-index') { return false; } // Ignore default index if (index.name.startsWith('_')) { return false; } // Check if this index is from a previous version return (!index.name.includes(dbState_1.default.schemaVersionTag)); })); // Drop all indexes if need be if (dropAllIndexes) { try { // eslint-disable-next-line no-console console.log(`DCE-MANGO: Found old schema for collection ${collectionName}! Dropping all indexes and re-creating them.`); yield collection.dropIndexes(); } catch (err) { // eslint-disable-next-line no-console console.log('DCE-MANGO: An error occurred while dropping old indexes. Fatal error. Now exiting.'); // eslint-disable-next-line no-console console.log(err); process.exit(1); } } // Gather a list of indexes that need to be created const indexesToCreate = []; // Add unique index to the list if (options.uniqueIndexKey) { // Create index options const indexOpts = { key: { [options.uniqueIndexKey]: 1, }, name: `main-${options.uniqueIndexKey}-unique-index-${dbState_1.default.schemaVersionTag}`, unique: true, background: false, }; // Add expiry if (options.expireAfterSeconds) { indexOpts.expireAfterSeconds = options.expireAfterSeconds; } // Add the index to the list indexesToCreate.push(indexOpts); } // Add secondary indexes to the list (options.indexKeys || []).forEach((secondaryIndexKey) => { indexesToCreate.push({ key: { [secondaryIndexKey]: 1, }, name: `secondary-${secondaryIndexKey}-index-${dbState_1.default.schemaVersionTag}`, unique: false, background: false, }); }); // Create the indexes try { yield collection.createIndexes(indexesToCreate); if (((_a = process.env.MANGO_LOG_LEVEL) !== null && _a !== void 0 ? _a : '').toLowerCase() === 'info') { // eslint-disable-next-line no-console console.log('DCE-MANGO: Created indexes. ', indexesToCreate); } } catch (err) { // eslint-disable-next-line no-console console.log('DCE-MANGO: An error occurred while creating indexes. Fatal error. Now exiting.'); // eslint-disable-next-line no-console console.log(err); process.exit(1); } // Return the collection return collection; }); exports.default = initCollection; //# sourceMappingURL=initCollection.js.map