Loopback getting-started-intermediate tutorial question

30 wyświetleń
Przejdź do pierwszej nieodczytanej wiadomości

Suyash Agrawal

nieprzeczytany,
16 sty 2018, 06:23:3216.01.2018
do LoopbackJS
Hi,

I am currently going through the loopback-getting-started-intermediate tutorial and I am not able to understand some defined relations.
Specifically, I fail to understand why did we define the relation "CoffeeShop hasmany Reviewers" ? This would require that, in each
Reviewer there should be a field "coffeeShopId", which makes no sense. Can anyone help me out on this ?

Also, if we define a relation "A hasmany B" then this makes object of B have an attribute "aId", but if we want to query all the items of B that A has then we would have to scan through the whole table and match all the B's whose "aId" is the corresponding one. Isn't this quite inefficient ? Wouldn't it have been better to have a list of "bId" in every A object which denoted the objects of B that A has.

Thanks,
Suyash Agrawal

Francois Laforge

nieprzeczytany,
16 lut 2018, 09:42:2616.02.2018
do loopb...@googlegroups.com
The example probably isn't the best.  If you assume that each coffee shop is independant and a reviewer needs to create an account for each coffeeshop they want to review, then the relationship makes sense.  If a reviewer wants to create 1 account and review many coffee shops, then a hasManyThrough relationship would be better (many-to-many).

For your other question, the hasMany relationship is a one-to-many relationship, so each reviewer has 1 coffeeShopId.  To query the data, you need to create a "hasMany" reationship in the coffeeShop model and a "belognsTo" relationsthip in the Reviewer model.

To get a list of reviewers for a coffee shop your filter would look like:

filter ={include:'reviewers'}

To see the coffeeshop a reviewer has reviewed (or belongs to in this case), your filter looks like this:

filter={include:'coffeeShop'}



Odpowiedz wszystkim
Odpowiedz autorowi
Przekaż
Nowe wiadomości: 0