Запрос Knex с объединением вложенных таблиц - PullRequest
1 голос
/ 16 июня 2019

Я пытаюсь создать этот запрос с помощью Knex:

SELECT  "CustomerProduct"."id", 
        "CustomerProduct"."last_delivered" AS "lastDelivered", 
        "CustomerProduct"."price", 
        "CustomerProduct"."active", 
        "CustomerProduct"."customer_id" AS "customerId", 
        "CustomerProduct"."product_id" AS "productId", 
        "customer"."id" AS "customer.id", 
        "customer"."code" AS "customer.code",  
        "customer"."title" AS "customer.title", 
        "customer"."supplier_id" AS "customer.supplierId", 
        "customer->users"."id" AS "customer.users.id", 
        "customer->users"."name" AS "customer.users.name", 
        "customer->users"."phone" AS "customer.users.phone", 
        "customer->users->CustomerUser"."id" AS "customer.users.CustomerUser.id", 
        "customer->users->CustomerUser"."user_id" AS "customer.users.CustomerUser.userId", 
        "customer->users->CustomerUser"."customer_id" AS "customer.users.CustomerUser.customerId" 
FROM    "customer_products" AS "CustomerProduct" 
LEFT OUTER JOIN ( "customers" AS "customer" 
    INNER JOIN ( "customer_users" AS "customer->users->CustomerUser" 
        INNER JOIN "users" AS "customer->users" ON "customer->users"."id" = "customer->users->CustomerUser"."user_id") 
    ON "customer"."id" = "customer->users->CustomerUser"."customer_id" AND "customer->users"."id" = :userId ) 
ON "CustomerProduct"."customer_id" = "customer"."id" 
WHERE "CustomerProduct"."id" = :customerProductId;

Это то, что я до сих пор придумал:

const allowed = db.sequelize.knex.select([
  'CustomerProduct.id',
  'CustomerProduct.last_delivered AS lastDelivered',
  'CustomerProduct.price',
  'CustomerProduct.active',
  'CustomerProduct.customer_id AS customerId',
  'CustomerProduct.product_id AS productId',
  'customer.id AS customer.id',
  'customer.code AS customer.code',
  'customer.title AS customer.title',
  'customer.supplier_id AS customer.supplierId',
  'customer->users.id AS customer->users.id',
  'customer->users.name AS customer->users.name',
  'customer->users.phone AS customer->users.phone',
  'customer->users->CustomerUser.id AS customer.users.CustomerUser.id',
      'customer->users->CustomerUser.user_id AS customer.users.CustomerUser.userId',
      'customer->users->CustomerUser.customer_id AS customer.users.CustomerUser.customerId',
    ])
  .from('customer-product AS CustomerProduct')
  .leftOuterJoin('customers AS customer', 'CustomerProduct.customerId', 'customer.id')
  .innerJoin('users AS customer->users', 'customer->users.id', 'customer->users->CustomerUser.user_id')
  .innerJoin('customer-users AS customer->users->CustomerUser', 'customer.id', 'customer->users->CustomerUser.customer_id')
    .andOn('customer->users.id', '=', ':userId', [userId]);

Когда я запускаю его, я вижу эту ошибку:

{"message":"ApiErrorMiddleware TypeError: db.sequelize.knex.select(...).from(...).leftOuterJoin(...).innerJoin(...).innerJoin(...).andOn is not a function","level":"info"}
{"message":"UnknownErrorMiddleware TypeError: db.sequelize.knex.select(...).from(...).leftOuterJoin(...).innerJoin(...).innerJoin(...).andOn is not a function","level":"info"}

Я тоже пробовал это:

.from('customer-product AS CustomerProduct')
      .leftOuterJoin('customers AS customer', function () {
        this.innerJoin('customer-users AS customer->users->CustomerUser', 'customer.id', 'customer->users->CustomerUser.customer_id', function () {
          this.innerJoin('users AS customer->users', 'customer->users.id', 'customer->users->CustomerUser.user_id')
          .andOn('customer->users.id', '=', ':userId', [userId])
        })
        .where('CustomerProduct.id', customerProductId);
      });

Это дает мне эту ошибку:

{"message":"ApiErrorMiddleware TypeError: this.innerJoin is not a function","level":"info"}
{"message":"UnknownErrorMiddleware TypeError: this.innerJoin is not a function","level":"info"}

Я думаю, что проблема в том, как таблицы customer_users и users являются вложенными, но я не знаю, как повторить это с Knex. Это правильно, и если да, то как должен выглядеть мой запрос Knex, чтобы это исправить? В качестве альтернативы, есть ли другой способ построения запроса, который можно выполнить с помощью Knex?

...