小编Num*_*ter的帖子

Node.js Sequelize UUID主键+ Postgres

我尝试通过使用sequelize创建数据库模型,但是我面临模型主键的问题。

设置

我在Docker容器中使用Postgres(v10),并为模型隔离(Node.js v10.1.0)和为请求处理的GraphQL(0.13.2)+ GraphQL-Sequalize(8.1.0)。

问题

通过sequelize-cli创建模型后,我手动尝试用uuid替换id列。这是我正在使用的模型迁移。

'use strict';
const DataTypes = require('sequelize').DataTypes;

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Currencies', {
      uuid: {
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false
      },
      name: {
        type: Sequelize.STRING
      },
      ticker: {
        type: Sequelize.STRING
      },
      alt_tickers: {
        type: Sequelize.ARRAY(Sequelize.STRING)
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Currencies');
  }
};
Run Code Online (Sandbox Code Playgroud)

模型:

'use strict'; …
Run Code Online (Sandbox Code Playgroud)

postgresql orm node.js sequelize.js graphql

2
推荐指数
2
解决办法
4059
查看次数

在 Node 中使用带有 Postgres 的 Sequelize 请求查询日期范围

我想要做的是通过在 Node.js 中使用 Sequelize ORM 来获取两个日期之间的行。我正在使用 PostgreSQL。问题是 Sequelize 错误地解释了我提出的请求。

这是我用来提出请求的代码

const dbresp = await Table.findAll({
  attributes: [...],
  where: {
    ...
    createdAt: {
       $between: [new Date(Date(startDate)), new Date(Date(endDate))],
       // same effect
       // $lte: new Date(startDate),
       // $gte: new Date(endDate),
    },
  },
  logging: console.log,
  raw: true,
  order: [['createdAt', 'ASC']],
  // limit: count,
});
Run Code Online (Sandbox Code Playgroud)

通过记录原始 SQL 请求,很明显请求是不正确的

SELECT ...
FROM "table" AS "table"
WHERE "table"."createdAt" = '2019-02-05 21:00:00.000 +00:00'
      "table"."createdAt" = '2019-02-05 21:00:00.000 +00:00'
ORDER BY "table"."createdAt" ASC;
Run Code Online (Sandbox Code Playgroud)

提出此类请求的正确方法是什么?我应该使用原始查询吗?

我已经用谷歌搜索了这个问题,但 StackOverflow 和 GitHub …

postgresql orm node.js sequelize.js

2
推荐指数
1
解决办法
7153
查看次数

标签 统计

node.js ×2

orm ×2

postgresql ×2

sequelize.js ×2

graphql ×1