Vigas Deep
Vigas Deep

Reputation: 122

Import test-users into Meteor app with use of npm script?

We can easily create a user from meteor shell like this

Accounts.createUser({username: 'john', password: '12345'})

Similarly, I just want to add multiple users via npm script. Any ideas?

In other words, I want to use fixtures functionality via npm command and not on the initial run.

Thank you.

Upvotes: 0

Views: 56

Answers (1)

ghybs
ghybs

Reputation: 53270

For normal collections (i.e. different than Meteor.users), you can directly tap into your MongoDB collection. Open a Meteor Mongo shell while your project is running in development mode, then directly type Mongo shell commands.

For Meteor.users collection, you want to leverage the accounts-base and accounts-password packages automatic management, so instead of directly fiddling the MongoDB, you want to insert documents / users through your Meteor app.

Unfortunately, your app source files (like your UsersFixtures.js file) are absolutely not suitable for CLI usage.

The usual solution is to embed a dedicated method within your app server:

// On your server.
// Make sure this Method is not available on production.
// When started with `meteor run`, NODE_ENV will be `development` unless set otherwise previously in your environment variables.
if (process.env.NODE_ENV !== 'production') {
  Meteor.methods({
    addTestUser(username, password) {
      Accounts.createUser({
        username,
        password // If you do not want to transmit the clear password even in dev environment, you can call the method with 2nd arg: {algorithm: "sha-256", digest: sha256function(password)}
      })
    }
  });
}

Then start your Meteor project in development mode (meteor run), access your app in your browser, open your browser console, and directly call the method from there:

Meteor.call('addTestUser', myUsername, myPassword)

You could also use Accounts.createUser directly in your browser console, but it will automatically log you in as the new user.

Upvotes: 1

Related Questions