2017-04-04 3 views
0

돛 프로젝트가 아닌 프로젝트에서 수선을 설정했습니다. 저는 0.11.x 버전입니다. 각 모델에서 연결을 설정하면 완벽하게 작동합니다.sailsjs없이 전 세계에서 흘수선 모델 연결 설정

Waterline = require('waterline'); 

module.exports = Waterline.Collection.extend({ 
    tableName: 'external_resource', 
    meta: { 
    schemaName: 'intellinotedb' 
    }, 
    // connection: 'myLocalPostgres', 
    attributes: { 
    id: { 
     type: 'integer', 
     required: true 
    }, 

그러나, 내가 시험에서 데이터베이스를 변경하려면 : (첫 번째 코드 블록의 주석 라인)

나는이 문서에 따라 각 모델에서 연결을 설정할 때 내 모델이 제대로 작동해야 내가 설정 한 NODE_ENV var에 기반하여 로컬로 프로덕션 할 수 있습니다. 모든 모델에 대해 전 세계적으로이를 수행 할 수있는 곳이 있습니까? 나는이 같은 모든 모델을로드 :

models = [] 
fs.readdirSync(HOMEDIR + '/lib/models/waterline').forEach (file) => 
    models.push(require(HOMEDIR + '/lib/models/waterline/' + file)) 

class WaterlineORM 

    init:(next)=> 
    models.forEach (model) -> 
     orm.loadCollection(model) 

    orm.initialize config, (err, models) => 
     throw err if(err) 
     global.models = models.collections 
     global.connections = models.connections 
     next() 

내 연결은 다음과 같이 :

const diskAdapter = require('sails-disk'); 
const postgresAdapter = require('sails-postgresql'); 

module.exports = { 
    adapters: { 
    'default': diskAdapter, 
    disk: diskAdapter, 
    postgres: postgresAdapter 
    }, 

    connections: { 
    myLocalDisk: { 
     adapter: 'disk', 
     migrate: 'alter' 
    }, 

    localhost: { 
     migrate: 'safe', 
     adapter: 'postgres', 
     database: 'intellinote', 
     host: 'localhost', 
     user: 'postgres', 
     password: '', 
     port: 5432 
    } 
    }, 

    defaults: { 

    } 
}; 
+1

변수를 사용할 수 있습니까? 예 : 환경 변수를 다음과 같이 설정하십시오 :'connection : process.env.CONNECTION_NAME, ' – Sangharsh

+1

예. 그럴 가능성이 가장 높습니다. 네가 원하면 너의 대답을 받아 들일거야. – joncodo

답변

1

사용 변수를.

예. 환경 변수

connection: process.env.CONNECTION_NAME, 

예 : 구성 변수

var config = require('../config'); 
... 
    connection: config.connectionName,