【问题标题】:How to set database connection string in Waterline ORM如何在 Waterline ORM 中设置数据库连接字符串
【发布时间】:2014-03-18 22:57:15
【问题描述】:

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

【问题讨论】:

  • 提供的答案有什么问题吗?

标签: javascript node.js orm sails.js waterline


【解决方案1】:

Waterline 在其当前状态下是Sails 框架的子项目。

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

当单独使用 Waterline 时,您必须自己处理这部分:您希望引导并将您的配置显式传递到 waterline。你必须一步一步做的事情:

  1. 需要水线和正确的水线适配器,在您的情况下:sails-postgresql
  2. 指定adapters配置
  3. 指定connections 配置,这将采用有问题的配置
  4. 定义并加载您的collections
  5. 初始化水线

一个如何做这一切的例子,来自这些 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
});

【讨论】:

  • 在回调中导出一些东西有点不正统。如果您想 module.export 模型并在不同的文件中要求它们怎么办?
  • 我认为你必须将postgreDev... 包裹在connections 中,否则你会得到一个异常
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-07
  • 2017-02-27
  • 1970-01-01
  • 2019-09-19
  • 1970-01-01
  • 2015-06-29
相关资源
最近更新 更多