How do I use a remote method in one model to return info from another model? - node.js

So I've set up something really simple to learn how to use Loopback.
The models are as follows:
Person
- based on built in User model
food_pref
typeId (number)
personId (number)
food_type
type (string)
Relationships:
Person has many food_prefs (foreign key: personId)
food_pref belongs to Person (foreign key: personId)
food_pref belongs to food_type (foreign key: typeId)
An auto-generated method gets created that returns the food_prefs based on the id of the Person.
People/{id}/foodPrefs
This returns:
[
{
"typeId": 0,
"personId": 0,
"id": 0
}
]
What I want to do is add a separate remote method called "getPrefs" that returns the name of the type under food_type based on the typeId in food_pref.
So let's say typeId is 1 and id 1 in food_types is Italian Food then the remote method would return this:
{
"type": "Italian Food"
}
I was told to use Person.js and add something along these lines but I'm really confused about the include statement as well as what to do inside the brackets. Often it crashes with an error saying: Error: Relation "food_pref" is not defined for Person model, see what they recommended below:
module.exports = function(Person) {
Person.getPrefs = function(personId, cb) {
Person.findById(personId, { include: { food_pref: "food_type" } }, function(err, user) {
if (err) throw err;
});
}
Person.remoteMethod (
'getPrefs',
{
http: {path: '/getPrefs', verb: 'get'},
accepts: {arg: 'personId', type: 'number', http: { source: 'query' } },
returns: {arg: 'type', type: 'string'}
}
);
};
What am I doing wrong here?

Edit:
According to strongloop documentation when you define a personal remote method, strongloop automatically provides a callback that will returns datas if needed.
See below updated code
You want to include the food_pref relation as well as the food_type realation inside this food_pref. Get this into your getPrefs custom method:
Person.getPrefs = function(personId, cb) {
Person.findById(personId, {
include: [{
relation: 'food_pref',
scope: {
include: {
relation: 'food_type'
}}}
]},
function(err, personFound) {
if (err)
console.log(err);
else {
cb(null, personFound)
}
});
};
What it does: your personal method is called with the personId argument and a cb argument (automatically passed by strongloop !). Your method finds the right person by id, includes the relations (name of the type of food as well), then when the result has been fetched, the callback inside "Person.findById" calls the callback 'cb' with datas fetched (here personFound)
Person.remoteMethod(
'getPrefs', {
http: {path: '/:personId/getPrefs', verb: 'get'},
accepts: [{arg: 'personId', type: 'number'}],
returns: {arg: 'person', type: 'object'},
description: ['a person object']
}
);
Then the returned object should contain the name of the type of food.
Make sure you include the right relation names you have in your Person.json and so on.
If you just want the name of food pref, follow the same idea with different methods:
just send the string inside your object to the automatic callback argument 'cb' (and modify the registration to announce the kind of return your method sends)
search directly inside food_type table with a "where" condition (where personId = the id of the person you are looking for)
Take a look at the link at the link to strongloop doc as it is pretty and detailed regarding remote methods.
Hope it helped.

Related

How to update the childs value of an object present in the mongod using mongoose with node.js?

i want to update particular child's value which is actually present inside the object and wants all my rest value be the same.
i want to use mongoose npm package.
for example:
basic:{
name: "ABC",
mobile: 1234567890,
age: 20
},
other:{
pincode: 123456,
email: "abc#abc.com"
}
My Code
Model.findOneAndUpdate(
{ "basic.mobile": 1234567890 },
{
basic:{ name: "CDE"}
},
(err, doc) => {
if (err) return res.send({ error: err });
res.send(`updated`);
}
);
it works but it override the previous basic data and create a new one which i provided. it should update but also rest value should be there.
how can i do this?
Your question contains the answer. For Filter you are referring a single field using dot operator properly that is
"basic.mobile": 1234567890
You should do similar thing for update as well, that is the second argument of your function call, the way you have defined is wrong becuase it is a new object - that should be used when you want replace whole basic object in DB. If you want to just update the name field then you should use dot operator like below.
"basic.name":"CDE"
So the whole function would look like below.
Model.findOneAndUpdate(
{ "basic.mobile": 1234567890 },
{ "basic.name": "CDE"},
(err, doc) => {
if (err) return res.send({ error: err });
res.send(`updated`);
}
);

GraphQL Resolver Problems

I've spent quite a bit of time reading through the GraphQL tutorials but unfortunately they don't seem to cover things in quite enough depth for me to get my head around. I'd really appreciate some help with this real world example.
In the examples the queries are placed at the root of the resolver object; I can get this to work fine for single level queries. When I attempt to resolve a nested query however the nested resolver never gets called. What I'm massively confused by is every tutorial I find that isn't issued on the graphql website put in a Query object and nest their queries underneeth that, not root level.
Consider the following Schema:
type Product {
id: String!
retailerId: String!
title: String!
description: String
price: String!
currency: String!
}
type OrderLine {
product: Product!
quantity: Int!
}
type Order {
id: String!
retailerId: String!
orderDate: Date!
orderLines: [OrderLine!]!
}
type Query {
product(id: String!): Product
order(id: String!): Order
}
schema {
query: Query
}
And the following query:
query {
order(id: "1") {
id
orderLines {
quantity
}
}
}
I have tried multiple versions of implementing the resolvers (just test data for now) and none seem to return what I exect. This is my current resolver implementation:
const resolvers = {
OrderLine: {
quantity: () => 1,
},
Order: {
orderLines: (parent: any, args: any) => { console.log("Calling order lines"); return []; },
},
Query: {
product(parent, args, ctx, other) {
return { id: args.id.toString(), test: true };
},
order: ({ id }) => { console.log("Calling order 1"); return { id: id.toString(), testOrder: true, orderLines: [] }; },
},
order: ({ id }) => { console.log("Calling order 2"); return { id: id.toString(), testOrder: true, orderLines: [] }; },
};
In the console I can oberse the "Calling order 2" log message, there are no logs to "Calling order lines" and the order lines array is empty.
So two part question:
1) Why does it hit "Calling order 2" and not "Calling order 1" in the above example?
2) Why won't the above work for the nested query Order.OrderLines?
Thanks in advance!
In query
type Query {
product(id: String!): Product
order(id: String!): Order
users: User
}
schema {
query: Query
}
In resolvers
const resolvers = {
order: ({ id }) => function
product: ({ id }) => function
}
Graphql work on query resolver concept. If you want to any query(example users) you must have
resolver(ie users) which return User having definition in type User.
Graphql query is interactive and case sensitive
The next step is to implement the resolver function for the order/product query.
In fact, one thing we haven’t mentioned yet is that not only root fields,
but virtually all fields on the types in a GraphQL schema have resolver functions.
1) Why does it hit "Calling order 2" and not "Calling order 1" in the above example?
In this Query
query {
order(id: "1") {
id
orderLines {
quantity
}
}
}
then it go to order which return Order with define type
2) Why won't the above work for the nested query Order.OrderLines?
You can only use two query first order and second product only as per your schema
Please check doc for nested query for this requirement.
If you use buildSchema to generate your schema, the only way to provide resolvers for your fields is through the root object. But this is more of a hack -- you're not actually overriding the default resolvers for the fields and as such, you're basically limited to just working with the root-level fields (as you are learning the hard way). This is why only the Query.order function is called -- this is a root-level field. Why passing functions through the root (kind of) works is explained in detail here.
The bottom line is you shouldn't be using buildSchema. If you want to use SDL to define your schema, migrate to using Apollo Server.

How to define a manytomany relationship on the same table in objectionjs?

I have an article table, where an article can cite multiple articles. Each article can also have a single author.
Article model :
class Article extends DefaultModel {
static get tableName() {
return "articles";
}
static get relationMappings() {
return {
author: {
relation: DefaultModel.BelongsToOneRelation,
modelClass: "Author",
join: {
from: "articles.authorId",
to: "authors.id"
}
},
mentions: {
relation: DefaultModel.ManyToManyRelation,
modelClass: "Article",
join: {
from: "articles.id",
through: {
from: "mentions.citingArticle",
to: "mentions.citedArticle"
},
to: "articles.id"
}
}
};
}
}
Mention model :
class Mention extends DefaultModel {
static get tableName() {
return "mentions";
}
static get idColumn() {
return ["citingArticle", "citedArticle"];
}
}
What I'm trying to do, is insertGraph, the main article + the articles that are mentioned in it, here is what I have :
async function insertData(fullArticle) {
const articleTx = await transaction(Article.knex(), async tx => {
const references = fullArticle.references
.map(articleObj => {
return {
...articleObj.article,
author: articleObj.author
};
});
const article = await Article.query(tx).insertGraph({
...fullArticle.article,
author: fullArticle.author,
mentions: references[0]
});
return article;
});
console.log(articleTx);
}
I know this is only inserting the main row + the first row, but I ran into multiple problems :
The mention model has a composite key, when I try to insert using the code above I get a (node:5944) UnhandledPromiseRejectionWarning: error: column "citedArticle" of relation "mentions" does not exist.
I also got the same error when I try to add "#id" to the main article mentions relation, and "#ref" to the cited article, and insert them in 2 different objects.
A third problem I ran into here, was a unique constraint on both the citing and the cited article authors. What if both of them were written by the same author? I have a unique string column in the authors table that isn't the id, when I tried to insert I got duplicate key value violates unique constraint "author_isn". I'm not sure how I can update the row to reference the existing author id if that happens.

What does 'cb' mean in loopback?

I am trying to learn loopback but I don't really understand what 'cb' means in function call. I read this In loopback documentation what does variable 'cb' stands for? and I have basic understanding of callback in nodejs but I just don't understand cb in loopback.
For example, http://docs.strongloop.com/display/public/LB/Remote+methods.
module.exports = function(Person){
Person.greet = function(msg, cb) {
cb(null, 'Greetings... ' + msg);
}
Person.remoteMethod(
'greet',
{
accepts: {arg: 'msg', type: 'string'},
returns: {arg: 'greeting', type: 'string'}
}
);
};
What does that cb mean? How can we know it accepts two parameters, null and a string? Hope someone could help.
So you have an Async function Person.greet which you'll call like this:
Person.greet('hello', function(err){
...
});
Notice that after 'hello' a second argument was passed and it is actually a function. It can also be defined outside with a name and passed this way:
function callback(err){
...
}
Person.greet('hello', callback);
Now it looks exactly how the Person.greet was defined:
Person.greet = function(msg, cb) {
cb(null, 'Greetings... ' + msg);
}
The difference here is just that in the definition it uses a different name: cb. It could've used any name since to it cb is just an argument. But generally "cb", "done", or "next" are used as a standard practice.
Looking at the answers, it seems to me that only 1 of the two questions were ever answered.
Question 1: What does that cb mean?
This has been answered before that it is a short for the callback function.
Question 2: How can we know it accepts two parameters, null and a string?
You define this in the return option of your remote method which does the following according to the docs:
Describes the remote method's callback arguments; See Argument descriptions. The err argument is assumed; do not specify.
So if we look at your example
Person.remoteMethod(
'greet',
{
accepts: {arg: 'msg', type: 'string'},
returns: {arg: 'greeting', type: 'string'}
}
);
You defined here that the callback parameters will be
callback(err, greeting: string)
Lets have another example from the docs:
MyModel.remoteMethod('download', {
isStatic: true,
returns: [
{ arg: 'body', type: 'file', root: true },
{ arg: 'Content-Type', type: 'string', http: { target: 'header' } },
],
});
For this example the callback will be
callback(err, body: file, Content-Type: string)
and the usage is like this
cb(null, stream, 'application/octet-stream');
I just came across the same question, and after a couple hours of frustration I've found the official answer.
https://docs.strongloop.com/display/public/LB/Remote+methods#Remotemethods-Howtodefinearemotemethod
OPTION ACCEPTS:
Defines arguments that the remote method accepts. These arguments map
to the static method you define. For the example above, you can see
the function signature: Person.greet(name, age, callback)... name is
the first argument, age is the second argument and callback is
automatically provided by LoopBack (do not specify it in your
accepts array). For more info, see Argument descriptions. Default if
not provided is the empty array, [].

Sails.js - Get an object (model) using multiple join

I am new to node.js and newer to Sails.js framework.
I am currently trying to work with my database, I don't understand all the things with Sails.js but I manage to do what I want step by step. (I am used to some PHP MVC frameworks so it is not too difficult to understand the structure.)
Here I am trying to get a row from my database, using 2 JOIN clause. I managed to do this using SQL and the Model.query() function, but I would like to do this in a "cleaner" way.
So I have 3 tables in my database: meta, lang and meta_lang. It's quite simple and a picture being better than words, here are some screenshots.
meta
lang
meta_lang
What I want to do is to get the row in meta_table that match with 'default' meta and 'en' lang (for example).
Here are Meta and Lang models (I created them with sails generate model command and edited them with what I needed):
Meta
module.exports = {
attributes: {
code : { type: 'string' },
metaLangs:{
collection: 'MetaLang',
via: 'meta'
}
}
};
Lang
module.exports = {
attributes: {
code : { type: 'string' },
metaLangs:{
collection: 'MetaLang',
via: 'lang'
}
}
};
And here is my MetaLang model, with 3 functions I created to test several methods. The first function, findCurrent, works perfectly, but as you can see I had to write SQL. That is what I want to avoid if it is possible, I find it more clean (and I would like to use Sails.js tools as often as I can).
module.exports = {
tableName: 'meta_lang',
attributes: {
title : { type: 'string' },
description : { type: 'text' },
keywords : { type: 'string' },
meta:{
model:'Meta',
columnName: 'meta_id'
},
lang:{
model:'Lang',
columnName: 'lang_id'
}
},
findCurrent: function (metaCode, langCode) {
var query = 'SELECT ml.* FROM meta_lang ml INNER JOIN meta m ON m.id = ml.meta_id INNER JOIN lang l ON l.id = ml.lang_id WHERE m.code = ? AND l.code = ?';
MetaLang.query(query, [metaCode, langCode], function(err, metaLang) {
console.log('findCurrent');
if (err) return console.log(err);
console.log(metaLang);
// OK this works exactly as I want (I would have prefered a 'findOne' result, only 1 object instead of an array with 1 object in it, but I can do with it.)
});
},
findCurrentTest: function (metaCode, langCode) {
Meta.findByCode(metaCode).populate('metaLangs').exec(function(err, metaLang) {
console.log('findCurrentTest');
if (err) return console.log(err);
console.log(metaLang);
// I get what I expected (though not what I want): my meta + all metaLangs related to meta with code "default".
// What I want is to get ONE metaLang related to meta with code "default" AND lang with code "en".
});
},
findCurrentOthertest: function (metaCode, langCode) {
MetaLang.find().populate('meta', {where: {code:metaCode}}).populate('lang', {where: {code:langCode}}).exec(function(err, metaLang) {
console.log('findCurrentOthertest');
if (err) return console.log(err);
console.log(metaLang);
// Doesn't work as I wanted: it gets ALL the metaLang rows.
});
}
};
I also tried to first get my Meta by code, then my Lang by code, and MetaLang using Meta.id and Lang.id . But I would like to avoid 3 queries when I can have only one.
What I'm looking for would be something like MetaLang.find({meta.code:"default", lang.code:"en"}).
Hope you've got all needed details, just comment and ask for more if you don't.
Do you know what populate is for ? its for including the whole associated object when loading it from the database. Its practically the join you are trying to do, if all you need is row retrieval than quering the table without populate will make both functions you built work.
To me it looks like you are re-writing how Sails did the association. Id suggest giving the Associations docs another read in Sails documentation: Associations. As depending on your case you are just trying a one-to-many association with each table, you could avoid a middle table in my guess, but to decide better id need to understand your use-case.
When I saw the mySQL code it seemed to me you are still thinking in MySQL and PHP which takes time to convert from :) forcing the joins and middle tables yourself, redoing a lot of the stuff sails automated for you. I redone your example on 'disk' adapter and it worked perfectly. The whole point of WaterlineORM is to abstract the layer of going down to SQL unless absolutely necessary. Here is what I would do for your example, first without SQL just on a disk adapter id create the models :
// Lang.js
attributes: {
id :{ type: "Integer" , autoIncrement : true, primaryKey: true },
code :"string"
}
you see what i did redundantly here ? I did not really need the Id part as Sails does it for me. Just an example.
// Meta.js
attributes: {
code :"string"
}
better :) ?
// MetaLang.js
attributes:
{
title : "string",
desc : "string",
meta_id :
{
model : "meta",
},
lang_id :
{
model : "lang",
}
}
Now after simply creating the same values as your example i run sails console type :
MetaLang.find({meta_id : 1 ,lang_id:2}).exec(function(er,res){
console.log(res);
});
Output >>>
sails> [ { meta_id: 1,
lang_id: 2,
title: 'My blog',
id: 2 } ]
Now if you want to display what is meta with id 1 and what is lang with id 2, we use populate, but the referencing for join/search is just as simple as this.
sails> Meta_lang.find({meta_id : 1 ,lang_id:2}).populate('lang_id').populate('meta_id').exec(function(er,res){ console.log(res); });
undefined
sails> [ {
meta_id:
{ code: 'default',
id: 1 },
lang_id:
{ code: 'En',
id: 2 },
title: 'My blog',
id: 2 } ]
At this point, id switch adapters to MySQL and then create the MySQL tables with the same column names as above. Create the FK_constraints and voila.
Another strict policy you can add is to set up the 'via' and dominance on each model. you can read more about that in the Association documentation and it depends on the nature of association (many-to-many etc.)
To get the same result without knowing the Ids before-hand :
sails> Meta.findOne({code : "default"}).exec(function(err,needed_meta){
..... Lang.findOne({code : "En"}).exec(function(err_lang,needed_lang){
....... Meta_lang.find({meta_id : needed_meta.id , lang_id : needed_lang.id}).exec(function(err_all,result){
......... console.log(result);});
....... });
..... });
undefined
sails> [ { meta_id: 1,
lang_id: 2,
title: 'My blog',
id: 2 } ]
Have you tried:
findCurrentTest: function (metaCode, langCode) {
Meta.findByCode(metaCode).populate('metaLangs', {where: {code:langCode}}).exec(function(err, metaLang) {
console.log('findCurrentTest');
if (err) return console.log(err);
console.log(metaLang);
});
},

Resources