使用Sequelize计算详细记录的总和和计数

时间:2017-06-07 18:45:27

标签: javascript postgresql sequelize.js

使用 Sequelize 4.1.0 ,我想为此 PostgreSQL 9.6.3 查询生成相同的结果:

SELECT
  "user"._id,
  "user"."lastName",
  "user"."firstName",
  "user".email,
  COALESCE(purchase.purchases, 0::bigint) - COALESCE(attendance.attendances, 0::bigint) AS balance
FROM "Users" "user"
  LEFT OUTER JOIN
    (SELECT "Purchases"."UserId", SUM("Purchases".quantity) AS purchases
    FROM "Purchases"
    GROUP BY "Purchases"."UserId") purchase ON "user"._id = purchase."UserId"
  LEFT OUTER JOIN
    (SELECT "Attendances"."UserId", COUNT("Attendances"._id) AS attendances
    FROM "Attendances"
    GROUP BY "Attendances"."UserId") attendance ON "user"._id = attendance."UserId"
    WHERE "user"."firstName" ILIKE 'Smith%' OR "user"."lastName" ILIKE 'Smith%' OR "user"."email" ILIKE 'Smith%';

我的模型(来源:https://github.com/nstuyvesant/shy/blob/master/server/sqldb/index.js)是使用Sequelize 4.0.0定义的......

import config from '../config/environment'; // source: https://github.com/nstuyvesant/shy/blob/master/server/config/environment/index.js
import Sequelize from 'sequelize';

let db = {
  Sequelize,
  sequelize: new Sequelize(config.sequelize.uri, config.sequelize.options)
};

// Imported model definitions
db.User = db.sequelize.import('../api/user/user.model'); // source: https://github.com/nstuyvesant/shy/blob/master/server/api/user/user.model.js
db.Purchase = db.sequelize.import('../api/user/purchase.model'); // source: https://github.com/nstuyvesant/shy/blob/master/server/api/user/purchase.model.js
db.Attendance = db.sequelize.import('../api/user/attendance.model'); // source: https://github.com/nstuyvesant/shy/blob/master/server/api/user/attendance.model.js

// Associations
db.Purchase.belongsTo(db.User);
db.Attendance.belongsTo(db.User);
db.User.hasMany(db.Purchase);
db.User.hasMany(db.Attendance);

module.exports = db;

在我的用户API控制器(来源:https://github.com/nstuyvesant/shy/blob/master/server/api/user/user.controller.js)中,我使用

回复GET /用户
import { User, Attendance, Purchase } from '../../sqldb';
import sequelize from 'sequelize';

export function index(req, res) {
  let startsWith = `${req.query.filter}%`;
  return User.findAll({
    where: {
      $or: [
        { firstName: { $iLike: startsWith } },
        { lastName: { $iLike: startsWith } },
        { email: { $iLike: startsWith } }
      ]
    },
    attributes: ['_id', 'lastName', 'firstName', 'email', [sequelize.literal('COALESCE(SUM(purchase.quantity), 0) - COALESCE(COUNT(attendance.id), 0)'), 'balance']],
    include: [{model: Purchase, attributes: ['quantity']}, {model: Attendance, attributes: ['_id']}],
    group: ['User._id', 'lastName', 'firstName', 'email', 'Purchases._id', 'Attendances._id']
  })
    .then(users => res.status(200).json(users))
    .catch(handleError(res));
}

这是它产生的查询:

SELECT
  "User"."_id", "User"."lastName", "User"."firstName", "User"."email", COALESCE(SUM("Purchases"."quantity"), 0) - COALESCE(COUNT("Attendances"."_id"), 0) AS "balance", "Purchases"."_id" AS "Purchases._id", "Purchases"."quantity" AS "Purchases.quantity", "Attendances"."_id" AS "Attendances._id"
FROM "Users" AS "User"
  LEFT OUTER JOIN "Purchases" AS "Purchases" ON "User"."_id" = "Purchases"."UserId"
  LEFT OUTER JOIN "Attendances" AS "Attendances" ON "User"."_id" = "Attendances"."UserId"
WHERE ("User"."firstName" ILIKE 'Stuyvesant%' OR "User"."lastName" ILIKE 'Stuyvesant%' OR "User"."email" ILIKE 'Stuyvesant%')
GROUP BY "User"."_id", "lastName", "firstName", "email", "Purchases"."_id", "Attendances"."_id";

关于我可能做错什么的任何想法?

1 个答案:

答案 0 :(得分:0)

有一种方法可以生成具有差异构造但具有相同结果的查询。假设已经定义了所有具有关联的模型,则查询将如下所示

User.findAll({
    attributes: ['id', 'lastName', 'firstName', 'email', [sequelize.literal('COALESCE(SUM(purchase.quantity), 0) - COALESCE(COUNT(attendance.id), 0)'), 'balance']],
    include: [{model: Purchase, attributes: 'quantity'}, {model: Attendance, attributes: 'id'}],
    group: ['id', 'lastName', 'firstName', 'email']
})