Use multiple datastore connections with Sequelize - node.js

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.

Related

TypeORM connection without explicitly specifying database

I wonder if it is somehow possible using TypeOrm to connect to a database server without having to specify a schema.
With the normal mysql module in Node it would work like this:
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password:"xxx"
});
However, a database must be specified in the connection options in TypeOrm, if this is an empty string or does not exist, an error occurs.
So I don't see any possibility to connect to the server with TypeOrm and create a new schema with this connection.
Do I have to use the normal mysql module or is there a possibility with TypeORM that I am not aware of?

Sequelize unable to connect to SQL Server through network

My setup is the following:
I have a Virtual Machine running all of my Database processes, let's call it DB-VM.
I'm currently developing at my own workstation (completely detached from DB-VM, except that we are under the same network.
I've created a valid connection string, validated by another database connection service throughout IIS and through a Data Link Properties file (.udl) and the connection.
This connection is described by the connection string as:
Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=DB-VM\MY_DATABASE.
I tried to insert it into my Sequelize configuration as following:
const sequelize = new Sequelize({
dialect: 'mssql',
dialectModulePath: 'sequelize-msnodesqlv8',
dialectOptions: {
connectionString: 'Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Data Source=DB-VM\MY_DATABASE',
trustedConnection: true,
}
});
And then proceeded to try and authenticate through:
sequelize.authenticate().then(() => {
console.log('Connection stablished successfully!');
}).catch(err => {
console.log(err);
});
And this the error is as follows:
Notice: The database uses dynamic ports, therefore I can't specify the port through the port property.
Notice 2: The Named Pipes are disabled on my database settings, and I'm not sure if I will be able to enabled it.
Notice 3: The database is already setup to allow remote connections (it is currently used through a Webpage and works fine!
According to this line the sequelize-msnodesqlv8 library expects "Driver" to be a part of the connection string, otherwise it tries to guess. Besides that, all the examples of connection strings here1 and here2 are using either Server=myServerName\theInstanceName or just Server=myServerName. Instead of the Data Source=....
So step 1 is to fix you connection string. You could try one of the examples like:
dialectOptions: {
connectionString: 'Driver={SQL Server Native Client 10.0};Server=DB-VM;Database=MY_DATABASE;Trusted_Connection=yes;'
},
After that if you get a new error, please update the question.

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

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.

Resources