Skip to content

Packages should be able to seed the database when bootstrapped #118

@timelf123

Description

@timelf123

Either with an env variable and JSON seed file (more flexible, can touch multiple collections), or a more simple one collection check. Example for a package called packageName, this code will run a seed function only if the collection does not exist

 // Seed the database...
 mongoose.connection.db.listCollections({
         name: 'packageName'
     })
     .next(function(err, collinfo) {
         if (!collinfo) {
             console.log('packageName collection does not exist, seeding!');
             var defaultData = require('./server/models/packageName.seed');
             var Model = mongoose.model('packageName');
             Model.insertMany(defaultData, function(err, r) {
                 if (err) {
                     console.log(err);
                 }
                 if (r) {
                     console.log(r);
                 }
             });
         }
     });
packageName.seed

module.exports = [{
  foo: bar,
  baz: fuzz
}];

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions