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