SailsJS Connections

sailsjs graphql
sails js modal
sails-mysql
sails-disk
sails-postgresql
sails create model
sails js elasticsearch
sailsjs primary key

I built my code using SailsJS, and configured the connection to work with PostgreSQL. But on username and password fields, I want to retrieve this information from another function. Anyone knows how I can do this.

Example:

pgDatabase: {
     adapter: 'sails-postgresql',
     host: 'server',
     port: port,
     user: getUsername(function(p) {
       return(p);
     }), // optional
     password: getPassword(function(c) {
       return(c);
     }), // optional
     database: 'db', //optional
     pool: false
   }

You can define username and password in your config/env file different for development and production and in connections you can access it as shown in below line of code

username: (process.env.NODE_ENV == "production") ? "productionUser" : "otherUserName",
password: (process.env.NODE_ENV == "production") ? "productionPassword" : "otherPassword";

NODE_ENV can be set while lifting sails

NODE_ENV=production sails lift
NODE_ENV=development sails lift

connections.js vs. datastore.js ?, Hello! Now in the sails.js 1.0.1 there are new changes with the connections.js file to datastore.js how to understand this now.? i have read now  This is called a connection URL (or "connection string"), and it's just another, more concise way, to tell Sails and Waterline about your datastore configuration. One major benefit to this style of configuration is that the format of a connection URL is the same across various types of databases.

You can define username, password in your api/services methods. Depending upon your need either you can create multiple services or single service which will return required values.

module.exports = () => {
  return () => {

    return 'localhost';
    //return Promise.resolve(convertedObject);
  }

}

As per your requirement, Sailsjs bootstrap file is the best place to set database credentials.

sails.config.bootstrap is a customizable, asynchronous bootstrap function that runs before your Sails app gets lifted (i.e. starts up).

The code in bootstrap :

sails.config.connections.developmentServer.host = setDBCredentials()();

In the above "developmentServer" is the for MySQL adapter key.

developmentServer: {
    adapter: 'sails-mysql',
    host: '',
    user: 'root', //optional
    password: 'password', //optional
    database: 'bb' //optional
  }

Lift the app , it will set the host into your adapter

SailsJS Connections, You can define username and password in your config/env file different for development and production and in connections you can access it  As per your requirement, Sailsjs bootstrap file is the best place to set database credentials. sails.config.bootstrap is a customizable, asynchronous bootstrap function that runs before your Sails app gets lifted (i.e. starts up). The code in bootstrap : sails.config.connections.developmentServer.host = setDBCredentials()();

mention it on Node_env file

username: (process.env.NODE_ENV == "production") ? "productionUser" : "otherUserName",
password: (process.env.NODE_ENV == "production") ? "productionPassword" : "otherPassword";

or create a js file on the app root path and export the config file, now on connection.js simply import the file and use it

export config
Ex: 
dbconfig.js
  const config =  (username, password) => {
           // Do your staff hear
           return username
           return password
}
export default config

import config
Ex:
connection.js
const dbConfig = require(../dbconfig.js)
const username = dbConfig.config , password = dbConfig.config

how to use multiple database connections in sails.js?, Yes, you can do this with sails. If you are using a version < v1. First thing to do is add a connection to each db in the connections.js file located  Configure the connection. So, for MySQL, you need to install sails-mysql and tell Sails to use it. Here are the steps. Install the module using the following command. npm install --save sails-mysql Once everything has been installed, go to config/connections.js and configure the MySQL settings, put in a proper username and password.

sails.js, To do this first locate config folder in your root. Then open connections.js. Locate // someMysqlServer: { // adapter: 'sails-mysql', // host:  The connection URL for the Redis instance to connect to. This may include one or more of the other settings below, e.g. redis://: [email protected] :1234/5 would indicate a host of myredishost.com , a port of 1234 , a pass of mypass and a db of 5 .

Sails.js Essentials, Let's create a new Sails.js project and develop our first Sails.js API. last chapter​, we need to edit config/connections.js and config/ models.js to connect Sails.js  All supported adapters can be configured in roughly the same way: by passing in a Sails/Waterline adapter (adapter), as well as a connection URL (url). For more information on configuring datastores, see sails.config.datastores. Having trouble connecting? Be sure to check your connection URL for typos.

How to Connect Sails.js To Multiple Databases, sails.config.connections. What is this? Adapters are the middle man between your Sails app and some kind of storage (typically a database). Global connections  Sails.js makes it easy to build custom, enterprise-grade Node.js apps. It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development.

Comments
  • This is ok, but not what I need. I'm needing to get from a funcion, not from environment. Because I have another system to manage the user and password