jlbali
jlbali

Reputation: 11

Meteor - resetting the database and creating an init script

We are developing a Meteor application and we need some data to be readily available in the Mongo database after creation.

For example, we have a Mongo Collection named Users and we want to start with two users (say John and Susan). We don't want to do this manually, so after a Mongo reset it would be great if these data were added automatically to the Mongo database of the application thanks to a script.

What is the best and "most polished" way to accomplish this?

Upvotes: 1

Views: 1316

Answers (2)

kuzyn
kuzyn

Reputation: 1995

What about creating a db_init.js file with something like:

if (Meteor.isServer && (process && process.env && process.env.NODE_ENV === "development")) {
  Meteor.startup(function () {

    var newUser = {
        username: 'test',
        email: '[email protected]',
        profile: {
            phone: '555-555-5555'
        },
        password: 'test'
    };

    Accounts.createUser(newUser);

  });
}

This could be coupled with something like percolatestudio/meteor-factory

Upvotes: 0

Kyll
Kyll

Reputation: 7139

The first, simple and maybe naive solution is to use some start-up script, like /dev/init_data.js:

Meteor.startup(function() {
  if(typeof someCollection.findOne() === 'undefined') {
    someCollection.insert(...);
  }
});

But that would go into deployment... Unless you manually (ugh) erase the /dev folder before deploying or do some other ugly trick to get the code to be inactive. We don't want this code deployed at all.
So let's use a debug package instead!

meteor create --package devinitdata

In your fresh package.js file:

Package.describe({
  name: 'devinitdata',
  ...
  debugOnly : true //!
});

And you can just write your init code in the package files to fill your collections with all kind of messy data.

Upvotes: 6

Related Questions