join-monster icon indicating copy to clipboard operation
join-monster copied to clipboard

1-to-1 Join

Open Felix-N opened this issue 6 years ago • 4 comments

Hi,

first thanks for join-monster, really helping a lot!

What I missed so far in the docs is how to handle 1-to-1 joins between tables, probably as an INNER JOIN, maybe LEFT JOIN.

My understanding so far is that I could create a new GraphQL Object. However this leaves me with a new unneeded object in the code and in the response, which could be just flat.

So far I've solved it by using a VIEW, but I'd prefer to keep those joins in purely the code.

Is there a way I have missed?

Felix-N avatar Apr 18 '18 15:04 Felix-N

In my case I can not use VIEWS and even if I could, it's the opposite way of what this library is doing. With VIEW you are JOINing tables even in cases when you don't need that data.

There is one more way you can solve this:

// One object using two tables
OneObject: {
  sqlTable: 'parent_table',
  uniqueKey: 'id',
  fields: {
    localField: {
      // using first table - parent_table
      sqlColumn: 'some_column'
    }
    remoteField: {
      // using second table - remote_table
      sqlExpr: parentTable => `(SELECT remote_column FROM remote_table  WHERE ${parentTable}.join_column = remote_table.join_column)`
    }
  }
}

But I agree it's not ideal and JOIN would probably perform faster.

horakmx avatar May 07 '18 15:05 horakmx

Good point - I'll give it a try.

Though I'd really like to see a possibility to do a 1-1 join more elegant.

Felix-N avatar May 07 '18 17:05 Felix-N

Same problem is discussed here: #227, #198

horakmx avatar May 09 '18 13:05 horakmx

Please join the discussion on this wiki page where we can lay down how this API should look like.

GlennMatthys avatar Nov 02 '18 19:11 GlennMatthys

Closing as marked as duplicate

nicoabie avatar May 05 '24 19:05 nicoabie