Hi, im using loopback 3, and i need to do this query:
select nbd.Comment.id as commentId,
nbd.Comment.content as commentConted,
commentCreator.id as userCommentId,
commentCreator.username userComment,
reply.id as replyId,
reply.content as replyContent,
replyCreator.id as replyUserId,
replyCreator.username as replyUser
from nbd.Comment
inner join nbd.User commentCreator on (nbd.Comment.userId =
commentCreator.id)
left join nbd.Comment reply on (nbd.Comment.commentParentId =
reply.commentParentId)
left join nbd.User replyCreator on (reply.userId = replyCreator.id)
So, to do that, i used this include filter:
{
"include": {
"relation": "user",
"scope": {
"fields": [
"id",
"username"
]
},
"relation1": "comments",
"scope1": {
"include": [
"user"
]
}
}
}
But, it doesn't work...
Here is the context, a comment is created by a user, also the comment can have a reply that also is created by a user.
This is the comment
model relations:
"relations": {
"user": {
"type": "belongsTo",
"model": "MyUser",
"foreignKey": "userId"
},
"comment": {
"type": "belongsTo",
"model": "Comment",
"foreignKey": "commentParentId"
},
"comments": {
"type": "hasMany",
"model": "Comment",
"foreignKey": "commentParentId"
}
And this is my-user
model relation with comments
:
"relations": {
"comments": {
"type": "hasMany",
"model": "Comment",
"foreignKey": "userId"
}
}
the solution is simple, if some one want to to a similar query or multiple includes in the same level with loopback, here is and example:
{
"where": {
"field": 1
},
"order": "field DESC",
"include": [
{
"relation": "relation1",
"scope": {
"fields": [
"field1",
"field2",
"field3"
],
"where": {
"field2": {
"gt": 2
}
}
}
},
{
"relation": "relation2",
"scope": {
"fields": [
"field1",
"field2",
"field3",
"field4"
]
}
}
]
}
With these structure is possible to generate multiples includes, the most important aspect on queries with includes are the relations, the relations have to be well defined.