【问题标题】:How do I access Waterline models from grunt tasks?如何从 grunt 任务中访问 Waterline 模型?
【发布时间】:2014-12-01 10:18:15
【问题描述】:

TL;DR - 我如何在一个 grunt 任务中使用我的 Waterline 模型,在一个sails 项目中?

我正在尝试在 Sails 项目中创建一些繁重的任务来处理 CSV 文件并将数据插入 Postgres 数据库。我希望为此使用 Waterline,但未能成功连接到数据库。

我想知道,我将如何在繁重的任务中使用我的 Waterline 模型?

我不太确定 Sails 在幕后做了什么来使我的模型易于访问,尽管我很想了解它。

我将在早上更新我尝试过的示例代码。

【问题讨论】:

  • 来自 PostgreSQL 背景,这就像流行语汤。帆,咕噜声,水线,是吧?如果没有代码和错误,即使我确实对您使用的工具有所了解,也无法有效地对此做出响应。
  • Grunt 和 Rake 一样,都是任务执行者。在运行 Sails 服务器时,Sails CLI 负责注册您的 Waterline 模型,并使它们在全球范围内可访问。我想使用 Waterline 模型从一项繁重的任务中与 Postgres 进行交互,但无法做到,因为我不知道 Sails 在幕后做了什么。我相信如果你理解我所说的工具,这个问题就相当简单了。如何在风帆项目中使用 grunt 任务中的 Waterline 模型?我现在将添加代码示例。
  • 感谢您的解释。我表达的更多是困惑而不是要求任何东西。 Node 和 Rails 社区都以惊人的速度生产新工具(名称很神秘)。

标签: node.js postgresql gruntjs sails.js waterline


【解决方案1】:

你也可以这样做

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();

    require('sails').load({
        hooks: {
            blueprints: false,
            controllers: false,
            cors: false,
            csrf: false,
            grunt: false,
            i18n: false,
            logger: false,
            policies: false,
            pubsub: false,
            request: false,
            responses: false,
            session: false,
            socket: false,
            views: false
        }
    }, function(err, app){
        // access to models, do your magic here

        done();
    });
});

【讨论】:

    【解决方案2】:

    我导入的东西有误。以下工作正常:

    
    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 格式。关键是在模型中指定连接。

    【讨论】:

      猜你喜欢
      • 2013-02-23
      • 2015-08-05
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2011-07-13
      • 1970-01-01
      相关资源
      最近更新 更多