Two relations from same model loopback not working - node.js

I have two models users and appointments.
The users model is like below-
{
"users": {
"0": {
"id": "1",
"name": "test1",
"role": "doctor"
},
"1": {
"id": "2",
"name": "test2",
"role": "patient"
},
"2": {
"id": "3",
"name": "test3",
"role": "support"
}
}
}
Now in the above model , if the role is doctor , we are calling it as doctor_id , if patient then patient_id and so on.
Now my appointment model is below->
{
"name": "appointments",
"plural": "appointments",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"appointmentDate": {
"type": "date"
},
"appointmentTime": {
"type": "string"
},
"doctorId": {
"type": "string"
},
"patientId": {
"type": "string"
}
},
"validations": [],
"relations": {
"Doctor": {
"type": "belongsTo",
"model": "users",
"foreignKey": "doctorId"
},
"Patient": {
"type": "belongsTo",
"model": "users",
"foreignKey": "patientId"
}
},
"acls": [],
"methods": {}
}
So when i try to GET all appointments , it is not sending the relation data from users. If i add single relation it work as expected , but not working with multiple relations from same model.
Thanks in advance,

Including my previous comment to give context:
The way I did it, I believe foreignkey should be "" for both. You should not define doctorId and patientId and you probably have to define two hasMany relations in you user class with foreign key "Doctor" and "Patient"
To give an example here are the relations defined in my user class (called customer)
"commentsWritten": {
"type": "hasMany",
"model": "comment",
"foreignKey": "sourceCustomerId"
},
"commentsReceived": {
"type": "hasMany",
"model": "comment",
"foreignKey": "targetCustomerId"
},
then in my comment definition I have the following
"properties": {
...
"targetCustomerId": {
"type": {
"required": true
}
},
"sourceCustomerId": {
"type": {
"required": true
}
}
},
"relations": {
"targetCustomer": {
"type": "belongsTo",
"model": "customer",
"foreignKey": ""
},
"sourceCustomer": {
"type": "belongsTo",
"model": "customer",
"foreignKey": ""
}
},
Please note, that I did define the properties (ids) but, if I remember correctly, it was only so I could force them to be not null, i.e. you should not need it.

If you go to the belongsTo documentation you can see that the foreignKey field is empty, so you need to remove it from your relations definition.
You probably also want to define the other side of the relation.
I.e. define a hasMany relation users hasMany appointments:
{
"name": "users",
"base": "PersistedModel",
...
"relations": {
"doctors": {
"type": "hasMany",
"model": "appointments",
"foreignKey": "doctorId"
},
"patients": {
"type": "hasMany",
"model": "appointments",
"foreignKey": "patientId"
},
...
}
However, what you are trying to do might not be supported (and in fact I'm not even sure if it makes sense for LoopBack).
You can check the documentation on polymorphic relations and even though it's a work in progress there's no mention of having Model A hasMany Model B through more than one foreignKey at the same time.
LoopBack would need to have some logic to search under the first foreign key and if nothing is found then searching under the other foreign key, and I'm not sure if this complex composition is supported.
Suggestion
Why not define two models, one for Doctors and one for Patients and have two different relations instead? You can even go to the documentation on HasManyThrough relations and you can see an example that models something very similar to what you are trying to do.

Related

Custom junction table in strapi

I have recently started working on strapi and was looking at the relations inside model in their documentation. My scenario is as follows. I have a model named course and another named tag. They have many to many relationship between them.This is what ./api/course/models/course.settings.json has when I made the relation between them named as tag2.
{
"connection": "default",
"collectionName": "course",
"info": {
"name": "course"
},
"options": {
"increments": true,
"timestamps": true
},
"attributes": {
"image_link": {
"type": "string"
},
"created_by": {
"columnName": "created_by_id",
"plugin": "users-permissions",
"model": "user"
},
"updated_by": {
"columnName": "updated_by_id",
"plugin": "users-permissions",
"model": "user"
},
"title": {
"type": "string"
},
"short_description": {
"type": "text"
},
"slug": {
"type": "string",
"unique": true
},
"tags2": {
"collection": "tag",
"via": "courses",
"dominant": true
}
}
}
When I specify the relation using the admin panel strapi itself made a junction table named as courses_tags_2_s__tags_courses.
Here is what tag model looks like
{
"connection": "default",
"collectionName": "tag",
"info": {
"name": "tag",
"mainField": "ui_label"
},
"options": {
"increments": true,
"timestamps": true
},
"attributes": {
"code": {
"type": "string"
},
"description": {
"type": "string"
},
"created_by": {
"plugin": "users-permissions",
"model": "user",
"columnName": "created_by_id"
},
"updated_by": {
"plugin": "users-permissions",
"model": "user",
"columnName": "updated_by_id"
},
"ui_label": {
"type": "string"
},
"courses": {
"via": "tags2",
"collection": "course"
}
}
}
I have a couple of questions
1) Is there a way I can set up the junction table as courses_tags ? i.e overriding the strapi one
2) I have set my mainField as "ui_label" in tag.settings.json but in the admin panel while editing course table content(rows in course table), in the related field of tag2 I see "code" field shown there instead of "ui_label". How to set the mainField?
Note: I have setup strapi with mysql server.
so to answer your first question, there is currently no way to override the join table between two models. This is totally auto-generated by Strapi.
For the second question, this part of the docs is out to date.
To manage display information you will have to use the content manager configuration in the admin panel.
Here a short video - https://www.youtube.com/watch?v=tzipS2CePRc&list=PL7Q0DQYATmvhlHxHqfKHsr-zFls2mIVTi&index=5&t=0s
For 1) Is there a way I can set up the junction table as courses_tags ? i.e overriding the strapi one:
You can specify the following option:
"collectionName": "courses_tags"

loopback relation to same model

I have a FollowMap as follows
{
"name": "FollowMap",
.
.
.
"relations": {
"follower_acc": {
"type": "belongsTo",
"model": "Account",
"foreignKey": "follower_id"
},
"following_acc": {
"type": "belongsTo",
"model": "Account",
"foreignKey": "following_id"
}
}
}
I want to build a relation to my Account model, so I can get one users follower & following list, the Account model is as follows;
{
"name": "Account",
.
.
.
"followers": {
"type": "hasMany",
"model": "FollowMap",
"foreignKey": "following_id"
},
"following": {
"type": "hasMany",
"model": "FollowMap",
"foreignKey": "follower_id"
}
}
}
From the relation, I can fetch a Account's follower and following count however, I can't fetch the follower and following Account list from these relations. I hope it's clear.
I know this issue is maybe outdated, but for those who are still looking for the solution, i'll give mine. I wanted to do implement the publisher/subscriber relation between the profils entities in my database.
The most obvious way to do it is with the hasAndBelongsToMany relation, but strangely you cannot change the foreignKey used in model: it means you cannot declare "publishedId" and "subscriberId" to be used as foreign keys referencing a Profil entity.
You have to declare manually the third table used to reference those 2 foreign keys.
My working example for this third table:
//common/models/subscribing.json
{
"name": "Subscribing",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"subscriber": {
"type": "belongsTo",
"model": "Profil",
"as": "subscriber"
},
"publisher": {
"type": "belongsTo",
"model": "Profil",
"as": "publisher"
}
},
"acls": [],
"methods": {}
}
And my Profil entity related to itself through the Subscribings table:
//common/models/profil.json
{
"name": "Profil",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"followers": {
"type": "hasMany",
"model": "Profil",
"foreignKey": "publisherId",
"keyThrough": "subscriberId",
"through": "Subscribing"
},
"subscribings": {
"type": "hasMany",
"model": "Profil",
"foreignKey": "subscriberId",
"keyThrough": "publisherId",
"through": "Subscribing"
}
},
"acls": [],
"methods": {}
}

How do I get Loopback to prefer short ids when doing id injection?

I have a loopback app which gives ids like "56dbfa7089223aca7946ca14" when creating models. I would prefer ids like "0" or "73". Is there a way to adjust id-injection practices to have ids start at 0 and increment as base-10 integers?
The data store is MongoDB v2.6.10)
running loopback v2.22 on node v5.7.1 on Ubuntu 15.10
Here's the relevant model.json
{
"name": "Term",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"beginDate": {
"type": "date",
"required": true
},
"endDate": {
"type": "date",
"required": true
}
},
"validations": [],
"relations": {
"lessons": {
"type": "hasMany",
"model": "Lesson",
"foreignKey": ""
},
"classes": {
"type": "hasMany",
"model": "Class",
"foreignKey": ""
},
"weeklySchedules": {
"type": "hasMany",
"model": "WeeklySchedule",
"foreignKey": ""
}
},
"acls": [],
"methods": {}
}
there are several ways to do it, you can read about them in official documentation
It is stated by mongo developers, that is completely ok, to use your own id, if you really need it, so go for it.
However if you do it only because you "dont want ObjectId, because it does not look good", I suggest you, to get used to it.

Include sub model in related model in loopback

I have Account model which hasMany Posts, and Post belongsTo Account
Account:
{
"name": "Account",
"base": "User",
"relations": {
"post": {
"type": "hasMany",
"model": "Post",
"foreignKey": "accountId"
},
...
},
...
}
Post:
{
"name": "Post",
"base": "PersistedModel",
"relations": {
"account": {
"type": "belongsTo",
"model": "Account",
"foreignKey": ""
}
},
...
}
Now, I have model Question which is sub model of Post model.
{
"name": "Question",
"base": "Post", ...
}
I want to query for specific Account all his fields and to include all his Questions with something like this
Account.findById({
id: id,
filter: {include: 'question'},
function(){...});
Is it possible?
Account.findById(id, { include: { relation: 'questions' } }, function(){...});
You may need to create a questions relationship in the Account model, as I don't think it will inherit those from your Post model.
Note also that you should probably rename the post relationship to posts. So your relations section should look like:
Account:
{
"name": "Account",
"base": "User",
"relations": {
"posts": {
"type": "hasMany",
"model": "Post",
"foreignKey": "accountId"
},
"questions": {
"type": "hasMany",
"model": "Question",
"foreignKey": "accountId"
}
...
},
...
}

Add the current user before store a model strongloop

I recently start with Strongloop framework,
I made a simple model
{
"name": "income",
"plural": "incomes",
"base": "PersistedModel",
"idInjection": true,
"properties": {
"description": {
"type": "string",
"required": true
},
"amount": {
"type": "number",
"required": true
},
"when": {
"type": "date",
"required": true
}
},
"validations": [],
"relations": {
"user": {
"type": "belongsTo",
"model": "User",
"foreignKey": ""
}
},
"acls": [],
"methods": []
}
I want make a relation with current session user and the income record. but I can't find a example for that.
You can use the current context, there is a code example at the bottom of the link, but you have to expand it:
Link

Resources