Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sails.js get many to many association count

I have a model (user) that has an association with another model (phone). This association is many to many. The following call is built in to Sails and allows me to get all the phone records for a particular user:

GET - /user/:userId/phones

I would like to be able to implement pagination on that call but cannot figure out how to get the total number of results. I've tried overwriting the blueprints find.js and/or findOne.js in order to return the count but the call above doesn't seem to run through that logic.

like image 773
Michael Abashian Avatar asked Mar 27 '15 18:03

Michael Abashian


1 Answers

Great question. Sails implements many-to-many associations using a "join" model. It doesn't show up in your api/models folder, but you can still query it if you need to. In your case it would be something like:

sails.models['user_phones__phone_users'].count({user_phones: userId}).exec(...)

The exact model name depends on your models and their via keys; simplest way to figure it out is to run sails console and do:

sails.util.keys(sails.models)

to list all the models in the system.

like image 146
sgress454 Avatar answered Sep 21 '22 09:09

sgress454