mongo-seeding
Version:
The ultimate Node.js library for populating your MongoDB database.
58 lines • 2.23 kB
JavaScript
;
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CollectionImporter = void 0;
/**
* Allows to import collections into database.
*/
class CollectionImporter {
/**
* Constructs new `CollectionImporter` instance.
*
* @param db Database object
* @param bulkWriteOptions Optional MongoDB bulk write options
* @param log Optional logger instance
*/
constructor(db, bulkWriteOptions, log) {
this.db = db;
this.bulkWriteOptions = bulkWriteOptions;
this.log = log
? log
: () => {
// do nothing
};
}
/**
* Imports multiple collections into database.
*
* @param collections Array of collections
*/
import(collections) {
return __awaiter(this, void 0, void 0, function* () {
for (const collection of collections) {
yield this.importCollection(collection);
}
});
}
/**
* Imports single collection into database.
*
* @param collection Collection definition
*/
importCollection(collection) {
return __awaiter(this, void 0, void 0, function* () {
this.log(`Inserting ${collection.documents.length} documents into collection ${collection.name}...`);
return this.db.insertDocumentsIntoCollection(collection.documents, collection.name, this.bulkWriteOptions);
});
}
}
exports.CollectionImporter = CollectionImporter;
//# sourceMappingURL=index.js.map