Hi everyoneI'm trying to build a json rest api with PostrgreSQL and Knex. I have the following data:Team (id, name)Event(id, date, teamid)When i just query it with knex like this:async get(id) { return await knex .from('team') .select('team.id', 'team.name', 'event.id as eventId', 'event.date') .where({ 'team.id': id }) .leftJoin('event', { 'team.id': 'event.teamId' }); } what i get from this:[ {"id": 14, "name": "Team1", "eventId": 2, "date":"2019-01-31T23:00:00.000Z" }, {"id": 14, "name": "Team1", "eventId": 3, "date":"2019-02-01T23:00:00.000Z"}, ... ] What i want to get instead:{ "id": 14, "name": "Team1", "events": [ {"id": 2, "date":"2019-01-31T23:00:00.000Z"}, {"id": 3, "date":"2019-02-01T23:00:00.000Z"}, ... ] } is there a way to do this directly in knex or do i have to manually group the data after the query?
Submitted January 11, 2019 at 05:15PM by Tiim_B
No comments:
Post a Comment