scope property is not being applied to loopback model - javascript

I have used an "include" : "organization" query in the scope of my request.json file, which is a related model. But, the relation is not being included in the resulting output from a query.
The model (request.json file) looks like...
{
"name": "request",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"amount": {
"type": "number",
"required": true
},
"deadline": {
"type": "date",
"required": true
}
},
"validations": [],
"relations": {
"organization": {
"type": "belongsTo",
"model": "organization",
"foreignKey": "",
"options": {
"nestRemoting": true
}
}
},
"scope" : {
"include" : "organization"
}
}

In order to include another model, you must have a foreignKey defined in your model relations.
"relations": {
"organization": {
"type": "belongsTo",
"model": "organization",
"foreignKey": "organizationId",
"options": {
"nestRemoting": true
}
}
},
set the name of the foreignKey that you want to use. organizationId in this case, and this field will be added to your model request

Related

Loopback query with where inside include

I am trying to query my data the following way:
const filter = {where: {activity_id: activity_id, include: {relation: 'planGoal', scope: {where: {activity_goal_id: goal_id}}}}};
However this doesn't seem to work only the activity_id filter is applied and the wrong data is returned.
So my question is how do I query data within an include? and is it even possible?
For reference, here are the models in question:
{
"name": "plan_goal_has_action_option",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "number"
},
"activity_id": {
"type": "number"
},
"plan_has_goal_id": {
"type": "number"
},
"action_option": {
"type": "string"
}
},
"validations": [],
"relations": {
"planGoal": {
"type": "belongsTo",
"model": "plan_has_goal",
"foreignKey": "plan_has_goal_id"
}
},
"acls": [],
"methods": {}
}
{
"name": "plan_has_goal",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"id": {
"type": "number"
},
"activity_id": {
"type": "number"
},
"activity_goal_id": {
"type": "number"
}
},
"validations": [],
"relations": {
"goal": {
"type": "belongsTo",
"model": "activity_goal",
"foreignKey": "activity_goal_id"
},
"actions": {
"type": "hasMany",
"model": "plan_goal_has_action_option",
"foreignKey": "plan_has_goal_id"
}
},
"acls": [],
"methods": {}
}
include and where are two different filters:
{
where: {
activity_id: activity_id
},
include: {
relation: 'planGoal',
scope: {
where: {
activity_goal_id: goal_id
}
}
}
}

Loopback query with join

in loopback, I started with this model:
[
{
"mov_id": 0,
"mov_tipo": "string",
"mov_valore": 0,
"mov_causale_fk": 0,
"mov_conto_fk": 0,
"mov_data": "2017-10-04T09:02:19.620Z",
"mov_note": "string",
"mov_utente_fk": 0,
"mov_aggiunta": "2017-10-04T09:02:19.620Z"
}
]
then I added two relations, which correspond to two Foreign Keys in my MySQL database:
"relations": {
"causale_fk": {
"type": "hasOne",
"model": "causali",
"foreignKey": "causale_id",
"options": {
"nestRemoting": true
}
},
"conto_fk": {
"type": "hasOne",
"model": "conti",
"foreignKey": "conto_id",
"options": {
"nestRemoting": true
}
}
},
I would also like to see the fields in those models, as if I did make a query with JOIN.
it's possible??
ok, i resolved with scope.
this is the model:
{
"name": "movimenti",
"plural": "movimenti",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"scope": {
"include": [
"causale_fk",
"conto_fk"
]
},
"properties": {
"mov_id": {
"type": "number",
"id": true,
"required": true
},
"mov_valore": {
"type": "number",
"required": true
}
},
"validations": [],
"relations": {
"causale_fk": {
"type": "hasOne",
"model": "causali",
"foreignKey": "causale_id"
},
"conto_fk": {
"type": "hasOne",
"model": "conti",
"foreignKey": "conto_id",
"include": "conti"
}
},
"acls": [],
"methods": {}
}
bye!!
You can your to add options in your filter, and the particular option you need to set is include. Check here for more details.
This is an example for you particular needs:
{
Model.find({include:['causale_fk','conto_fk']}, function(){});
}

check uniqueness of multicolumn model in strongloop

How can I validate multicolumn uniqueness in strongloop?
{
"name": "Table1",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"column1": {
"type": "string"
},
"column2": {
"type": "string"
},
"column3": {
"type": "string"
}
},
"validations": [],
"relations": {}
},
"acls": [],
"methods": {}
}
In above model, how can I make column1 and column2 as unique key in loopback model? I checked validatesUniquenessOf but it doesn't validate multiple columns.
Thanks
It is working with below:
Table1.validatesUniquenessOf('column1', 'column2');
Not sure, I tried it previously also but that time it was not working. Probably I didn't have re-started loopback server.

Access related models inside the Loopback isomorphic client

I want to fetch the profiles for a given company using the Loopback isomorphic client.
The Company model makes use of the MySQL connector and the profiles live inside an ElasticSearch instance.
The loopback client is working perfectly inside my front-end app, the following code runs successfully:
let lbclient = window.require('lbclient')
lbclient.models.RemoteProfile.find().done(profiles => {
// do something with the profiles array
})
My goal is to fetch profiles as a nested resource:
/api/companies/{id}/profiles/
The question is: why the following code doesn't work on the client?
// This is the code I execute on the client
lbclient.models.RemoteCompany.findById(8, (err, company) => {
company.profiles // undefined
})
// This code works very well on the server
Company.findById(8, (err, company) => {
company.profiles((err, profiles) => {
// profiles belong the the company having id=8
}
})
common/models/company.json
{
"name": "Company",
"plural": "companies",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true,
"default": "My Company"
}
},
"validations": [],
"relations": {
"profiles": {
"type": "hasMany",
"model": "Profile",
"foreignKey": "company_id"
}
},
"acls": [],
"methods": {}
}
common/models/profile.json
{
"name": "Profile",
"plural": "profiles",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string"
},
},
"validations": [],
"relations": {
"company": {
"type": "belongsTo",
"model": "Company",
"foreignKey": "company_id"
}
},
"acls": [],
"methods": {}
}
client/loopback/models/remote-company.json
{
"name": "RemoteCompany",
"base": "Company",
"plural": "companies",
"trackChanges": false,
"enableRemoteReplication": true
}
client/loopback/models/remote-profile.json
{
"name": "RemoteProfile",
"base": "Profile",
"plural": "profiles",
"trackChanges": false,
"enableRemoteReplication": true
}
You need to include profiles in query like this :
lbclient.models.RemoteCompany.findById(8, {include: 'profiles'}, (err, company) => {
})

Double many-to-many relationship

In loopback I have defined two models with a double many-to-many relationship as follows:
action.json:
{
"name": "Action",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"text": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"benchmark": {
"type": "hasAndBelongsToMany",
"model": "Course",
"foreignKey": "benchmark"
},
"main": {
"type": "hasAndBelongsToMany",
"model": "Course",
"foreignKey": "main"
}
},
"acls": [],
"methods": {}
}
course.json:
{
"name": "Course",
"base": "TrashModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"benchmark": {
"type": "hasAndBelongsToMany",
"model": "Action",
"foreignKey": "benchmark"
},
"main": {
"type": "hasAndBelongsToMany",
"model": "Action",
"foreignKey": "main"
}
},
"acls": [],
"methods": {}
}
Now when I try to create a relation between an action model and a course model with the following PUT request:
http://localhost:3000/api/courses/57331a4eeff440cb02c886ae/benchmark/rel/5731d60da2c6d238e3c3d9b3
Then when I request the course model with the related action models included with the following GET request:
http://localhost:3000/api/courses/57331a4eeff440cb02c886ae?filter=%7B%22include%22%3A%5B%22benchmark%22%2C%22main%22%5D%7D
I get:
{
"name": "Introduction Lessons",
"id": "57331a4eeff440cb02c886ae",
"benchmark": [{
"text": "text here",
"id": "5731d60da2c6d238e3c3d9b3"
}],
"main": [{
"text": "text here",
"id": "5731d60da2c6d238e3c3d9b3"
}]
}
So apparently the action is now attached through both the benchmark as the main relation. How did this happen? Do I setup my models wrong?
From what I understand, when you use hasAndBelongsToMany relashionship, loopback uses a through table which it manages automatically for you.
I believe by default it calls itself From_modelTo_model. In order to have two such relashion you need to tell loopback to manage it differently otherwise they use the same through table.
Try with through option such as
Action
"benchmark": {
"type": "hasAndBelongsToMany",
"model": "Course",
"foreignKey": "benchmark",
"through":"ActionCourseBenchmark"
},
"main": {
"type": "hasAndBelongsToMany",
"model": "Course",
"foreignKey": "main",
"through":"ActionCourseMain"
}
Course
"benchmark": {
"type": "hasAndBelongsToMany",
"model": "Action",
"foreignKey": "benchmark",
"through":"ActionCourseBenchmark"
},
"main": {
"type": "hasAndBelongsToMany",
"model": "Action",
"foreignKey": "main",
"through":"ActionCourseMain"
}
see this github issue for more details

Categories

Resources