Sails connect-redis adapter - node.js

I want to use redis commands for ZSET elements. In sails, I enabled the connect-redis adapter as per the docs. Now how do I access the redis commands with this redis client which is already created(I guess) when sails is lifted?
Also do I need to create a global variable for it?
/* config/session.js */
adapter: 'connect-redis',
host: 'localhost',
port: 6379,
db: 0,
// pass: <redis auth password>,
prefix: 'sess:',
Thanks in advance.

I think it should be available as sails.config.session.store.client, and if you have globals enabled, it would work in any place in your code.

Related

How to create a pool object in javascript that can connect to a database backend using SSL?

I followed a guide on how to create a Node JS backend on PostgreSQL and a ReactJS frontend, which is working well, see here for more.
The js pool object of the quide looks as follows:
const Pool = require('pg').Pool
const pool = new Pool({
user: 'my_user',
host: 'localhost',
database: 'my_database',
password: 'postgres',
port: 5432,
});
I tried doing the same with a PostgreSQL Webserver using SSL encryption. This question is not tagged postgresql since the pool looks the same for mysql and other databases. It is also not tagged Linux since it should work the same on other OS.
How to create a pool object from the Pool class in javascript that can connect to a PostgreSQL backend using SSL?
You need to save the certificate, call it server-certificate.pem, in a place with a static path starting with / (~ for your user's home directory does not work). Therefore, you need to move it to a static place.
I copied the file to a newly created directory certificates, but that is of course up to you. You will need super user rights for this.
/etc/certificates/server-certificate.pem
You need to load fs and use it in the SSL part, see How to establish a secure connection (SSL) from a Node.js API to an AWS RDS.
const Pool = require('pg').Pool
const fs = require('fs');
const pool = new Pool({
user: 'my_user',
host: 'my_host',
database: 'my_database',
password: 'my_password',
port: 22222,
ssl: {
ca: fs
.readFileSync("/etc/certificates/server-certificate.pem")
.toString()
}
});

Use multiple datastore connections with Sequelize

I've defined 2 connections in the /config/connections.js file:
monolithMysql: {
user: 'store_app',
database: 'store',
dialect: 'mysql',
options: {
dialect: 'mysql',
host: 'dockerhost',
port: 3306,
logging: console.log
}
},
postgres: {
user: 'user_app',
database: 'user_authentication',
dialect: 'postgres',
options: {
dialect: 'postgres',
host: 'dockerhost',
port: 8201,
logging: console.log
}
}
and in the different models I've put the property connection, so that they're distinguished, as follows:
module.exports = {
options: {
connection: 'monolithMysql', // or 'postgres' in other models.
...
In /config/models.js I set as default connection the monolithMysql one. But that should be overridden by the connection property, if specified, in the models. If I comment out or do not specify the connection property in /config/models.js then Sequelize hook fails to load.
Nevertheless, when trying to query models that have postgres as connection, it still queries them in the MySQL DB, and fails... If I set postgres as default connection, then it will always query in that DB, no matter what the local connection property of the different models says.
Any suggestions how to setup 2 connections at the same time?
Update: found out that it initializes only 1 instance of Sequelize - an instance with the default connection, specified in /config/models.js
Eventually, I ended up writing an upgrade to sails-hook-sequelize to support multiple database connection instances simultaneously.
The pull-request hasn't been reviewed at this point (13 April '16), but it has passed all tests and CI checks, so no worries. (the owner of the repo seems to be neglecting PR reviews in the last couple of months).
You can find the pull-request here.
In order to require the module, along with the multiple db connections upgrade, in your package.json file, just write:
"sails-hook-sequelize": "git#github.com:galioy/sails-hook-sequelize.git#f0c0c5d72ee97ac5504e6f3a0825e37c3587909a"
The configuration is the basic one, as you would do it normally for Sequelize:
add connections configurations to /config/connections.js (see OP)
for each model that you want to use secondary connection - just add
the name of the connection to options.connection property in the model itself (see
OP).
For detailed explanation, see the main comment of the pull-request.

Restart MongoDB Connecton on crash - NodeJS

I have server implemented using nodejs. According to this post, I have one single connection (using native MongoDB) in my index.js file which connects to the MongoDB on server start.
I am using forever, so each time the server crashes this module restarts it.
My question is: What will happen if my mongo connection will crash? How can I handle this situation? How can I detect the crash programmatically? Should I restart it, if yes, how?
You can set auto-connect parameter as true.
app.use(session({
store: new MongoStore({
// Basic usage
host: 'localhost', // Default, optional
port: 27017, // Default, optional
db: 'test-app', // Required
// Basic authentication (optional)
username: 'user12345',
password: 'foobar',
// Advanced options (optional)
autoReconnect: true, // Default
})
}));

change database to mongo db on sails.js

I am a newbie in sails.js framework(and for node.js) and I have created myself an API from the address bar using sails.js features. my question is how do I transfer this data to mongodb so I can see it visually.
I have tried to follow this guide: https://www.npmjs.org/package/sails-mongo
but its for 0.9 sails version(current version is 0.10) so that a lot of files that I needed to change has been modified and gone/renamed. I couldn't find an updated tutorial as well so if anyone can please write down how can I implement mongoDB to my sails project that would be wonderful.
In config/connections.js you need to configure your mongoDB connection. Then go to config/models.js, uncomment the connection property and set the name of your mongoDB connection.
In config/connection.js, You can configure your MongoDb connection like this.
module.exports.connections = {
MongoConnection: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
// user: 'username',
// password: 'password',
database: 'DB_Name'
}
};
You need to install sails-mongo package using npm in your working folder.
Then in config/models.js, Add the name of your mongoDb connection like this:
module.exports.models = {
connection: 'MongoConnection',
migrate: 'alter'
};
Thats it, you are ready to go.

NodeJS connect-redis with nodejitsu URI

I create a Redis db on nodejitsu and got a full URI such as:
nodejitsu:nodejitsudb11158161232.redis.irstack.com:f327c9sj29sj26e80b8e975fbebb4#nodejitsudb3058169sj32.redis.irstack.com
Connect-redis wants a host, port, etc... Anyone know of an easy way I can just pass it this string? Didn't seem to work when setting the host to that - mongodb seems to work fine with this schema.
Use url.parse http://nodejs.org/docs/latest/api/url.html to parse that url into its parts such as host, port, etc....
So diving in the source it looks like it's unable to parse a dsn string. url.parse doesn't return the necessary info so it has to be broken down further.
The full dsn has the redis:// protocol as well - it must be there for this to work. Because my local redis dsn string doesn't have a password/user there is a check for that in case it's missing:
var redisUrl = 'redis://nodejitsu:nodejitsudb3051531232.redis.irstack.com:fd470c971946e8der75fbebb4#nodejitsudb305813frt232.redis.irstack.com:6379';
var redis = url.parse(redisUrl);
app.use(express.session({
secret: settings.sessionSecret,
store: new RedisStore({host: redis.hostname, port: redis.port, pass: redis.auth ? redis.auth.substring(redis.auth.indexOf(':') + 1): null})
}));

Resources