Brandon Keith Biggs Brandon Keith Biggs - 6 days ago 6
Node.js Question

Adding info to log into a database in FeathersJS

I can't seem to find how to log into a database in my FeathersJS app.
I would prefer to specify database info and login info in the service, but I just need it to work.
In myApp/config/default.json there is the following line:

"postgres": "postgres://postgress:@localhost:5432/feathers_db",


at:

http://docs.sequelizejs.com/en/latest/docs/getting-started/

It says that a string like the above should be:

"postgres": "postgres://username:user_password@localhost:5432/feathers_db",


But this does not work. It is also not very Feathers-like as now I am locked into one postgress db for all my postgress transactions.

In services/index.js there is the following line:

const sequelize = new Sequelize(app.get('postgres'), {
dialect: 'postgres',
logging: false
});


I could customize the above line to be what Sequelize says to do in their guide and have username and password as an argument, but then why is the template not already laid out like this?
There is also this line:

app.set('sequelize', sequelize);


If I have several postgress databases what do I do? DO I make new Sequelize objects and do something like:

app.set('sequelize_db1', sequelize_db1);
app.set('sequelize_db2', sequelize_db2);


Or do I specify db info, including user info in the service's model?
What does the logging in process for Postgress look like if one is using the generic db language rather than sequelize?

Answer

So in a word "yes". Everything I asked in my question was yes. I can connect to the db like shown in the sequelize documentation. I can also configure the config.json file to have a "postgres" configuration with my user and db name. I could also place the full path in the services/index.js file when creating the new sequelize object. The best way to check that there is a connection is to have the following code after creating the new sequelize object:

new_sequelize_object
    .authenticate()
    .then(function(err) {
        console.log('Connection to the DB has been established successfully.');
    })
    .catch(function (err) {
        console.log('Unable to connect to the database:', err);
    });

(taken from: http://docs.sequelizejs.com/en/latest/docs/getting-started/)

one can also define several sequelize objects and set them in the app. Then when defining the model in the specific service's index.js file, place the new bound name in the app.get('new_sequelize_object').

Here is the services/index.js file with two databases defined:

'use strict';
const service1 = require('./service1');
const authentication = require('./authentication');
const user = require('./user');
const Sequelize = require('sequelize');
module.exports = function() {
  const app = this;

  const sequelize = new Sequelize('feathers_db1', 'u1', 'upw', {
host: 'localhost',
port: 5432,
    dialect: 'postgres',
  logging: false
  });

  const sequelize2 = new Sequelize('postgres://u1:upw@localhost:5432/feathers_db2', {
    dialect: 'postgres',
  logging: false
  });

  app.set('sequelize', sequelize);
  app.set('sequelize2', sequelize2);

sequelize
  .authenticate()
  .then(function(err) {
    console.log('Connection to sequelize has been established successfully.');
  })
  .catch(function (err) {
    console.log('Unable to connect to the database:', err);
  });

sequelize2
  .authenticate()
  .then(function(err) {
    console.log('Connection has been established to sequelize2 successfully.');
  })
  .catch(function (err) {
    console.log('Unable to connect to the database:', err);
  });


  app.configure(authentication);
  app.configure(user);
  app.configure(service1);
};

And here is the service1/index.js file that uses service sequelize2: 'use strict';

const service = require('feathers-sequelize');
const service1 = require('./service1-model');
const hooks = require('./hooks');

module.exports = function(){
  const app = this;

  const options = {
    //Here is where one sets the name of the differeng sequelize objects 
    Model: service1(app.get('sequelize2')),
    paginate: {
      default: 5,
      max: 25
    }
  };

  // Initialize our service with any options it requires
  app.use('/service1', service(options));

  // Get our initialize service to that we can bind hooks
  const service1Service = app.service('/service1');


  // Set up our before hooks
  service1Service.before(hooks.before);
  // Set up our after hooks
  service1Service.after(hooks.after);
};