Skip to content

Filtering relationships #182

@carlbennettnz

Description

@carlbennettnz

I've found a case in an app I'm working on where I need to be able to filter relationships. My domain model looks like this:

  • Members have many networks
  • Members have many tags
  • Tags have one network

I need to query a member's tags for a given network. I think I could make this work in a hacky way using SQL views. I could have a network_members view which holds a copy of each member for each network they belong to, with an added network field.

How that would work, if it matters
CREATE VIEW network_members AS
  SELECT members.*, network_members.network AS network FROM members
  LEFT JOIN network_members ON
    network_members.member = members.id

The tags relationship would then be built with this join:

JOIN member_tags ON
  network_members.id = member_tags.member
  AND network_members.network = member_tags.network

I would then expose the network_members table instead of the members table to the knex adapter.


 
This isn't a great solution though, and I don't think you could do something like that with MongoDB. The design described in this JSON API issue would be a lot nicer. Using that API, I could do this:

GET /members?include=tags
GET /members?include=tags&filter[tags]=(network,42)

I also have a similar problem when making updates. I want to be able to PATCH a member with new tags linkage, but limit the change to tags for a given network. I haven't seen a proposal for this, but I might have missed it. That request could maybe look something like this:

PATCH /members/123?filter[tags]=(network,42)

The only work around for this I can think of is implementing a query transform that intercepts the PATCH requests and modifies the linkage to only change the things it's supposed to change. I also don't think that would work with the view-based workaround for the querying limitations above. There should be a better way.

So with that in mind, a few questions:

  • Is there any standardisation of JSON:API filtering coming in the near-ish future? It's always seemed like a big whole in the otherwise-quite-thorough spec to me.
  • Do you think that linked proposal would be something worth implementing here?
  • Is there any workaround you can think of that would work for both reads and writes?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions