在Sails.js中处理数据库环境配置 [英] Handling database environment configuration in Sails.js

查看:142
本文介绍了在Sails.js中处理数据库环境配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题与官方文档


注意如果使用与适配器的任何连接通过模型,那么到该适配器的所有连接将被加载到sails.lift上,无论模型是否实际使用它们。在上面的示例中,如果模型配置为使用localMysql连接,则localMysql和remoteMysql都会在运行时尝试连接。因此,良好的做法是按环境拆分连接配置,并将其保存到适当的特定于环境的配置文件,否则注释掉您不想要的活动连接。




如何配置生产服务器的连接?



我的 connections.js 文件看起来像这样:

  module.exports.connections = {

mongoDev:{
adapter:'sails-mongo',
host:'localhost',
port:27017,
user:'username',
password:'password',
database:'database'
},

mongoLive:{
adapter:'sails-mongo',
host:'host.mongolab.com',
port:31681,
user:'user',
password:'password',
database:'database'
}
}

在我的环境配置文件中我有:



development.js

  module.exports = {
models: {
connection:'mongoDev'
}
};

production.js


$ b b

  module.exports = {
models:{
connection:'mongoLive'
},
port:3000,
};

这在我的本地计算机上工作,因为生产数据库服务器在外部服务器上。
在生产环境我得到以下错误:

  [错误:连接到[localhost: 27017]] 



如果从连接对象中删除mongoDev对象,

我也尝试过使用adaptors.js,但这只会导致一些弃用错误。

  $ sails -v 
info:v0.9.9

sails lift 时:

  b info:v0.10.5 


解决方案

在development.js或production.js中的实际连接定义,并从connections.js中删除它们。这有点不直观。



development.js

  .exports = {
connections:{
mongoDev:{
adapter:'sails-mongo',
host:'localhost',
port:27017,
user:'username',
password:'password',
database:'database'
}
},
models:{
connection :'mongoDev'
}
};

production.js

  module.exports = {
connections:{
mongoLive:{
adapter:'sails-mongo',
host:'host.mongolab.com' ,
port:31681,
user:'user',
password:'password',
database:'database'
}
} b $ b模型:{
connection:'mongoLive'
},
port:3000,
};


The issue I have is related to the following quote from the official documentation:

Note If any connection to an adapter is used by a model, then all connections to that adapter will be loaded on sails.lift, whether or not models are actually using them. In the example above, if a model was configured to use the localMysql connection, then both localMysql and remoteMysql would attempt to connect at run time. It is therefore good practice to split your connection configurations up by environment and save them to the appropriate environment-specific config files, or else comment out any connections that you don't want active.

How can you configure the connection for a production server?

My connections.js file looks like this:

module.exports.connections = {

  mongoDev: {
    adapter: 'sails-mongo',
    host: 'localhost',
    port: 27017,
    user: 'username',
    password: 'password',
    database: 'database'
  },

  mongoLive: {
    adapter: 'sails-mongo',
    host: 'host.mongolab.com',
    port: 31681,
    user: 'user',
    password: 'password',
    database: 'database'
  }   
};

And in my environment config files I've got:

development.js

module.exports = {
  models: {
    connection: 'mongoDev'
  }    
};

production.js

module.exports = {
  models: {
     connection: 'mongoLive'
  },
  port: 3000,
};

This works on my local machine, because the production database server is on an external server. On the production environment I'm getting the following error:

[Error: failed to connect to [localhost:27017]]

It works if I remove the mongoDev object from my the connections object.

I've also tried using adaptors.js, but this only resulted in some deprecation errors.

$ sails -v
info: v0.9.9

I'm getting something different when running sails lift:

info:    Sails         
info:    v0.10.5

解决方案

You want to save the actual connection definition in the either development.js or production.js and remove them from connections.js. It's a little non-intuitive.

development.js

module.exports = {
  connections : {
    mongoDev: {
      adapter: 'sails-mongo',
      host: 'localhost',
      port: 27017,
      user: 'username',
      password: 'password',
      database: 'database'
    }
  },
  models: {
    connection: 'mongoDev'
  }    
};

production.js

module.exports = {
  connections : {
    mongoLive: {
      adapter: 'sails-mongo',
      host: 'host.mongolab.com',
      port: 31681,
      user: 'user',
      password: 'password',
      database: 'database'
    } 
  },
  models: {
     connection: 'mongoLive'
  },
  port: 3000,
};

这篇关于在Sails.js中处理数据库环境配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆