2014-02-18 59 views

回答

10

Waterline處於其當前狀態Sails框架的子項目。

您正在搜索的是傳統的數據庫配置。當使用水線作爲Sails的一部分時,這個約定將通過Sails自動配置文件到全局的sails對象的方式來定義。

當自己使用水線時,您必須自己照顧這部分:您想引導並將您的配置明確地傳遞到水線。什麼你必須通過一步一步做:

  1. 要求水線和正確的水線適配器,你的情況:sails-postgresql
  2. 指定adapters配置
  3. 指定connections配置,這將有問題的配置
  4. 定義並加載您collections
  5. 初始化水線

一個例子是如何做到這一切,從這些例子水線得出:https://github.com/balderdashy/waterline/blob/master/example/

// 1. Require Waterline and the correct Waterline adapter 
Waterline = require('waterline'), 
postgreAdapter = require('sails-postgresql'); 

var config = { 
    // 2. Specify `adapters` config 
    adapters: { 
    postgre: postgreAdapter 
    }, 

    // 3. Specify `connections` config 
    postgreDev: { 
    adapter: 'postgre', 
    host: 'localhost', 
    database: 'development', 
    user: 'developer', 
    password: 'somethingsupersecret' 
    } 
}; 

// 4. Define and load your collections 
var User = Waterline.Collection.extend({ 
    // collection.identity and collection.connection 
    // have to be specified explicitly when using Waterline without Sails 
    identity: 'user', 
    connection: 'postgreDev', 

    attributes: { 
    ... 
    } 
}); 

var waterline = new Waterline(); 
waterline.loadCollection(User); 

// 5. Initialize Waterline 
waterline.initialize(config, function(err, models) { 
    if (err) throw err; 

    // Expose your models for further use 
}); 
+1

在回調中導出某些內容是有點不正常的。如果你想module.export你的模型,並要求他們在不同的文件? –

+2

我想你必須在'connections'中包裝'postgreDev ...',否則你會得到一個異常 – davidhq