如何在Waterline ORM中设置数据库连接字符串

时间:2022-03-17 11:52:10

I just downloaded Waterline from npm. I got some folders, but cant find where can i set the host/user/password etc. to connect my postgress database. I watched all files in the waterline folder and nothing. Can anyone tell me where set it ?

我刚从npm下载了Waterline。我有一些文件夹,但无法找到我在哪里可以设置主机/用户/密码等连接我的postgress数据库。我看了waterline文件夹中的所有文件,什么都没有。谁能告诉我在哪里设置它?

1 个解决方案

#1


10  

Waterline is in its current state a sub project of the Sails framework.

Waterline目前处于Sails框架的子项目中。

What you are searching for is the conventional place to put your database configuration. When using Waterline as part of Sails this convention would be defined by the way Sails auto-requires configuration files into the global sails object.

您要搜索的是放置数据库配置的常规位置。当使用Waterline作为Sails的一部分时,这个约定将通过Sails自动要求配置文件到全局sails对象的方式来定义。

When using Waterline on its own you'll have to take care of this part for yourself: You want to bootstrap and pass your configuration explicitly into waterline. What you'll have to do step by step:

当你自己使用Waterline时,你必须自己处理这个部分:你想引导并将你的配置明确地传递到水线。您需要一步一步做的事情:

  1. Require Waterline and the correct Waterline adapter, in your case: sails-postgresql
  2. 在你的情况下需要Waterline和正确的Waterline适配器:sails-postgresql
  3. Specify adapters config
  4. 指定适配器配置
  5. Specify connections config, this will take the config in question
  6. 指定连接配置,这将采用有问题的配置
  7. Define and load your collections
  8. 定义并加载您的集合
  9. Initialize Waterline
  10. 初始化水线

An example how to do all this, derived from these Waterline examples: https://github.com/balderdashy/waterline/blob/master/example/

从这些Waterline示例中得到的所有这些的示例: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


10  

Waterline is in its current state a sub project of the Sails framework.

Waterline目前处于Sails框架的子项目中。

What you are searching for is the conventional place to put your database configuration. When using Waterline as part of Sails this convention would be defined by the way Sails auto-requires configuration files into the global sails object.

您要搜索的是放置数据库配置的常规位置。当使用Waterline作为Sails的一部分时,这个约定将通过Sails自动要求配置文件到全局sails对象的方式来定义。

When using Waterline on its own you'll have to take care of this part for yourself: You want to bootstrap and pass your configuration explicitly into waterline. What you'll have to do step by step:

当你自己使用Waterline时,你必须自己处理这个部分:你想引导并将你的配置明确地传递到水线。您需要一步一步做的事情:

  1. Require Waterline and the correct Waterline adapter, in your case: sails-postgresql
  2. 在你的情况下需要Waterline和正确的Waterline适配器:sails-postgresql
  3. Specify adapters config
  4. 指定适配器配置
  5. Specify connections config, this will take the config in question
  6. 指定连接配置,这将采用有问题的配置
  7. Define and load your collections
  8. 定义并加载您的集合
  9. Initialize Waterline
  10. 初始化水线

An example how to do all this, derived from these Waterline examples: https://github.com/balderdashy/waterline/blob/master/example/

从这些Waterline示例中得到的所有这些的示例: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
});