Using hasMany relation and cascading ORM level in loopback4 - loopbackjs

I have created 2 models Author and Books where Author has hasMany relation with Book and Book has belongsTo relation with author.
While saving data using ORM models the cascading is not happening i.e
{
"authorId": 1,
"name": "qwery",
"experience": 2,
"books": [{
"BookId": 12,
"category": "string"
}]
}
The above should create a Author record in Author table and create a Book record with the authorId in Book table, which is not happening whereas from belongsTo it can able to create an Author record with just authorId.
You can find the code in the following GIT

You need to create a AuthorBookController to connect the two. Please see example code in here: https://github.com/strongloop/loopback-next/blob/master/examples/todo-list/src/controllers/todo-list-todo.controller.ts

Related

Associating multiple items with a single REST call

I am using the Loopback framework, and have modelA which is in a many-to-many relation with modelB.
I want to know if it's possible to relate multiple items from modelB to modelA.
There is currently a way to relate one item with this call:
/modelA/{id}/modelB/rel/{fk}
Is there any way to perform this in a bulk operation?
If I understand your question correctly, I think you can simply do it through a filter. ModelA has many modelBs, Let me assume the relation name is 'modelBs'
modelA.find({
where: [your filter option on model A]
include: {
relation: 'modelBs',
scope: {
[your filters on model B]
}
}
})
in restful way:
/modelAs?filter[include]=modelBs&filter[where]....
The official documentation may help: https://docs.strongloop.com/display/public/LB/Querying+data
It seems to be HasManyThrough model.
I believe your model names starts with lowercase but ideally it should be ModelA and ModelB and their instances can then be saved in modelA and modelB variables.
In order to use add method, you will need to first find instance of ModelA first using ModelA.findById which you can save in modelA variable and then use the following code:
modelA.modelBs.add(modelBFieldsObject, function(err, patient) {
...
});
where modelBs should be the name of relation in the ModalA.json file as in
"relations": {
"modelBs": {
"type": "hasMany",
"model": "ModelB",
"foreignKey": "modelAId",
"through": "ModelAModelB",
"keyThrough": "modelBId"
}
...
I think it should be allowed to pass an array of modelBFieldsObject to create multiple instances as in
modelA.modelBs.add([modelBFieldsObject, modelBFieldsObject], function(err, patient) {
...
});
Tip: For clarity, name your models beginning with upper case letter and their instance variable in camelCase format.
References: Methods added to the model.

How to set conditions on "through" model defined by "hasManyThrough" relation in Loopback

I defined 3 models in Loopback: job, contact and job_contact(through model) and using hasManyThrough relations defined below relation:
job has many contact through job_contact.
and I used the code below to find contacts by job through job_contact
job.findById(id, {
include: {
relation:'contact',
where :{deleted: false}, // no working here
scope:{
where:{deleted: false} // here will add condition on contact table
}
}
})
Someone got any ideas? How can I put conditions on "through" model, job_contact model, in this case?
I have found it is not possible to interact with the through model with filters on queries the two other models. If you want to access the through model you need to query it independently.
See part More Info at the bottom of the answer here and the linked github discussion.

How to create relationship using Strongloop strong arc?

How can I create relationships via slc or by directly editing models in a text editor?
I could not find a way to do so using strong arc composer. Does this feature exist?
You cannot create relations using Arc (unfortunately!). It's sure nice to have.
To create relations you can use the command in the cli from the project's root:
slc loopback:relation
This will prompt you with the models available. You can then select the type of relationship you want to have with the selected models. eg, one to many or many to many.
Then you can see the modified .json file in the common folder to view the relations created.
Alternatively, you can also edit the .json file directly. see the example which sets the relation between user and user-tokens
{
"name": "User",
. .
.
"relations": { // relations
"accessTokens": { // specify relation name
"type": "hasMany", // type of relation
"model": "AccessToken", // model to which relation is made
"foreignKey": "userId" // foreign key
}
}
}

Django Union Query

I need to develop a UNION query in Django with 3 models namely WebQuery,WebReply and BusinessOwners and the output should be of the form below.
{
"(#conversation_id#)_(#b_id#)": {
"from": "(#user_id)",
"email": "(#user_email)",
"date_time": "#get from db",
"query": "are you open ?",
"from_r_id": "(#representative_id)",
"from_r_name": "(#rep_name)",
"business_registered": "FALSE"
"to_business_name": "CCD saket",
"chat": [{
"direction": 1,
"text": "yes sir",
"date_time": "424 577"
}, {
"direction": 0,
"text": "ok",
"date_time": "424 577"
}]
},
I know how to query when only one model is involved, but not sure of the union query.
How will this be achieved?
I personally would say that if this is going to be a common query then I would recommend making a SQL View then querying that.
w3schools has a VERY simple overview of what a view is : http://www.w3schools.com/sql/sql_view.asp
In SQL, a view is a virtual table based on the result-set of an SQL statement.
This means you can write your required sql statement and create a view using this. Then create a django model which mirrors that view which you can then use to query.
So, you will create an SQL view:
CREATE VIEW view_name AS
SELECT a, b, c
FROM table_name
WHERE condition
Then create a django model, which has a slight difference to a normal model:
class view_name(models.Model):
class Meta:
# https://docs.djangoproject.com/en/1.5/ref/models/options/#django.db.models.Options.managed
managed = False
a = models.CharField(max_length)
....
managed = false > https://docs.djangoproject.com/en/1.5/ref/models/options/#django.db.models.Options.managed
You can then query this using the normal django orm syntax
Or there is similar questions:
Previous stackoverflow question, union in django orm
How can I find the union of two Django querysets?
How can I find the union of two Django querysets? provides an example of a union using the '|' operator. I'm not sure how different your models are. If there's common fields you could place those in a separate model and use model inheritance

Backbone-relational with Django

I'm building a Backbonejs app that is running Django in the backend. In my Django, i have models like Author, books, shelf and user and they are related to one another. In Backbone, I have a model Author and when I do a fetch() I get its related models in an array. Should I proceed like this or it's better to create the same models in Backbone and do the same relations between them? (with backbone-relational)
Also, let's say I go with the second option, when i do fetch() and get related models, shall backbone-relational recognize it directly?
Thanks
I would recommend leveraging Backbone relational--especially if you expect the app to get somewhat complex later.
Probably you won't have to change your server side code, you can get Backbone relational to instantiate any related models that are included in the JSON of the model you have fetched. So if for an author query your backend returns:
[{
name: "Hemingway, Ernest",
books: [
{name: "For whom the bell tolls", ISBN: 1234},
{name: "The sun also rises", ISBN: 2345}
]
}]
... and you have defined a has-many relationship between Authors and books Backbone relational will instantiate the books as well.