Performance issue on a simple query - node.js

I have near ~10'000 records in the Image table and I want to retrieve them with a query.
The image model definition:
{
"name": "Image",
"plural": "Images",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": false
},
"uploaddate": {
"type": "date",
"required": true
},
"location": {
"type": "string"
},
"description": {
"type": "string"
},
"shootingdate": {
"type": "date"
},
"imageStatusTitle": {
"type": "string"
},
"category": {
"type": "string"
},
"canton": {
"type": "string"
},
"rights": {
"type": "boolean"
},
"earmarked": {
"type": "boolean"
}
},
"validations": [],
"relations": {
"imageStatus": {
"type": "embedsOne",
"model": "ImageStatus",
"foreignKey": "",
"options": {
"persistent": false
}
},
"categories": {
"type": "hasMany",
"model": "ImageCategory",
"foreignKey": ""
},
"votes": {
"type": "hasMany",
"model": "WebsiteUser",
"foreignKey": "imageId",
"through": "ImageVote"
},
"original": {
"type": "embedsOne",
"model": "File",
"property": "original",
"options": {
"persistent": false
}
},
"small": {
"type": "embedsOne",
"model": "File",
"property": "small",
"options": {
"persistent": true
}
},
"medium": {
"type": "embedsOne",
"model": "File",
"property": "medium",
"options": {
"persistent": true
}
},
"large": {
"type": "embedsOne",
"model": "File",
"property": "large",
"options": {
"persistent": true
}
},
"xlarge": {
"type": "embedsOne",
"model": "File",
"property": "xlarge",
"options": {
"persistent": true
}
},
"owner": {
"type": "belongsTo",
"model": "WebsiteUser",
"foreignKey": "fotographerId"
},
"widgets": {
"type": "hasMany",
"model": "Widget",
"through": "WidgetImage"
}
}
...
The query I will make is the following:
const query = {
fields: ['id', 'name', 'fotographerId'],
include: {
relation: 'owner',
fields: ['id', 'firstname', 'lastname', 'street', 'zip', 'city', 'email', 'phone', 'mobile', 'locale']
}
};
if (0 < whereFilter.length) {
query.where = {
and: whereFilter
};
}
app.models.Image.find(query, function(err, records) {
...
}
The problem is that this query takes 5 minutes to execute.
I then improve it by 4 minutes by removing the default ordering explained in this issue.
But anyways 4 minutes for such query on 10'000 document seems very slow to me. Do you see what can be the problem? Or do you have an idea how to speed up this query?

Just in case someone has the same problem. The cause of the performance issue was on a hook that execute some code every time a model was fetched/loaded. Now it takes 2 seconds to perform.

Related

hasManyThrough polymorphic relation

I have a student model,a favorite model and media models eg music,video etc. I want to implement hasManyThrough ploymorphic relation in which the through model is favorite and then stores these favorites in favorite table in my case mongoDB. Am using loopback3 and its documentation isn't clear about this topic.Any lead?
Your models would look like this:
common/models/student.json
{
"name": "Student",
"base": "User",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
},
"validations": [],
"relations": {
"favorites": {
"type": "hasMany",
"model": "Favorite",
"foreignKey": "studentId"
},
"videos": {
"type": "hasMany",
"model": "Video",
"foreignKey": "studentId",
"through": "Favorite",
"keyThrough": "favoriteId"
},
"musics": {
"type": "hasMany",
"model": "Music",
"foreignKey": "studentId",
"through": "Favorite",
"keyThrough": "favoriteId"
}
},
"acls": [],
"methods": {}
}
common/models/video.json
{
"name": "Video",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
},
"validations": [],
"relations": {
"favorites": {
"type": "hasMany",
"model": "Favorite",
"foreignKey": "videoId"
},
"students": {
"type": "hasMany",
"model": "Student",
"foreignKey": "videoId",
"through": "Favorite",
"keyThrough": "studentId"
}
},
"acls": [
],
"methods": {}
}
common/models/favorite.json
{
"name": "Favorite",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
},
"validations": [],
"relations": {
"student": {
"type": "belongsTo",
"model": "Student",
"foreignKey": "studentId"
},
"video": {
"type": "belongsTo",
"model": "Video",
"foreignKey": "videoId"
},
"music": {
"type": "belongsTo",
"model": "Music",
"foreignKey": "musicId"
}
},
"acls": [],
"methods": {}
}
Then, you simply need to POST a new Favorite item with the attributes studentId and videoId to add the new relation.
EDIT: Added music.json
common/models/music.json
{
"name": "Music",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
},
"validations": [],
"relations": {
"favorites": {
"type": "hasMany",
"model": "Favorite",
"foreignKey": "musicId"
},
"students": {
"type": "hasMany",
"model": "Student",
"foreignKey": "musicId",
"through": "Favorite",
"keyThrough": "studentId"
}
},
"acls": [
],
"methods": {}
}

Loopback relation

How to do the relation to match with an array of data in loopback
For e.g
My Models
// Regions model
{
"name": "regions",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"title": {
"type": "string",
"required": true
},
"images": {
"type": [
{
"target_id": {
"type": "string"
}
}
]
},
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
// Images model
{
"name": "images",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"url": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": { },
"acls": [],
"methods": {}
}
Expected output:
[
{
"title": "Region 1",
"field_images": [{
"name": "Image 2",
"url": "/media/image-1600x650.jpg",
},{
"name": "Image 1",
"url": "/media/image-1600x650.jpg",
}]
} ]
Assuming that the relationship is HasMany
{
"name": "regions",
"base": "PersistedModel",
...
"relations": {
"images": {
"type": "hasMany",
"model": "images",
"foreignKey": "title", //region title
}
}
...
}
A relationship is defined between two models. So, I highly doubt you would get the images inside an array (if you want to define a relationship between them).
But on loopback you can use separate APIs to retrieve the field_images instead of getting it as an array.
For example - you could retrieve the images like this
region.images([filter],
function(err, images) {
...
});

StrongLoop REST API "hasManyThrough" include filter always returns empty

Premise
Pretty short and simple. In my testing/learning environment for StrongLoop, I have set up two models: CoffeeShop and Person. Persons can have many "employers", and CoffeeShops can have many "employees". This relationship is maintained by a "hasMany"/"through" type relationship on both the CofeeShop and Person models.
Problem
When querying either model through the REST API, an include filter always returns an empty array for the associated relation, even if relations exist. In other words, the API call
http://localhost:3000/api/CoffeeShops/67/employees
works as expected, but
GET: http://localhost:3000/api/CoffeeShops/67/?filter={"include":["employees"]}
Does not.
Could anyone help me figure out why this is? I've attached pictures and the .json model definitions files as well.
// /common/models/coffee-shop.json
{
"name": "CoffeeShop",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"city": {
"type": "string",
"required": true
},
"numberEmployees": {
"type": "number"
},
"ownerId": {
"type": "number"
},
"isSmallBusiness": {
"type": "boolean"
}
},
"validations": [],
"relations": {
"owner": {
"type": "belongsTo",
"model": "Person",
"foreignKey": ""
},
"employees": {
"type": "hasMany",
"model": "Person",
"foreignKey": "employerId",
"through": "CoffeeShopPersonEmployeeEmployer"
}
},
"acls": [],
"methods": {}
}
// /omon/models/person.json
{
"name": "Person",
"plural": "Persons",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"age": {
"type": "number"
}
},
"validations": [],
"relations": {
"shops": {
"type": "hasMany",
"model": "CoffeeShop",
"foreignKey": "ownerId"
},
"employers": {
"type": "hasMany",
"model": "CoffeeShop",
"foreignKey": "employeeId",
"through": "CoffeeShopPersonEmployeeEmployer"
}
},
"acls": [],
"methods": {}
}
// /common/models/coffee-shop-person-employee-employer.json
{
"name": "CoffeeShopPersonEmployeeEmployer",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"person": {
"type": "belongsTo",
"model": "Person",
"foreignKey": "employeeId"
},
"coffeeShop": {
"type": "belongsTo",
"model": "CoffeeShop",
"foreignKey": "employerId"
}
},
"acls": [],
"methods": {}
}
Images of REST requests
in your CoffeeShopPersonEmployeeEmployer Model:
add foreign keys properties:
"properties": {
"CoffeeShopId": {
"type": "number",
"required": true
},
"PersonId": {
"type": "number",
"required": true
}
}
and ajust the relations :
"relations": {
"person": {
"type": "belongsTo",
"model": "Person",
"foreignKey": "PersonId"
},
"coffeeShop": {
"type": "belongsTo",
"model": "CoffeeShop",
"foreignKey": "CoffeeShopId"
}
}

Model Relation in Loopback framework

I want to establish a relation between two models, following the json files:
{
"name": "Surveyor",
"plural": "surveyors",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"mongodb": {
"collection": "surveyors"
},
"settings": {
"mongodb": {
"allowExtendedOperators": true
}
},
"properties": {
"name": {
"type": "string",
"required": true
},
"surname": {
"type": "string",
"required": true
},
"dateOfBirth": {
"type": "date"
},
"birthPlace": {
"type": "string"
},
"provinceOfBirth": {
"type": "string"
},
"gender": {
"type": "string"
},
"registrationNumber": {
"type": "string",
"required": true,
"unique": true
}
},
"validations": [],
"relations": {}
},
"acls": [],
"methods": {}
}
{
"name": "Dossier",
"plural": "dossiers",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"mongodb": {
"collection": "dossiers"
},
"settings": {
"mongodb": {
"allowExtendedOperators": true
}
},
"properties": {
"delCode": {
"type": "string",
"required": true
},
"client": {
"name": {
"type": "string",
"required": true
},
"surname": {
"type": "string",
"required": true
},
"taxCode": {
"type": "string"
}
},
"surveyor": {
"type": "string"
},
"examination": {
"type": "object"
},
"appointment": {
"type": "date"
},
"status": {
"type": "string",
"required": true
},
"notes": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
One dossier can have one surveyor, as key I want to use the property surveyor in the Dossier Model, corresponding to the property registrationNumber in the Surveyor Model.
I want to query the dossier Model and obtain the surveyor data in the dossier doc:
[GET] /api/dossiers
or with a filter:
[GET] /api/dossiers?filter={"include":"surveyor"}
if it is possible I want to configure the relation in the json file, not in the code.
You can use the relation generator to create that for you. In your case you're going to need to create a belongsTO relation from Dossier to Surveyor. If you want to query the other way around (dossiers from a surveyor) you're going to need to create a hasMany relation from Surveyor to Dossier.
In short, for what you've asked, you're going to end up with something like this:
In Dossier model:
...
"relations": {
"surveyor": {
"type": "belongsTo",
"model": "Surveyor",
"foreignKey": "surveyor",
"primaryKey": "registrationNumber"
}
}
...

hasMany relation loopback

My models.json:
My models.json
"masterTrip": {
"options": {
"relations": {
"trips": {
"type": "hasMany",
"model": "trip",
"foreignKey": "masterTripId"
}
}
}
},
"trip": {
"options": {
"relations": {
"masterTrips": {
"type": "belongsTo",
"model": "masterTrip",
"foreignKey": "masterTripId"
}
}
}
},
But I do not get the relation between the trip and mastertrip. can anybody explain?
There are a couple things that might be the problem. Here is what comes to mind:
You should only need the foreign key on the model that belongsTo the other one. (In this case, that would be trip.)
Have you actually created trips underneath masterTrip? (Either in the code itself or via the API?) I know this sounds silly, but the context wasn't clear enough for me to tell if you had created the sample data or not.
It sounds like you might actually be getting data when you do a GET on /masterTrip/1/trips Is that right? If so , then that sounds like the correct behavior.
I'm still relatively new to LoopBack myself, but I'm not sure that filter[include]=belongsToRelationName is the correct way to get the data you want. Technically, you are just looking for the associated array of hasMany data, right? In this case, trips that belongTo masterTrip. The RESTful way to get that would be masterTrip/{id}/trips
Hope one of those helps.
Your "belongs to" relation name is not singular. It should be singular.
When you making " belongs to " relation name is singular and for hasMany your relation name in plural. Please see the official documentation for more details -
See this working example below -
{
"name": "Booking",
"base": "PersistedModel",
"strict": false,
"idInjection": true,
"properties": {
"myuserId": {
"type": "number"
},
"orgin": {
"type": "string"
},
"orgin_lat": {
"type": "string"
},
"orgin_lon": {
"type": "string"
},
"destination": {
"type": "string"
},
"dest_lat": {
"type": "string"
},
"dest_lon": {
"type": "string"
},
"parcel_type": {
"type": "string"
},
"volume": {
"type": "string"
},
"weight": {
"type": "string"
},
"price": {
"type": "string"
},
"receiver_phn": {
"type": "string"
},
"payment_mode": {
"type": "string"
},
"booking_status": {
"type": "string"
},
"lang": {
"type": "string"
},
"booking_no": {
"type": "string"
},
"cancel_reason": {
"type": "string"
},
"cancel_by": {
"type": "string"
},
"booking_date": {
"type": "string"
},
"plan_later": {
"type": "string"
},
"plan_date": {
"type": "string"
},
"created": {
"type": "string"
},
"modified": {
"type": "string"
}
},
"validations": [],
"relations": {
"biddings": {
"type": "hasMany",
"model": "Bidding",
"foreignKey": "bookingId"
}
},
"acls": [],
"methods": {}
}
{
"name": "Bidding",
"base": "PersistedModel",
"strict": false,
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"myuserId": {
"type": "number"
},
"bookingId": {
"type": "number"
},
"booking_no": {
"type": "string"
},
"price": {
"type": "string"
},
"message": {
"type": "string"
},
"bid_date": {
"type": "string"
},
"bid_time": {
"type": "string"
},
"bid_status": {
"type": "string"
},
"lang": {
"type": "string"
},
"rate_driver": {
"type": "number"
},
"created": {
"type": "string"
},
"modified": {
"type": "string"
}
},
"validations": [],
"relations": {
"booking": {
"type": "belongsTo",
"model": "Booking",
"foreignKey": "bookingId"
},
"myuser": {
"type": "belongsTo",
"model": "Myuser",
"foreignKey": "myuserId"
}
},
"acls": [],
"methods": {}
}

Resources