Querying from models with HasManyThrough relations - strongloop api - node.js

This is a follow up to a previous question. Currently, the api can query from the category and game model which share a relation. For example, through this endpoint /Categories/1001/games/mature I can list all games of fighting category that have mature set to true. However, I have included a third model gameInfo from db table game_info. Since, I want to fetch the information from those three tables, i have included a through model named gamesCategoriesBridge from db table games_categories_bridge. I followed the guidelines to set HasManyThrough relations. The issue is that the additional information such as description and publishedDate doesnt show in the final result. How could I properly set the remoteMethod to accomplish the below?
common/models/category.js
module.exports = function(Category) {
Category.mature = function(id, callback) {
var app = this.app;
var Game = app.models.Game;
Game.find({
"where": {
categoryId: id,
mature: true
}
}, function(err, gameArr) {
if (err) return callback(err);
console.log(gameArr);
callback(null, gameArr);
});
}
Category.remoteMethod(
'mature', {
accepts: [{
arg: 'id',
type: 'number',
required: true
}],
// mixing ':id' into the rest url allows $owner to be determined and used for access control
http: {
path: '/:id/games/mature',
verb: 'get'
},
returns: {
arg: 'games',
type: 'array'
}
}
);
};
Table schema:
catgories
category_name category_id
------------- -----------
fighting 1001
racing 1002
sports 1003
games
game_id game_name category_id mature
----------- ------------ ----------- --------------
13KXZ74XL8M Tekken 10001 true
138XZ5LPJgM Forza 10002 false
game_info
game_id description published_date
----------- ----------- --------------
13KXZ74XL8M Published by Namco. 1994
138XZ5LPJgM Published by Microsoft Studios. 2005
games_categories_bridge
game_id category_id
----------- -----------
13KXZ74XL8M 10001
138XZ5LPJgM 10002
Endpoint: /categories/{id}/games/mature
Desired Format for API Response:
games [
{
gameName: 'Tekken',
gameInfo :
[
{
description : 'Published by Namco.',
published_date : '1994'
}
],
categorName: 'fighting',
categoryId: 1001,
mature: true
}
.....
]

First create a hasMany relation between game and game_info model
//Now inside remote_method.
Category.mature = function(id, callback) {
var app = this.app;
var Game = app.models.game;
Category.findById(id, {}, function(err, category) {
if (err) return callback(err);
//Now call the Game find method
Game.find({
"where": {
categoryId: id,
mature: true
},
include:'game_info'
}, function(err, gameArr) {
if (err) return callback(err);
gameArr.forEach(function(gameObj, index){
gameObj.categoryName = category.category_name;
});
callback(null, gameArr);
});
});
}

Related

Node.js - How to get associated model data using graphql?

In Node.js app, I am using graphql to get the list of data. I have created two models called School and Grade. Association for these models like School has many Grades and Grade belongs to School.
While querying I am getting null value for associated model.
In model school.js,
module.exports = (sequelize, Sequelize) => {
const School = sequelize.define("school", {
name: {
type: Sequelize.STRING,
},
email: {
type: Sequelize.STRING,
},
});
School.associate = function (models) {
School.hasMany(models.Grade, { foreignKey: "school_id" });
};
return School;
};
In typeDefs.graphql,
type Query {
getSchoolDetails: [School]
getSchoolDetail(id: ID!): School
getGradeDetails: [Grade]
}
type School {
id: ID!
name: String
email: String
grades: [Grade]
}
type Grade {
id: ID!
school_id: ID!
name: String
school: School
}
In resolvers.js,
const Query = {
getSchoolDetails: async () => {
try {
const schools = await school.findAll();
return schools;
} catch (err) {
console.log(err);
}
},
getSchoolDetail: async (root, { id }) => {
try {
const scl = await school.findByPk(id);
return scl;
} catch (err) {
console.log(err);
}
},
getGradeDetails: async () => {
try {
const grades = await grade.findAll({});
return grades;
} catch (err) {
console.log(err);
}
},
}
In playground when I query like,
query {
getSchoolDetails{
id
name
email
grades{
name
}
}
}
Output for this is,
{
"data": {
"getSchoolDetails": [
{
"id": "1",
"name": "Rotary West School",
"email": "rotary#gmail.com",
"grades": null
},
{
"id": "2",
"name": "Excel Public School",
"email": "excel#gmail.com",
"grades": null
},
]
}
Same way when I query to get Grades will get school as null. I am learning nodejs relations with graphql, please help me to solve this.
You have to use include with your queries in resolvers.js file like below
getSchoolDetails: async () => {
try {
const schools = await school.findAll({
include: [{ model: Grade }],
});
return schools;
} catch (err) {
console.log(err);
}
},
This Will get all the grades associated with schools
And if you want to include school with grade you have to associate grade with school using Grades belongsTo school

How do you seed a mongodb database such that the Keystone 5 CMS recognizes the many-to-many relationships?

Let's say I have two objects: Product and Seller
Products can have multiple Sellers.
A single Seller can sell multiple Products.
The goal is to write a seeding script that successfully seeds my MongoDB database such that Keystone.js's CMS recognizes the many-to-many relationship.
Schemas
Product.ts
import { text, relationship } from "#keystone-next/fields";
import { list } from "#keystone-next/keystone/schema";
export const Product = list({
fields: {
name: text({ isRequired: true }),
sellers: relationship({
ref: "Seller.products",
many: true,
}),
},
});
Seller.ts
import { text, relationship } from "#keystone-next/fields";
import { list } from "#keystone-next/keystone/schema";
export const Product = list({
fields: {
name: text({ isRequired: true }),
products: relationship({
ref: "Product.sellers",
many: true,
}),
},
});
KeystoneJS config
My keystone.ts config, shortened for brevity, looks like this:
import { insertSeedData } from "./seed-data"
...
db: {
adapter: "mongoose",
url: databaseURL,
async onConnect(keystone) {
console.log("Connected to the database!");
if (process.argv.includes("--seed-data")) {
await insertSeedData(keystone);
}
},
},
lists: createSchema({
Product,
Seller,
}),
...
Seeding Scripts (these are the files I expect to change)
I have a script that populates the database (seed-data/index.ts):
import { products } from "./data";
import { sellers } from "./data";
export async function insertSeedData(ks: any) {
// setup code
const keystone = ks.keystone || ks;
const adapter = keystone.adapters?.MongooseAdapter || keystone.adapter;
const { mongoose } = adapter;
mongoose.set("debug", true);
// adding products to DB
for (const product of products) {
await mongoose.model("Product").create(product);
}
// adding sellers to DB
for (const seller of sellers) {
await mongoose.model("Seller").create(seller);
}
}
And finally, data.ts looks something like this:
export const products = [
{
name: "apple",
sellers: ["Joe", "Anne", "Duke", "Alicia"],
},
{
name: "orange",
sellers: ["Duke", "Alicia"],
},
...
];
export const sellers = [
{
name: "Joe",
products: ["apple", "banana"],
},
{
name: "Duke",
products: ["apple", "orange", "banana"],
},
...
];
The above setup does not work for a variety of reasons. The most obvious is that the sellers and products attributes of the Product and Seller objects (respectively) should reference objects (ObjectId) and not names (e.g. "apple", "Joe").
I'll post a few attempts below that I thought would work, but did not:
Attempt 1
I figured I'd just give them temporary ids (the id attribute in data.ts below) and then, once MongoDB assigns an ObjectId, I'll use those.
seed-data/index.ts
...
const productIdsMapping = [];
...
// adding products to DB
for (const product of products) {
const productToPutInMongoDB = { name: product.name };
const { _id } = await mongoose.model("Product").create(productToPutInMongoDB);
productIdsMapping.push(_id);
}
// adding sellers to DB (using product IDs created by MongoDB)
for (const seller of sellers) {
const productMongoDBIds = [];
for (const productSeedId of seller.products) {
productMongoDBIds.push(productIdsMapping[productSeedId]);
const sellerToPutInMongoDB = { name: seller.name, products: productMongoDBIds };
await mongoose.model("Seller").create(sellerToPutInMongoDB);
}
...
data.ts
export const products = [
{
id: 0,
name: "apple",
sellers: [0, 1, 2, 3],
},
{
id: 1,
name: "orange",
sellers: [2, 3],
},
...
];
export const sellers = [
{
id: 0
name: "Joe",
products: [0, 2],
},
...
{
id: 2
name: "Duke",
products: [0, 1, 2],
},
...
];
Output (attempt 1):
It just doesn't seem to care about or acknowledge the products attribute.
Mongoose: sellers.insertOne({ _id: ObjectId("$ID"), name: 'Joe', __v: 0}, { session: null })
{
results: {
_id: $ID,
name: 'Joe',
__v: 0
}
}
Attempt 2
I figured maybe I just didn't format it correctly, for some reason, so maybe if I queried the products and shoved them directly into the seller object, that would work.
seed-data/index.ts
...
const productIdsMapping = [];
...
// adding products to DB
for (const product of products) {
const productToPutInMongoDB = { name: product.name };
const { _id } = await mongoose.model("Product").create(productToPutInMongoDB);
productIdsMapping.push(_id);
}
// adding sellers to DB (using product IDs created by MongoDB)
for (const seller of sellers) {
const productMongoDBIds = [];
for (const productSeedId of seller.products) {
productMongoDBIds.push(productIdsMapping[productSeedId]);
}
const sellerToPutInMongoDB = { name: seller.name };
const { _id } = await mongoose.model("Seller").create(sellerToPutInMongoDB);
const resultsToBeConsoleLogged = await mongoose.model("Seller").findByIdAndUpdate(
_id,
{
$push: {
products: productMongoDBIds,
},
},
{ new: true, useFindAndModify: false, upsert: true }
);
}
...
data.ts
Same data.ts file as attempt 1.
Output (attempt 2):
Same thing. No luck on the products attribute appearing.
Mongoose: sellers.insertOne({ _id: ObjectId("$ID"), name: 'Joe', __v: 0}, { session: null })
{
results: {
_id: $ID,
name: 'Joe',
__v: 0
}
}
So, now I'm stuck. I figured attempt 1 would Just Work™ like this answer:
https://stackoverflow.com/a/52965025
Any thoughts?
I figured out a solution. Here's the background:
When I define the schema, Keystone creates corresponding MongoDB collections. If there is a many-to-many relationship between object A and object B, Keystone will create 3 collections: A, B, and A_relationshipToB_B_relationshipToA.
That 3rd collection is the interface between the two. It's just a collection with pairs of ids from A and B.
Hence, in order to seed my database with a many-to-many relationship that shows up in the Keystone CMS, I have to seed not only A and B, but also the 3rd collection: A_relationshipToB_B_relationshipToA.
Hence, seed-data/index.ts will have some code that inserts into that table:
...
for (const seller of sellers) {
const sellerToAdd = { name: seller.name };
const { _id } = await mongoose.model("Seller").create(sellerToAdd);
// Product_sellers_Seller_products Insertion
for (const productId of seller.products) {
await mongoose
.model("Product_sellers_Seller_products")
.create({
Product_left_id: productIds[productId], // (data.ts id) --> (Mongo ID)
Seller_right_id: _id,
});
}
}
...

Mongoose and Postman: test a model with nested objects

I created a model like this in nodeJS, using Mongoose:
'use strict';
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var plantSchema = new Schema({
plantData: [
{
family: { type: String, default: 'Liliaceae' },
genusObj: {
genus: { type: String, required: 'Please enter the genus plant name' },
tulipGroup: { type: String }, // e.g. Single Early
tulipGroupNumber: { type: Number } // e.g. 1
},
species: { type: String, required: 'Please enter the species plant name' },
commonName: { type: String },
description: { type: String },
mainImage: {},
otherImages: {},
images: {},
}
],
detailsData: [ .... ]
});
module.exports = mongoose.model('plants', plantSchema);
And this is my controller:
var mongoose = require('mongoose'),
Plant = mongoose.model('plants');
// READ ALL
exports.list_all_plants = function(req, res) {
Plant.find({}, function(err, plants) {
if (err) {
res.send(err);
}
res.json(plants);
});
};
// CREATE
exports.create_new_plant = function(req, res) {
var new_plant = new Plant(req.body);
new_plant.save(function(err, plant_inserted) {
if (err) {
res.send(err);
}
res.json(plant_inserted);
});
};
// READ (probably plantId comes from an _id previously retrieved)
exports.read_a_plant = function(req, res) {
Plant.findById(req.params.plantId, function(err, plant_searched) {
if (err) {
res.send(err);
}
res.json(plant_searched);
});
};
// UPDATE
exports.update_a_plant = function(req, res) {
Plant.findOneAndUpdate(
{
_id: req.params.plantId
},
req.body,
{new: true},
function(err, plant_to_update) {
if (err) {
res.send(err);
}
res.json(plant_to_update);
}
);
};
// DELETE
exports.delete_a_plant = function(req, res) {
Task.remove(
{
_id: req.params.plantId
},
function(err, task) {
if (err) {
res.send(err);
}
res.json({ message: 'Plant successfully deleted' });
}
);
};
And finally, i have this router:
'use strict';
module.exports = function(app) {
var plantList = require('../controllers/plantController');
// plant routes
app.route('/plants')
.get(plantList.list_all_plants)
.post(plantList.create_new_plant);
app.route('/plants/:plantId')
.get(plantList.read_a_plant)
.put(plantList.update_a_plant)
.delete(plantList.delete_a_plant);
What I'd like to do is testing all this with Postman.
If I try with the GET method, using simply
http://localhost:3000/plants
everything works fine: I mean, it returns an empty array (mongodb is up and running, and everything is set).
Now I wanted to try to insert a new element with Postman: I selected POST and x-www-form-urlencoded under body. Required properties are plantData{genusObj{genus}} and plantData{species} : since I'm quite new with both postman and mongodb, how can I enter a sub-element in postman, to create a new Plant ?
there are only KEY and VALUE options, and i don't know how to write a sub-key like plantData->genusObj->genus.
P.S.: Suggestions on data model are welcome, I tried to build a generic plant database but oriented on tulips (so usually i can enter tulips, but if i need to enter something else, i can).
Well, it seems that this answer fits to me: in fact, on Postman i selected under "body" the "raw" option, then I selected JSON instead of TEXT from the dropdown menu, and finally I used this object (meanwhile I slightly changed the
model) - don't forget the " symbols everywhere, like I did - ' is not accepted:
{
"plantData": [
{
"family": "Liliaceae",
"genusObj": {
"genus": "Tulipa",
"tulipGroup": "Single Late",
"tulipGroupNumber": 5
},
"species": "TEST",
"sellName": "Queen of night",
"description": "black tulip",
"mainImage": "",
"otherImages": "",
"images": ""
}
],
"sellingData": [
{
"price": 0.50,
"availableQuantity": 100
}
],
"detailsData": [
{
"heightInCm": "60-65",
"floweringTime": "late spring",
"plantDepthCm": "20",
"plantSpacingCm": "10",
"bulbSizeInCm": "12",
"flowerColor": "Black",
"lightRequirements": "full sun"
}
]
}

Paginate Result of related models

This is another follow up to a previous question. There are only two models involved: category and game which share a hasMany relation. Example: The endpoint /Categories/1001/games/mature list all games of fighting category that have mature flag set to true. However, I am unable to paginate the response. What is the proper way to paginate based on the code shown below? I would like to only display 10 results at a time.
common/models/category.js
Category.mature = function(id, callback) {
var app = this.app;
var Game = app.models.game;
Category.findById(id, {}, function(err, category) {
if (err) return callback(err);
//Now call the Game find method
Game.find({
"where": {
categoryId: id,
mature: true
}
}, function(err, gameArr) {
if (err) return callback(err);
callback(null, gameArr);
});
});
}
Category.remoteMethod(
'mature', {
accepts: [{
arg: 'id',
type: 'number',
required: true
}],
// mixing ':id' into the rest url allows $owner to be determined and used for access control
http: {
path: '/:id/games/mature',
verb: 'get'
},
returns: {
arg: 'games',
type: 'array'
}
}
);
Tables/Models
catgories
category_name category_id
------------- -----------
fighting 1001
racing 1002
sports 1003
games
game_id game_name category_id mature description published_date
----------- ------------ ----------- ------- ----------- --------------
13KXZ74XL8M Tekken 10001 true Published by Namco. 1994
138XZ5LPJgM Forza 10002 false Published by Microsoft 2005
API Result:
games [
{
gameName: 'Tekken',
gameInfo :
[
{
description : 'Published by Namco.',
published_date : '1994'
}
],
categorName: 'fighting',
categoryId: 1001,
mature: true
}
.....
]
If you are stuck with the code above, you will need to download the full set of Games and then paginate on the frontend. Without being able to send in limit and skip values to your queries, there is no other way.
If you can change this code and add arguments to the remote method, the underlying mysql connector format with the Node API would look like this:
Game.find({
"where": {
categoryId: id,
mature: true
},
"limit": 10, // 10 per page
"skip": 10 // hard coded for page 2, this needs to be passed in
}, function(err, gameArr) {
if (err) return callback(err);
callback(null, gameArr);
});
The values for limit and skip should be added to your remote method definition and registration, and then the UI can send in dynamic values based on the page displayed.
The page on the skip filter has an example for pagination as well:
https://docs.strongloop.com/display/public/LB/Skip+filter
If this will be using some sort of UI library like the Angular SDK, you can make the same query at the Controller level using the lb-ng generator script and the models created there. You could also add the pagination values there, no need for a custom remote method.
Update:
To add the skip and limit numbers to your remote method, you need to update your remote method signature, the accepts array would change to
accepts: [
{
arg: 'id',
type: 'number',
required: true
},
{
arg: 'skip',
type: 'number',
required: true
},
{
arg: 'limit',
type: 'number',
required: true
}
]
And then add the same 2 new arguments to the method itself:
Category.mature = function(id, skip, limit, callback) {
// ...your code...
});
You can call it using query parameters at this point, like ?skip=10&limit=10 just appended to the existing path. Or, you can change the http.path to something like
path: '/:id/games/mature/:skip/:limit'
and then call the resource with /1/games/mature/10/10 to achieve the same result with a parameterized URL.

Sails.js / Waterline: How do I use a collection attribute in a find query?

I am working on a Waterline query which filters objects by a collection attribute. In this simplified example, I have two models, Video and Category:
// Video.js
module.exports = {
attributes: {
title: {
type: 'string'
},
categories: {
collection: 'Category',
via: 'videos'
}
}
};
// Category.js
module.exports = {
attributes: {
name: {
type: 'string'
},
videos: {
collection: 'Video',
via 'categories'
}
}
};
I want to find all the videos which are associated with a certain category. I have the category id stored in a variable named categoryID and am trying this query:
Video.find('categories': categoryID).exec(function (err, videos) {
// videos should contain all videos associated with the categoryID
});
However, I always end up with an empty result, even though there are videos associated with the category for which I am looking. I know that waterline does currently not support deep queries for values in collection attributes, but I thought at least a query for the id of the object would work. Am I wrong?
If so, is there an alternative way to achieve the desired result without using native queries?
I am aware that I could add a collection attribute to Category and build my query from the Category side. However, this is only the beginning of a more complicated search where I also narrow down the result using other attributes stored in a Video object, such as the user id of the creator of a video. In the end, I iterate through the video results using pagination. So I am looking for a way to retrieve videos of certain category which can be combined with other search attributes stored in a Video object.
Modify Category.js to:
module.exports = {
attributes: {
name: {
type: 'string'
},
videos: {
collection: 'Video',
via: 'categories'
}
}
};
Modify Video.js
module.exports = {
attributes: {
title: {
type: 'string'
},
categories: {
collection: 'Category',
via: 'videos'
}
}
};
When adding a video,
var title = req.param('title');
var categories = req.param('categories').split(','); //ids of categories in format 1,3,8 etc
Video.create({name: title, categories: categories}, function(err, succ){
if(err){
return res.serverError(err);
}else {
return res.json(200, succ);
}
});
To find all videos with a specific category, use the populate() helper in waterline.
var categoryId = req.param('catId');
Category.find({id: categoryId}).populate('videos').exec(function(err, results){
if(err){
return res.serverError(err);
}else {
return res.json(200, results);
}
});
Video model :
module.exports = {
attributes: {
name: {
type: 'string'
},
categories: {
collection: 'category',
via: 'videos'
},
toJSON: function() {
var obj = this.toObject();
return obj;
}
}
};
Category model :
module.exports = {
attributes: {
name: {
type: 'string'
},
videos: {
collection: 'Video',
via: 'categories'
}
}
};
And your query will look like :
var arr = ['56667a2cbaea1fcd11c54851','56667b1053c6c37a1283ea75'];
Video.find().populate("categories",{id:arr}).exec(function(e, r) {
res.json(r);
})

Resources