Several associations in Sails.js

I am currently playing with associations in Sails.js beta (v 0.10.0-rc4).

I am trying to link multiple databases into one result (using sails-mysql).

The link looks like this: PostTagRelationTag. And I, when I query the table Post, I want the returned object to include related names Tag.

Models look something like this:

// Post model
var Post = {
    title: 'string',
    body: 'string',
    tag_relations: {
        collection: 'TagRelation'
            via: 'post_id'
    }
};
// Tag Relation model
var TagRelation = {
    post_id: {
        model: 'Post'
    },
    tag_id: {
        model: 'Tag'
    }
};
// Tag model
var Tag = {
    name: 'string',
    tag_relations: {
        collection: 'Tag',
        via: 'tag_id'
    }
};

Now, when I http://localhost:1337/post/1get to , I will get a JSON object with a key tag_relationscontaining an array of objects TagRelation, but is there a way to get a list of the actual objects Tagthey mean instead? Or is there a better way to do this?

+4
1

Sails , TagRelation:

// Post model
var Post = {
    title: 'string',
    body: 'string',
    tags: {
        collection: 'tag',
        via: 'posts',
        dominant: true // could be on either model, doesn't matter
    }
};

// Tag model
var Tag = {
    name: 'string',
    posts: {
        collection: 'post',
        via: 'tags'
    }
};

, /post/1 tags. . docs - ​​ .

dominant:true Sails , , . , , , .

+8

Source: https://habr.com/ru/post/1533363/


All Articles