我有几个grunt任务在我的MySql数据库上执行操作。
为了公开数据库ORM,我首先需要实例化它,然后在回调中运行数据库调用。
问题是,由于每个grunt任务都单独运行,我必须为每个任务实例化ORM。
Here is the snippet I use to instantiate the DB
比方说,我试图这样做:
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'
]);
当运行grunt import:deleteAll时,脚本将在第二个任务上崩溃,告诉我适配器已经初始化。
所以我的问题是,如何在同一个sailsTasksModels.init回调中运行我的三个任务?
由于
答案 0 :(得分:1)
一种选择是使用grunt set:
文档:http://gruntjs.com/api/grunt.config#grunt.config.set
我的一个sails.js项目中的示例:https://github.com/ABS-org/we-cdp/blob/master/tasks/config/watch-sass-theme.js#L8