Sequelize - 范围包括

时间:2016-04-04 14:59:06

标签: node.js express sequelize.js

我在我的Node.js / Express应用程序中使用了Sequelize,其中包含范围概念(http://docs.sequelizejs.com/en/latest/docs/scopes/)。

我想在模型上有两个范围。这些范围中的每一个都基于另一个表的连接(请参阅两个范围inLocation& withExpertises):

const Agency = sequelize.define("agency", {
    id:            { type: DataTypes.INTEGER, primaryKey: true },
    name:          DataTypes.STRING,
    address:       DataTypes.STRING,
    size:          DataTypes.INTEGER,
    description:   DataTypes.TEXT,
    logoFileName:  { type: DataTypes.STRING, field: "logo_file_name" },
    externalScore: { type: DataTypes.FLOAT, field: "external_score" }
  }, {
    classMethods: {
      associate(models) {
        Agency.belongsToMany(models.location, {
          through:    "locationsAgency",
          foreignKey: "location_id"
        });
        Agency.hasOne(models.locationsAgency);
        Agency.hasMany(models.service);
      }
    },
    scopes: {
      inLocation(locationId) {
        return {
          include: [{
            model: locationsAgency, where: { locationId: locationId }
          }]
        };
      },
      withExpertises(expertiseIds) {
        return {
          include: [{
            model: service, where: { expertiseId: expertiseIds }
          }]
        };
      },
      orderByRelevance: {
        order: '"externalScore" DESC'
      }
    }
  });

当我正在进行以下呼叫时:

models.agency.scope({ method: ["inLocation", 9260] },
                    { method: ["withExpertises", 79] },
                    "orderByRelevance")
             .findAll({ limit: 10 })

我生成了这个SQL:

SELECT "agency".*, 
"services"."id" AS "services.id", 
"services"."expertise_id" AS "services.expertiseId"
FROM (
    SELECT "agency".*, 
    "locationsAgency"."id" AS "locationsAgency.id",
    "locationsAgency"."location_id" AS "locationsAgency.locationId",
    "locationsAgency"."agency_id" AS "locationsAgency.agencyId" 
    FROM "agencies" AS "agency" 
    INNER JOIN "locations_agencies" AS "locationsAgency" ON "agency"."id" = "locationsAgency"."agency_id" 
    AND "locationsAgency"."location_id" = 9260 
    WHERE ( 
        SELECT "agency_id" 
        FROM "services" AS "service" 
        WHERE ("service"."agency_id" = "agency"."id" AND "service"."expertise_id" = 79) 
        LIMIT 1 
    ) 
    IS NOT NULL LIMIT 10
) AS "agency" 
INNER JOIN "services" AS "services" ON "agency"."id" = "services"."agency_id" 
AND "services"."expertise_id" = 79 
ORDER BY "externalScore" DESC

正如您所看到的,范围嵌套在FROM子句中,这给了我一个糟糕的SQL,然后limit语句不是一个好地方。

我希望有以下SQL查询:

SELECT "agency".*, 
"services"."id" AS "services.id", 
"services"."expertise_id" AS "services.expertiseId", 
"services"."created_at" AS "services.created_at", 
"services"."updated_at" AS "services.updated_at",
"locationsAgency"."id" AS "locationsAgency.id", 
"locationsAgency"."location_id" AS "locationsAgency.locationId", 
"locationsAgency"."agency_id" AS "locationsAgency.agencyId"
FROM "agencies" AS "agency"
INNER JOIN "locations_agencies" AS "locationsAgency" ON "agency"."id" = "locationsAgency"."agency_id" 
INNER JOIN "services" AS "services" ON "agency"."id" = "services"."agency_id" 
AND "locationsAgency"."location_id" = 9260 
AND "services"."expertise_id" = 79 
ORDER BY "external_score" DESC
LIMIT 10

有关使用范围如何使用此SQL的任何想法吗?

谢谢!

0 个答案:

没有答案