6
votes

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.

4

4 Answers

7
votes

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
});
2
votes

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

0
votes

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

0
votes

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