change database to mongo db on sails.js - node.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.

Related

Configuring knex database connection uisng an access key

Well I recently started using knex for my database connectivity for my nodejs project. Here's the following code:
var knex = require('knex')({
client: 'mysql',
connection: {
'host':'localhost',
'port':'3306',
'user':'root',
'password':'',
'database':'db_sample'
}
});
So what I need is, I want to add an access key as well in my Database configuration. I just wanted to know if its possible to do it. If yes, please do tell me how. Thank you

Sails connect-redis adapter

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.

Possible: Sailsjs with both NoSQL and SQL?

I have an idea to use both NoSQL (Google datastore) and SQL (google SQL) together in my nodejs project hosted on Google cloud platform. The reason for this is because I want to keep the statistics, massive amount of data, away from the mySQL db. It will create to many writes, a lot cheaper and faster to use noSQL for this purpose.
I cannot use noSQL for the entire site, the project has already been created with mySQL.
Is this possible? If so please share any guidelines to do this
using sails.js (waterline) you can define as many connections as you want. And then in the model you can select the connection that you want to use.
It is very cool, 2 models, using 2 different connections (mysql - gdatastore) can even have relations between one and the other one.
basically in your model you have to set the connection key { connection: 'mysqldb'}
here you can find the documentation: https://github.com/balderdashy/waterline-docs/blob/master/models/configuration.md
#model
module.exports.connections = {
localMysql: {
adapter: 'sails-mysql',
user: 'root',
host: 'localhost',
database: 'someDbase'
},
remoteMysql: {
adapter: 'sails-mysql',
user: 'remoteUser',
password: 'remotePassword',
host: 'http://remote-mysql-host.com',
database: 'remoteDbase'
},
localMongo:{
adapter: 'sails-mongo',
......
}
};

Using Sequelize with Redshift

Is it possible to use Sequelize with Redshift? If not, what are the alternatives? I need an ORM for Node.js with built-in support for transactions, hence Sails.js is not an option. I've also looked at Bookshelf, but could not find any support for Redshift either.
I've been able to get Sequelize to at least connect to Redshift (and make a simple SELECT query) with these options:
var Sequelize = require('sequelize');
Sequelize.HSTORE.types.postgres.oids.push('dummy'); // avoid auto-detection and typarray/pg_type error
module.exports = new Sequelize(process.env.REDSHIFT_DATABASE, process.env.REDSHIFT_USER, process.env.REDSHIFT_PASSWORD, {
host: process.env.REDSHIFT_HOST,
port: process.env.REDSHIFT_PORT,
dialect: 'postgres',
pool: false,
keepDefaultTimezone: true, // avoid SET TIMEZONE
databaseVersion: '8.0.2' // avoid SHOW SERVER_VERSION
});
Sequelize is not compatible with Redshift. Though Redshift is written on top of Postgres, it is a columnar DB and major core functions are rewritten.
While trying to connect to it gives an error 'Set Time Zone is not supported'
The following thread shows a few people overriding the time zone error but facing other issues subsequently. 'Using Node 'pg' library to connect to Amazon Redshift
if Redshift is the mandatory you may use the node-jdbc package to connect with Redshift
https://github.com/CraZySacX/node-jdbc
of if ORM is mandatory, you should may try moving your data store to pure Postgres
Redshift is based on top of postgres 8.0.2 (http://docs.aws.amazon.com/redshift/latest/dg/c_redshift-and-postgres-sql.html), so both postgres and sequelize should be able to connect to it.
I don't have any personal experience with it, but the redshift documentation suggests that you can connect to it using regular JDBC / ODBC drivers, so I would be surprised if the node drivers don't work
At this point in time I dont think Sequelize is the best option to connect to Redshift. I recommend that you use node-postgres module directly.
That being said it might be the case that you might already have a project that uses Sequelize and want to reuse it in those cases you can do the following.
const Sequelize = require('sequelize');
// sequelize config options documented at
// https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor
const config = {
dialect: 'postgres',
host: 'localhost',
port: 5439,
database: '',
username: '',
password: '',
dialectOptions: {
// either set to ssl to true or use the config options documented at
// https://nodejs.org/api/tls.html#tls_new_tls_tlssocket_socket_options
ssl: true
},
standardConformingStrings: false,
clientMinMessages: false
};
const redshift = new Sequelize(config);
const queryOpts = {type: sequelize.QueryTypes.SELECT, raw:true};
redshift.query('SELECT 1', queryOpts)
.then(console.log)
.catch(console.log);
This will generate the following deprecation warning as Redshift is based on postgres 8.x so consider using pg module directly.
(node:5177) [SEQUELIZE0006] DeprecationWarning: This database engine version is not supported, please update your database server. More information https://github.com/sequelize/sequelize/blob/master/ENGINE.md

Unable to get the messages from cloud AMQP queue

Hi i am trying to use rabbitmq in my sails app. when I try to post data in my model after giving the connections adapter I can see the data received in my rabbit queue but the database has not created.
I'm using mongo database for my models and sails-rabbitmq as my node_module
I referred this link
npm install sails-rabbitmq
The connection adapter is -
rabbitCluster: {
adapter: 'sails-rabbitmq',
//my amqp rabbitmq installation url
url: 'amqp://localhost',
},
MongoDB: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
database: 'myTest'
}
inside my model specified the connection as follow
connection : ['rabbitCluster','MongoDB']
//in my config/rabbitmq.js file
module.exports.rabbitmq = {
pkFormat: 'string'
};
as I understand we don't need to write create and update controller methods it should work with default CRUD operations provided by sails. but i get no response when i try to create a database entry.

Resources