【问题标题】:Using Sequelize with Redshift将 Sequelize 与 Redshift 一起使用
【发布时间】:2015-11-09 07:44:31
【问题描述】:

Sequelize 可以和Redshift 一起使用吗?如果没有,有什么替代方案?我需要一个具有内置事务支持的 Node.js 的 ORM,因此 Sails.js 不是一个选项。我也查看了Bookshelf,但也找不到任何对 Redshift 的支持。

【问题讨论】:

    标签: node.js sails.js sequelize.js amazon-redshift bookshelf.js


    【解决方案1】:

    Redshift 基于 postgres 8.0.2 (http://docs.aws.amazon.com/redshift/latest/dg/c_redshift-and-postgres-sql.html),因此 postgres 和 sequelize 都应该能够连接到它。

    我没有任何个人经验,但 redshift 文档建议您可以使用常规 JDBC / ODBC 驱动程序连接到它,所以如果节点驱动程序不起作用,我会感到惊讶

    【讨论】:

    • 似乎 node-pg(由 sequelize 和 bookshelf 使用)连接到 redshift 就好了github.com/brianc/node-postgres/issues/686
    • 早先的question 表明过去时区存在一些问题,但现在可能已经修复了。
    • 关于时区的问题已经解决了。我不知道有任何问题,官方sequelize bug tracker 中没有报告任何问题(完全披露,我是sequelize 开发者)
    【解决方案2】:

    Sequelize 与 Redshift 不兼容。虽然 Redshift 是在 Postgres 之上编写的,但它是一个柱状数据库,并且主要的核心功能都被重写了。

    尝试连接时出现错误“不支持设置时区”

    以下线程显示了一些人覆盖时区错误但随后面临其他问题。 'Using Node 'pg' library to connect to Amazon Redshift

    如果 Redshift 是强制性的,您可以使用 node-jdbc 包连接 Redshift https://github.com/CraZySacX/node-jdbc

    如果 ORM 是强制性的,您应该尝试将数据存储移动到纯 Postgres

    【讨论】:

      【解决方案3】:

      我已经能够使用这些选项让 Sequelize 至少连接到 Redshift(并进行简单的 SELECT 查询):

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

      【讨论】:

      • 对 pool:false 的支持在 v4.0 中被移除。删除该行,它适用于版本 4.x
      【解决方案4】:

      目前我认为Sequelize 不是连接到 Redshift 的最佳选择。我建议你直接使用node-postgres 模块。

      话虽如此,您可能已经有一个使用Sequelize 的项目并希望在这些情况下重用它,您可以执行以下操作。

      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);
      

      这将生成以下弃用警告,因为 Redshift 基于 postgres 8.x,因此请考虑直接使用 pg 模块。

      (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 
      

      【讨论】:

      • 连接成功了吗?我遇到了同样的问题,但我得到了SequelizeHostNotFoundError: getaddrinfo ENOTFOUND 我尝试使用端点和 jdbc,但什么也没有
      • 我连接 Redshift 没有任何问题,我只是不喜欢重要模块中的弃用警告。至于您的情况,该错误非常具有描述性,因此请检查主机/端口是否可访问以及连接配置参数。
      猜你喜欢
      • 2017-08-23
      • 1970-01-01
      • 2017-02-21
      • 1970-01-01
      • 2020-04-17
      • 1970-01-01
      • 2019-11-07
      • 2019-12-27
      • 1970-01-01
      相关资源
      最近更新 更多