0

I have several grunt tasks performing operations on my MySql database.

In order to expose the database ORM, I first need to instantiate it and then run the database calls within the callback.

The problem is that since every grunt task run separately, I have to instantiate the ORM for every tasks.

Here is the snippet I use to instantiate the DB

Let's say for instance, I'm trying to do this:

grunt.registerTask('import:cleanUsers', 'Clean the DB', function() {
  var done = this.async();
  var sql = 'delete from user';

  sailsTasksModels.init(function(ontology) {
    ontology.collections.country.query(sql, function(err, results) {
      if (!err) console.log('User table has been emptied');
      done(results);
    });
  });
});

grunt.registerTask('import:cleanRadios', 'Clean the DB', function() {
  var done = this.async();
  var sql = 'delete from radio';

  sailsTasksModels.init(function(ontology) {
    ontology.collections.country.query(sql, function(err, results) {
      if (!err) console.log('Radio table has been emptied');
      done(results);
    });
  });
});

grunt.registerTask('import:cleanCampaigns', 'Clean the DB', function() {
  var done = this.async();
  var sql = 'delete from campaign';

  sailsTasksModels.init(function(ontology) {
    ontology.collections.country.query(sql, function(err, results) {
      if (!err) console.log('Campaign table has been emptied');
      done(results);
    });
  });
});

grunt.registerTask('import:deleteAll', [
  'import:cleanUsers',
  'import:cleanRadios',
  'import:cleanCampaigns'
]);

When running grunt import:deleteAll, the script will crash on the second tasks telling me the adapter has already been intitialized.

So my question is, how can I run my three tasks within the same sailsTasksModels.init callback?

Thanks

m0g
  • 115
  • 1
  • 14
  • I would suggest redesigning your `SailsTasksModels` to be a singleton. In this case it'll be initialized only once, no matter how many time you'll use it in your code. – Leonid Beschastny Nov 26 '14 at 11:10

1 Answers1

1

One option is to use grunt set:

Docs: http://gruntjs.com/api/grunt.config#grunt.config.set

  1. Register the main task
  2. Set configs in others files of modules

Example in one of my sails.js projects: https://github.com/ABS-org/we-cdp/blob/master/tasks/config/watch-sass-theme.js#L8

Alberto Souza
  • 681
  • 5
  • 10
  • 1
    Cool, it worked out. So when passing a object to grunt.config.set, when you want to retrieve it, you have to grunt.config.getRaw. Here is a little example https://gist.github.com/m0g/c92fa94086b271e37976 – m0g Nov 28 '14 at 08:57