Sunday 31 December 2017

How to appropriately query a has many relationship with knexjs ?

I've used sequelize before and they have a include property where you can add associations to different models. See here for an example of how I used it The return data type would be something like this:(FYI, I am using MySQL)Say there is a table called boards & threads.The relation I have is that "boards has many threads". So I want the response to look like this:{ boardName: "pol", threads: [{subject: "...", author: "", ...etc}]} Where the threads array is given to me because of the "included" model I specified.How can this be achieved in knexjs? What query must I build? I tried using the join() method but that merged everything together as expected. I came up with this solution but I feel as though I can take advantage of knexjs query methods to create my association array like sequelize does.TL;DR - How can I use knexjs to return an array of values based on a has many relation for tables?

Submitted January 01, 2018 at 12:36AM by HappyZombies

No comments:

Post a Comment