What relation should exist between a user model and team model?

22 views
Skip to first unread message

Jackson Vaughan

unread,
Jun 14, 2018, 3:04:21 PM6/14/18
to LoopbackJS
Hi, 

I've been reading through the documentation and am fairly confused on what the suggested relation should be for my use case (which I consider to be somewhat standard).

I have two user models - `users` and `teams`

These are the requirements:
- A user can have (be the owner) of multiple teams
- A team can have multiple members (users)

So far, I've set it up so that teams have a belongsTo relation with users as such:

"owner": {
"type": "belongsTo",
"model": "user",
"foreignKey": "",
"options": {
"nestRemoting": false
}
}

This seems to work as expected; a user can create a team, etc. 

For the members relationship however, I can't figure out what the right relation would be. If I do a `hasMany` relation for example, I can't add existing users to the team... Would a `hasAndBelongsToMany` relation make sense? That however seems to require a join model that I'd need to apply or create somehow. When I implement the following, it throws an error saying the database does not exist if I try to add a user as a member:

"members": {
"type": "hasAndBelongsToMany",
"model": "user",
"foreignKey": "",
"options": {
"nestRemoting": false
}
}

 But I can't find much documentation on it, only this unresolved issue: https://github.com/strongloop/loopback/issues/1960

Any help would be much appreciated!

Samrita Sen Bakshi

unread,
Jun 19, 2018, 2:50:24 AM6/19/18
to loopb...@googlegroups.com
This should help Since owner is the user and so are the other team memebers hence you can make a relation in owner model namely teamOwner .
//In owner model

"properties":{
"teamOwner": {
"type": "string",
"required": false
}

},

"relations":{
"members": {
"type": "hasMany",
"model": "owner",
"foreignKey": "teamOwner"
}
}

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/0ce80ebc-22ac-4ac0-8a47-b03acfe5a848%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Regards,
Samrita Sen Bakshi
9962976959
Reply all
Reply to author
Forward
0 new messages