1
votes

I have a Seller model. It's base class is User.

I have a Sku model that belongsTo Seller. Seller hasMany relation with Sku.

I have a License model that belongsTo Sku and Sku hasMany relation with License.

If I was in MySql db, I would put seller_id in Sku table and make sku (sku string in Sku table) and seller_id together unique. Because every seller can have the same Sku but one Seller can't have more than one sku.

At the same time I would put seller_id and sku_id in License table that I can get the all licenses for a Seller.

Back to the my loopback models. I'm thinking that if Seller has hasMany relation to License and License belongsTo Seller, would it be the same with my MySql thoughts?

1
Providing some code could help - Ebrahim Pasbani
@EbrahimPasbani there is no code. It's only modeling. - Volkan Ozyilmaz
I mean that modeling, like json files. Anyway when you have relation from Seller to Sku and Sku has relation to License, there is no need to make a relation from Seller to licence directly. - Ebrahim Pasbani
@IsmaelDiVita you mean, should I take care about the permissions? I haven't learned ACL yet but I was assuming that I could manage to add permissions if I follow Models' ids. Isn't it possible to give the permission only owner (Seller) of the license trough Sku? - Volkan Ozyilmaz
Yes, the $owner ACL only works with models related with a Model based on User, in your case Seller, you'll have to make a relation between Seller and License (belongsTo) - Ismael Di Vita

1 Answers

0
votes

Loopback doesn't support JOIN.

So for question

At the same time I would put seller_id and sku_id in License table that I can get the all licenses for a Seller.

in lookpack, model License only has one foreignId skuId.

seller.skus({include: licenses}) would return you a seller's licenses in the form of:

[
  sku_1: [
      license_1,
      license_2,
      ...
  ],
  sku_2: [
      license_1,
      license_3,
      ...
  ],
  ...
]

Some doc link you might be interested in:

How to query related models in loopback: http://loopback.io/doc/en/lb2/Querying-related-models.html

How to use filter include: http://loopback.io/doc/en/lb2/Include-filter.html