UNPKG

mongo-seeding

Version:

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

89 lines 3.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.mergeCollectionReadingOptions = exports.defaultCollectionReadingOptions = exports.mergeConnection = exports.mergeSeederConfigAndDeleteDb = exports.defaultSeederConfig = void 0; const extend = require("extend"); const connection_string_1 = require("connection-string"); const database_1 = require("./database"); /** * Stores default configuration for database seeding. */ exports.defaultSeederConfig = { databaseReconnectTimeout: 10000, dropDatabase: false, dropCollections: false, removeAllDocuments: false, }; /** * Merges configuration for seeding and deletes database property. * * @param partial Partial config object. If not specified, returns a default config object. * @param previous Previous config object. If not specified, uses a default config object as a base. */ const mergeSeederConfigAndDeleteDb = (partial, previous) => { const source = previous ? previous : exports.defaultSeederConfig; if ('database' in source) { delete source.database; } if (!partial) { return source; } const config = {}; delete partial.database; return extend(true, config, source, partial); }; exports.mergeSeederConfigAndDeleteDb = mergeSeederConfigAndDeleteDb; const mergeConnection = (partial, previous) => { const source = previous !== null && previous !== void 0 ? previous : (0, database_1.parseSeederDatabaseConfig)(undefined); if (source.hosts && source.hosts.length > 1) { source.hosts = [source.hosts[0]]; } if (!partial) { return source; } if (typeof partial === 'string') { return (0, database_1.parseSeederDatabaseConfig)(partial); } const partialConn = (0, database_1.parseSeederDatabaseConfig)(partial, true); // override hosts manually if (partialConn.hosts && partialConn.hosts.length > 0 && source.hosts && source.hosts.length > 0) { const newHost = partialConn.hosts[0]; if (!newHost.name) { newHost.name = source.hosts[0].name; } if (!newHost.port) { newHost.port = source.hosts[0].port; } } const config = new connection_string_1.ConnectionString(); return extend(true, config, source, partialConn); }; exports.mergeConnection = mergeConnection; /** * Stores default collection reading configuration values. */ exports.defaultCollectionReadingOptions = { extensions: ['json', 'js', 'cjs'], ejsonParseOptions: { relaxed: true, }, transformers: [], }; /** * Merges configuration for collection reading. * * @param partial Partial config object. If not specified, returns a default config object. * @param previous Previous config object. If not specified, uses a default config object as a base. */ const mergeCollectionReadingOptions = (partial, previous) => { const source = previous ? previous : exports.defaultCollectionReadingOptions; if (!partial) { return source; } const config = {}; return extend(true, config, source, partial); }; exports.mergeCollectionReadingOptions = mergeCollectionReadingOptions; //# sourceMappingURL=config.js.map