Loopback referencesMany nested foreign key - node.js

I want to reference a different model(as discribed here: https://loopback.io/doc/en/lb2/Embedded-models-and-relations.html) but the by a nested id:
{
"name" : "person",
...
"relations": {
"cars": {
"type": "referencesMany",
"model": "car",
"foreignKey": "cars.id"
}
}
Person json will actually be something like:
{
...
cars: [{"id": 1, "name": "car1"}, ...]
}
And car model will be the full car details
Do I have to write my own remote method to do this?

Yosh DaafVader,
I've came accross this issue also and took time to find a solution ^^ but actually you just have to play with the parameter options inside your target relation property. The documentation states how the relation should be defined (sure the loopback cli does not include in version 3.x yet the way to use embeds nor references).
In your person model you have to change the foreignKey and to add the following options to be able to only use id to reference cars.
{
"name" : "person",
...
"relations": {
"cars": {
"type": "referencesMany",
"model": "car",
"foreignKey": "",
"options": {
"validate": true,
"forceId": true
}
}
}
Now you will be able to see in the explorer the new routes to add, remove and see the cars that belongs to the target person.
[Edit]
the foreignKey shall be blank, in order to be able to add items properly in the list of cars, or you can test and give some feedbacks about it
The validate option ensures the id exists in your database
forceId option will ensure it accepts only ids as a parameter
[/Edit]
Hope it will help :)
Cheers

Related

JHipster - How to generate Entity with a field as List of String?

I'm using jhipster-generator 4.14.5 and im trying to generate an Entity with a field Persons. But Persons is a List of String List<String> Persons.
How can i achieve it in JHipster. I tried to generate a simple field as String, then i changed the POJO like this :
#ElementCollection
#Column(name="persons")
List<String> persons;
The domain.json containing the whole table remain not touched.
I tried to run the application, after running liquibase:diff, without success. How can i fix it?
Use the generator entity to create a relationship :
Create an entity Person (maybe with only the "name", but more fields will soon be needed. Like "active", some dates ...)
.jhipster/[YourEntity].json should contain :
"fields": [
{
"fieldName": "xxx",
"fieldType": "Integer"
}
],
"relationships": [
{
"relationshipName": "person",
"otherEntityName": "person",
"relationshipType": "one-to-many",
"relationshipValidateRules": [
"required"
],
"otherEntityField": "name"
}
],
don't forget to commit before using the generator. Maybe you will need multiple executions to get it right.

Is there a way to maintain referential integrity/data consistency among models if I'm using the MongoDB connector?

Let's say I have a user who possesses a referencesMany relationship with home. That would look something like this in common/models/user.json:
"relations": {
"homes": {
"type": "referencesMany",
"model": "home",
"foreignKey": "homeIds"
}
},
So, as I associate many home models with the user model, the array of homeIds grows larger:
{
"homeIds": ["1", "2", "3"]
}
But, let's say I delete the home object with the id of "1".
How do I ensure that every other model referencing this model is updated to remove its reference to the deleted model?

Loopback "Include" Filter Fails with hasManyThrough Relation

I have 3 models. 2 resource models, account(id, name) and widget(id, name), and 1 mapping model to map between the two widget_to_account(id, account_id, widget_id), to tell what widgets an account has access to, so to speak.
When stating the relationship between the models in their JSONs, using the guide in http://loopback.io/doc/en/lb3/HasManyThrough-relations.html, RESTful requests like "get widgets of account id=1" for example, works perfectly.
GET /accounts/1/widgets yields the widgets that account 1 has, yielding a widgets array:
[
{
"id": 1,
"name": "wg_user_mgr"
},
{
"id": 2,
"name": "wg_desc"
}
]
That's all good.
However, say I wanted to append this widgets array result along with the account object returned by a GET to the account model?. Loopback documentation suggests that this is done using the include keyword with the request, like so:
GET /accounts/1?filter[include]=widgets, returning hopefully an account model with it's allowed widgets:
{
"id": 1,
"name": "Account1Name",
"widgets": [
{
"id": 1,
"name": "wg_user_mgr",
"display_name": "User Manager"
},
{
"id": 2,
"name": "wg_desc",
"display_name": "Description"
}
]
}
However, what is actually returned by loopback with that request, is:
{
"id": 1,
"name": "Account1Name",
"widgets": []
}
Empty widgets array! When I look at the loopback SQL debugs, I see that it does go to the widget_to_account table and selects the entries of account_id=1, but interestingly it stop there and just returns an empty widgets array.
Any clues? The hasManyThrough loopback docs doesn't actually show any examples of using include like this to bridge two models that are connected via a mapping model.
My guess is they just forgot to code it in ¯\_(ツ)_/¯
UPDATE:
Doing some more digging around, I found the answer at https://groups.google.com/forum/#!topic/loopbackjs/sH7bKoqzU5c.
Where you define the relationships in the 2 resource models, you have to specifically define the "keyThrough" value.
NOT THIS:
"relations": {
"widgets": {
"type": "hasMany",
"model": "widget",
"foreignKey": "account_id",
"through": "widget_to_account"
}
}
BUT THIS:
"relations": {
"widgets": {
"type": "hasMany",
"model": "widget",
"foreignKey": "account_id",
"through": "widget_to_account",
"keyThrough": "account_id"
}
}
This is not made super clear, and is even stated incorrectly in the loopback api docs -.-
UPDATE:
Doing some more digging around, I found the answer at https://groups.google.com/forum/#!topic/loopbackjs/sH7bKoqzU5c.
Where you define the relationships in the 2 resource models, you have to specifically define the "keyThrough" value.
NOT THIS:
"relations": {
"widgets": {
"type": "hasMany",
"model": "widget",
"foreignKey": "account_id",
"through": "widget_to_account"
}
}
BUT THIS:
"relations": {
"widgets": {
"type": "hasMany",
"model": "widget",
"foreignKey": "account_id",
"through": "widget_to_account",
"keyThrough": "widget_id"
}
}
This is not made super clear, and is even stated incorrectly in the loopback api docs. I wish they'de stop this "auto-naming" paradigm they've been pushing around. Looking at loopback SO and the wider community, it's generally caused so much pain with models being named incorrectly, keys like this being set to totally arbitary names -.-

Loopback "hasMany" relation issue in (PUT modelA/{id}/modelB/{fk}) method with MongoDB

I'm trying to use (GET or PUT) /modelA/{id}/modelB/{fk}, to add a relation from an existing model in A and B.
Every time, I use the method I just get:
"No instance with id xxxxx found for modelB" (Althought this instance already exist).
ModelA.json
{
"name":"modelA",
...
"relations": {
"deals": {
"type": "hasMany",
"model": "Deal",
"foreignKey": ""
}
},
....
}
ModelB.json
{
"name":"modelB",
...
"relations": {
"category": {
"type": "belongsTo",
"model": "modelA",
"foreignKey": ""
}
}
....
}
Note: Im using MongoDB. I tried the option here (Loopback hasMany relation doesn't work on mongodb) of changing the version of mongodb connector and juggler, but still the same.
Also, I have another model C (modelA hasMany model of it, relation defined in .json), but modelC (can also be in many, but not define in .json, just implicit, because I don't need the inverse relation), and also the method (PUT) and (GET) that have the fk are not working (GET or PUT) /modelA/{id}/modelC/{fk}.

Active Model Serializer remove relationship member when using JSON API

I'm using the latest AMS v0.10.0.rc3 with the JSON API adapter.
So far is working great and is adding some useful conventions that i would like to change.
For example, lets suppose that i have a Post serializer and a Comment serializer like this:
class Post < ActiveModel::Serializer
attributes :id, :title
has_many :comments
end
class Comment < ActiveModel::Serializer
attributes :id, :comment
belongs_to :post
end
Then if i request /posts/1 for example i get the following
{
"data": {
"id": "1",
"type": "posts",
"attributes": {
"title": "My awesome title",
},
"relationships": {
"comments": {
"data": [
{
"id": "1",
"type": "comments"
},
{
"id": "2",
"type": "comments"
}
]
}
}
}
}
Notice how the relationships member appears and according with the spec is marked as an optional member with MAY.
It is a nice convention that i need to override some times.
So my question is:
How i remove the relationship member at the serializer or controller level ?
(If i miss some detail please comment and i will update the question.)
Currently, your best option is to define two serializers, one with the associations, one without, and specify in your render call which to use (render json: posts, each_serializer: PostWithoutAssociationsSerializer).
There is an ongoing discussion about adding an include_if option to associations and attributes, which you could potentially leverage if it gets merged.

Resources