Last active
August 2, 2021 11:43
-
-
Save pkosiec/27070f5b716b968028227c64685e87ea to your computer and use it in GitHub Desktop.
Seed MongoDB database programmatically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require('path'); | |
const { Seeder } = require('mongo-seeding'); | |
const config = { | |
database: { | |
name: 'mydbname', | |
}, | |
dropDatabase: true, | |
}; | |
const seeder = new Seeder(config); | |
const collections = seeder.readCollectionsFromPath( | |
path.resolve('./mydb/data-import'), | |
{ | |
transformers: [Seeder.Transformers.replaceDocumentIdWithUnderscoreId], | |
}, | |
); | |
seeder | |
.import(collections) | |
.then(() => { | |
console.log('Success'); | |
}) | |
.catch(err => { | |
console.log('Error', err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment