Access related models inside the Loopback isomorphic client - javascript

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) => {
})

Related

What value do I insert inside the 'Body' parameter of my API request according to this API Specification?

I'm trying to make a data request from a URL based on the data provider's API specification.
My app is built using the Node.JS and NPM.
I'm subscribed to use the API and I've verified that all my identity credentials, policies and IAM keys that give me permission to access the API.
Could someone please help me define the correct value to place inside the 'Body' parameter of my API request based on the API specification provided by the URL.
Thanks!
My current request parameter code looks like below:
const request_parameter = {
DataSetId: "xxxxxac09b4xxxxxxxxxx",
RevisionId: "xxxxxxc8df94fxxxxxxx",
AssetId: "xxxxxxx2bde1fa3xxxxxxx",
Method: "POST",
Path: "/pxxx/graphql",
QueryStringParameters: {
param1: " ",
param2: " "
},
RequestHeaders: {
"Content-Type": "application/json"
},
Body: {\"body_param\":\"body_param_value\"} // FIX ME
};
The data provider's actual API specification looks like below:
{
"openapi": "3.0.1",
"info": {
"title": "image-services",
"description": "API Gateway for integration of Platform Services into AWS Data Exchange",
"version": "2022-03-03T20:04:18Z"
},
"servers": [
{
"url": ""
}
],
"paths": {
"/psdm/graphql": {
"post": {
"requestBody": {
"description": "GraphQL query for the Catalog in the form of:\n\n `{\n availablemetadatarecord(\n QUERYPARAMS\n ) { \n FIELDLIST\n } \n }`\n",
"content": {
"application/graphql": {
"schema": {
"$ref": "#/components/schemas/CatalogSearchRequest"
},
"examples": {
"Limit returned data": {
"description": "Limit the number of records in a response.\n\nIn your query, simply specify `limit: <int>` \n",
"value": "{\n availablemetadatarecord(\n limit: 10\n content: { catalogProperties: { vendor: { eq: \"EYE\" } } }\n ) {\n assignedId\n }\n}\n"
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CatalogSearchResponse"
}
}
}
}
},
"security": [
{
"sigv4": []
}
]
}
}
},
"components": {
"schemas": {
"CatalogSearchRequest": {
"title": "CatalogSearchRequest",
"type": "object"
},
"CatalogSearchResponse": {
"title": "Catalog Search Response",
"required": [
"data",
"paginationInfo"
],
"type": "object",
"properties": {
"paginationInfo": {
"type": "object",
"properties": {
"recordsInPage": {
"type": "integer"
},
"nextOffset": {
"type": "integer"
}
}
},
"data": {
"type": "object",
"properties": {
"availablemetadatarecord": {
"type": "array",
"description": "Records returned by query",
"items": {
"type": "object"
}
}
}
}
}
}
},
"securitySchemes": {
"sigv4": {
"type": "apiKey",
"name": "Authorization",
"in": "header",
"x-amazon-apigateway-authtype": "awsSigv4"
}
}
}
}

Remote method in LoopBack not being called

When calling a remote method in my LoopBack project, it seems to only return an empty array. I don't think it is really being called at all but perhaps the default implementation is being called.
member.js
module.exports = function(Member) {
Member.getProjectsForMember = function(id, callback) {
console.error('HERE');
return callback(null, {'test': '123'});
};
};
member.json
{
"name": "member",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": {
"projects": {
"type": "hasMany",
"model": "project",
"options": {
"nestRemoting": false
}
}
},
"acls": [{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$authenticated",
"permission": "ALLOW"
},
{
"accessType": "*",
"principalType": "ROLE",
"principalId": "$unauthenticated",
"permission": "DENY"
}
],
"methods": {
"getProjectsForMember": {
"accepts": [{
"arg": "id",
"type": "number",
"required": true,
"http": {
"source": "path"
}
}],
"returns": [{
"arg": "projects",
"type": "Object",
"root": true,
}],
"http": [{
"path": "/:id/projects",
"verb": "get"
}]
}
}
}
I am calling it using the LoopBack API Explorer via a GET to /members/{id}/projects:
http://localhost:3000/api/members/3e26u0aa62155715vcb52afa/projects?access_token=R6GKVHwFuMG2caJexuyoMd0JSNOWtvLVXIEmRj1IkNSrM54bwomQLxHcpqlyFaHk
The response is []. I would expect to see the response of {'test': '123'} and the 'HERE' logged to the terminal but I do not.

scope property is not being applied to loopback model

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

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(){});
}

Categories

Resources