【问题标题】:How can a waterline adapter get access to attributes defined in the model?水线适配器如何访问模型中定义的属性?
【发布时间】:2015-06-17 14:11:48
【问题描述】:

我正在为 RestLike 数据源构建 Sails/WaterLine 适配器。为了将实例返回给 WaterLine,我需要转换结果以处理日期和 null 等内容。为此,我需要访问模型上的属性定义。但我不知道如何访问它们。

sails-rest 似乎以某种方式在连接上存储了一个definition 对象,然后稍后使用它来格式化结果。这几乎是我所需要的,但我不知道这个 definition 对象是如何派生的。

水线适配器如何访问模型中定义的属性?

【问题讨论】:

    标签: sails.js waterline


    【解决方案1】:

    找到了!

    registerConnection方法,获取collections参数

    该对象包含所有模型及其定义。将其存储在连接上,以便稍后在其他适配器方法中引用它。

    registerConnection: function(connection, collections, cb) {
      if(!connection.identity) return cb(new Error('Connection is missing an identity.'));
      if(connections[connection.identity]) return cb(new Error('Connection is already registered.'));
    
      // Add in logic here to initialize connection
      // e.g. connections[connection.identity] = new Database(connection, collections);
    
        var dbConnection = '... create connection here ...'
    
      connections[connection.identity] = {
          dbConnection : dbConnection,
          collections : collections  //  <-- store collection
      }
    
      cb();
    }
    

    ...稍后在需要模型定义的其他函数中

    create: function (connection, collection, values, cb){
    
        // database connection
        var dbConnection = connections[connection].dbConnection;
    
        // model definition
        var definition = connections[connection].collections[collection].definition
    
        // do the rest of the stuff
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 2018-09-10
      • 1970-01-01
      相关资源
      最近更新 更多