TL;DR - 如何在 sails 项目的 grunt 任务中使用我的 Waterline 模型?
我正在尝试在 Sails 项目中创建一些繁重的任务来处理 CSV 文件并将数据插入 Postgres 数据库。我希望为此使用 Waterline,但未能成功连接到数据库。
我想知道,如何在 grunt 任务中使用我的 Waterline 模型?
我不太确定 Sails 在幕后做了什么使我的模型易于访问,但我很想了解。
我会在早上更新我尝试过的示例代码。
最佳答案
我输入错误。以下工作正常:
var CWD = process.cwd();
var path = require('path');
var Waterline = require('waterline');
var User = Waterline.Collection.extend(require(path.join(CWD, 'api/models/User')));
module.exports = function (grunt) {
grunt.registerTask('seedDb', 'Given a list of addresses, assign long and lat.', function (clinicCsv, outputCsv) {
// tell grunt this task is async
var done = this.async();
// create ORM
var orm = new Waterline();
orm.loadCollection(User);
// initialize ORM
orm.initialize({
adapters: {
'sails-postgresql': require('sails-postgresql')
},
connections: require(path.join(CWD, 'config/connections')).connections,
defaults: require(path.join(CWD, 'config/models')).models
}, function (err, ontology) {
if (err) throw err;
console.log(ontology.collections);
done();
});
});
};
api/models/User 在哪里:
module.exports = {
identity: 'User',
connection: 'localPostgresqlServer',
attributes: {
firstName: {
type: 'string'
},
lastName: {
type: 'sting'
},
email: {
type: 'email'
},
password: {
type: 'string'
}
}
};
并且 config/connections 和 config/models 是标准的 Sails 格式。关键是指定模型中的连接。
关于node.js - 如何从 grunt 任务访问 Waterline 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26228370/