Every order has productIds property which is an array of productIds as explained bellow. How should I implement the relations in StrongLoop \ Loopbck 3?
I couldn't find in the document the right relation type i should choose. I'm using MongoDB.
Orders
{
"_id": "5bc9eed3336746028d88651f",
"name": "My Order",
"createdAt": "2018-10-19T14:48:50.961Z",
"updatedAt": "2018-11-21T09:57:34.729Z",
"currency": "EUR",
"productIds": ["5f16de532b3fa13644bdf513", "5f170559afcb6d3644ee4f1e"]
}
Products
{
"_id": "5f16de532b3fa13644bdf513",
"name": "Product One",
"createdAt": "2018-10-19T14:48:50.961Z",
"updatedAt": "2018-11-21T09:57:34.729Z"
}
{
"_id": "5f170559afcb6d3644ee4f1e",
"name": "Product Two",
"createdAt": "2018-10-19T14:48:50.961Z",
"updatedAt": "2018-11-21T09:57:34.729Z"
}
It really depends on whether you need to reference back from the product to the corresponding orders.
Case 1: If you need that and the products can be related to many orders, the right relation is ManyToMany. For lb3 and this type of relation you can use the hasAndBelongsToMany relation.
Case 2: If you need that and each product can be related to one order then use the hasMany relation.
Case 3: If you don't need that, then the best choice would be the referencesMany, as it allows to connect the orders to their corresponding products without affecting the product instances.
From your description, I would guess that the case 3 matches what you are trying to build, so I would go with the referencesMany.
Example Definition:
{
"name": "Order",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"name": "string"
},
"relations": {
"products": {
"type": "referencesMany",
"model": "Product",
"foreignKey": "productIds"
}
...
}