Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set database connection string in Waterline ORM

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 ?

like image 621
CSharpBeginner Avatar asked Feb 18 '14 22:02

CSharpBeginner


1 Answers

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

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.

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:

  1. Require Waterline and the correct Waterline adapter, in your case: sails-postgresql
  2. Specify adapters config
  3. Specify connections config, this will take the config in question
  4. Define and load your collections
  5. Initialize Waterline

An example how to do all this, derived from these Waterline examples: 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
});
like image 147
marionebl Avatar answered Oct 03 '22 07:10

marionebl