Search code examples
sails.jswaterline

SailsJS/Waterline Association with 3 Models


Context: Let's say that I've got 3 models in a sailsJS app talking to a PostGres backend via waterline: artist, song and rating. The artist contains a collection of songs, the songs contain a collection of ratings. Simple, yes?

Problem: I want to be able to query on the song and have it populate the list of artists with ratings that go along with that. So I might do something like this:

ratings:[4,2,1]
songs:[1,4,5]

where each rating is the id of the object that I created before and each song is the id of the song object in the association.

That way, on the client, I can go in, find the index on both the ratings and songs arrays, and know that I've got rating 4 on song 1, rating 2 on song 4, etc. But when I do that, it seems that Waterline re-orders the objects in the association from low-to-high based on the id. So when I call the endpoint, I get this:

ratings:[1,2,4]
songs:[1,4,5]

Thereby breaking the relationship between the different arrays.

I think this is probably a commmon use case and I'm missing something basic...what is it? A parameter setting on waterline?

Or is there a better way to do this?


Solution

  • I never found an answer for this, but I ended up refactoring my models to achieve a higher-level of abstraction in my associations.