Saturday, 22 April 2017

How can I do a Many-to-Many where clause in Sails.js/Waterline?

I have this code

Image.find({ tags: {id: 3}}).exec(console.log);

which is wrong, but my intention is to find all images that have the tag id 3.

An image can have many tags, and many images can be tagged with the same tag (many-to-many).

Model code.

Image

module.exports = {
    attributes: {
        tags: {
            collection: 'Tag',
            via: 'images'
        }
    }
};

Tag

module.exports = {
    attributes: {
        images: {
            collection: 'Image',
            via: 'tags'
        }
    }
};

I don't want to use SQL raw queries, nor I want to use N+1 queries to populate everything.

I also tried using .populate() by using Image.find(3).populate("images")... but it will only populate the images, but each image won't have tabs, so this doesn't work for me.



via Felo Vilches