将sequelize查询转换为knex

时间:2017-12-09 23:08:31

标签: sql postgresql sequelize.js knex.js junction-table

我正在尝试了解如何使用联结表进行多对多关系的联接。我用sequelize设置它,但出于我自己的原因,我更喜欢knex。我试图将查询转换为knex以更好地了解如何使用联结表进行这些联接。

基本上我有membersevents与联结表members_events。我希望我的结果看起来像:

[{
   event_id,
   title,
   description
   members: [
      {
        member_id,
        etc
      }...
   ]
}...]

我使用sequelize获得了正确的格式。工作sequelize查询生成的代码就是这个。

  SELECT "event".*, "members"."id"
    AS "members.id", "members"."name"
    AS "members.name", "members->members_events"."createdAt"
    AS "members.members_events.createdAt", "members->members_events"."updatedAt"
    AS "members.members_events.updatedAt", "members->members_events"."memberId"
    AS "members.members_events.memberId", "members->members_events"."eventId"
    AS "members.members_events.eventId"
  FROM (SELECT "event"."id", "event"."title" FROM "events" AS "event" LIMIT 15)
    AS "event"
  LEFT OUTER JOIN ( "members_events" AS "members->members_events"
    INNER JOIN "members" AS "members"
    ON "members"."id" = "members->members_events"."memberId")
    ON "event"."id" = "members->members_events"."eventId";

有人可以帮我将其转换为knex查询,如果可能的话,不是knex.raw查询。感谢。

1 个答案:

答案 0 :(得分:2)

我决定使用bookshelfjs而不是sequelize。看起来更像是我在一个orm中寻找的东西。