在 Knex 中加入表后如何 return 一个数据数组?

How to a return an array of data after joining tables in Knex?

我需要加入三个表:剧院、movies_theaters、电影。我需要 return 三个表中的所有数据,但 movies_theaters 和电影在剧院的数组中。

我目前拥有的:

const theatersMoviesJoin = knex('theaters as t')
  .join('movies_theaters as mt', 't.theater_id', 'mt.theater_id')
  .join('movies as m', 'm.movie_id', 'mt.movie_id');

const getAllTheaters = () => theatersMoviesJoin.select('t.*', 'm.*', 'mt.*');

结果如下:

{
  "theater_id": 1,
  "name": "Regal City Center",
  "address_line_1": "801 C St.",
  "address_line_2": "",
  "city": "Vancouver",
  "state": "WA",
  "zip": "98660",
  "true": null,
  "movie_id": 1,
  "title": "Spirited Away",
  "runtime_in_minutes": 125,
  "rating": "PG",
  "description": "Chihiro and her parents are moving to a small Japanese town in the countryside, much to Chihiro's dismay. On the way to their new home, Chihiro's father makes a wrong turn and drives down a lonely one-lane road which dead-ends in front of a tunnel. Her parents decide to stop the car and explore the area. They go through the tunnel and find an abandoned amusement park on the other side, with its own little town...",
  "image_url": "https://imdb-api.com/images/original/MV5BMjlmZmI5MDctNDE2YS00YWE0LWE5ZWItZDBhYWQ0NTcxNWRhXkEyXkFqcGdeQXVyMTMxODk2OTU@._V1_Ratio0.6791_AL_.jpg",
  "created_at": "2021-03-08T19:07:31.130Z",
  "updated_at": "2021-03-08T19:07:31.130Z",
  "is_showing": true
}

我需要的样子:

{
  "theater_id": 1,
  "name": "Regal City Center",
  "address_line_1": "801 C St.",
  "address_line_2": "",
  "city": "Vancouver",
  "state": "WA",
  "zip": "98660",
  "created_at": "2021-02-23T20:48:13.335Z",
  "updated_at": "2021-02-23T20:48:13.335Z",
  "movies": [
    {
      "movie_id": 1,
      "title": "Spirited Away",
      "runtime_in_minutes": 125,
      "rating": "PG",
      "description": "Chihiro...",
      "image_url": "https://imdb-api.com...",
      "created_at": "2021-02-23T20:48:13.342Z",
      "updated_at": "2021-02-23T20:48:13.342Z",
      "is_showing": false,
      "theater_id": 1
    }
  ]
}

无法使用 Knex(它只是一个查询生成器)完成,您需要手动完成。

const flatList = [{ ... }, { ... }];

function groupByTheater(flatList) {
  const groupedList = flatList.reduce((result, row) => {
    result[row.theater_id] = result[row.theater_id] || {
      theater_id: row.theater_id,
      name: row.name,
      address_line_1: row.address_line_1,
      address_line_2: row.address_line_2,
      city: row.city,
      state: row.state,
      zip: row.zip,
      created_at: row.created_at,
      updated_at: row.updated_at,
      movies: [],
    };

    result[row.theater_id].movies.push({
      movie_id: row.movie_id,
      title: row.title,
      runtime_in_minutes: row.runtime_in_minutes,
      rating: row.rating,
      description: row.description,
      image_url: row.image_url,
      created_at: row.created_at,
      updated_at: row.updated_at,
      is_showing: row.is_showing,
      theater_id: row.theater_id,
    });

    return result;
  }, {});

  return Object.values(groupedList);
}

const groupedList = groupByTheater(flatList);