Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
require('dotenv-safe').config()
const { Seeder } = require('mongo-seeding')
const path = require('path')
const config = {
database: process.env.MONGO_URI,
inputPath: path.resolve(__dirname, './data'),
dropDatabase: false
}
const seeder = new Seeder(config)
const collections = seeder.readCollectionsFromPath(path.resolve('./data'))
const main = async () => {
try {
await seeder.import(collections)
console.log('Seed complete!')
process.exit(0)
} catch (err) {
console.log(err)
process.exit(0)
}
}
main()
(async () => {
try {
await seedDatabase(envOptions);
} catch (err) {
console.error(`Error ${err.name}: ${err.message}`);
process.exit(1);
}
})();
if (shouldShowHelp(options)) {
showHelp();
return;
}
try {
validateOptions(options);
} catch (err) {
this.printError(err);
return;
}
const config = createConfigFromOptions(options);
this.useCliSpecificOptions(config as DeepPartial);
const seeder = new Seeder(config as DeepPartial);
const collectionsPath = this.getCollectionsPath(options);
const collectionReadingConfig = this.getCollectionReadingConfig(options);
try {
const collections = seeder.readCollectionsFromPath(
resolve(collectionsPath),
collectionReadingConfig,
);
await seeder.import(collections);
} catch (err) {
this.printError(err);
}
process.exit(0);
private getCollectionReadingConfig = (
options: CommandLineArguments,
): SeederCollectionReadingOptions => {
const transformers = [];
const replaceIdWithUnderscoreId =
options['replace-id'] || process.env.REPLACE_ID === 'true';
if (replaceIdWithUnderscoreId) {
transformers.push(Seeder.Transformers.replaceDocumentIdWithUnderscoreId);
}
return {
extensions: ['ts', 'js', 'json'],
transformers,
};
};