How to perform Model Joins + Condition on Relations in Sails.js? - sails.js

How can I make a model join query(condition) and sort on relation models on Sails?
Example: I have 4 tables(collections in mongodb) and 4 related models in mongodb:
User: user_id, name
Follow: user_id, following_id (user id is being followed)
Point: user_id, point
Post: name, content, user_id, created_at
So from the post table, I want to make a query to find the posts of users that I'm following and sort by their point. Like this raw sql:
SELECT post.* FROM post
LEFT JOIN user_point up ON up.user_id = post.user_id
WHERE post.user_id IN (1,2,3,4) // assume I got my following_user_ids result is 1,2,3,4 for this case so no need to join follow table
ORDER BY up.point DESC // high point then first return
I don't know how can do this by Sails model? I have read many instructions by got no helps. Almost people said: Sails Association, but it just helps return the relation instead of do the where or order by to sort original model results(is this case: post).
I have worked with Yii2, a PHP framework so with this case I can do it easily:
Post::model()->leftJoin('user_point up', 'up.user_id = post.user_id')->where(['post.user_id' => [1,2,3,4])->orderBy(['up.point' => SORT_DESC])->all();
I'm stucked in Sails, very thanks if someone help me!!!

Because you're using Mongo, and because you need the full power of normal JOIN's, you will probably be forced to use some other ORM solution (i.e. mongodb package on npm) for queries like that.
Why? See the API documentation for sendNativeQuery(), which states native query features are only available for SQL-like DBMS's.

Related

How to design MongoDB collection relation correctly

I am trying to create a simple DB of Customers/Products/Orders to practice my skills with aggregate and so on. I need some help on how to design the relation between those collections correctly (that i'll really be able to store the details in the right way).
I go for the minimal build and information-->
Customer:
(1) customer_id ,
(2)orders (this will be an array with all the order_id's this customer did)
Product:
(1) product_id
Order:
(1) order_id ,
(2) products (this will be an array with all the products in this order) + I wonder how I add quantity for each product ,
(3) user_id (this will store the user_id did this order) or i don't even need this field??
I hope someone could tell me if this is a right thinking, because I come from SQL and this what I would do there I guess
// I'm not using embedded document because in case i'll want to change a specific field value it would change in all places

How to fetch data from multi tables in nosql using SEMBAST plugin

I am not able to fetch data when multi table which are having relationship . so can any one help as per the below image
finally i need data like
ORDER_ID, USER_FULL_NAME, PRODUCT_NAME, PRODUCT_PRICE from 3 different table .
please help me out.
Sembast, does not provide a way to query multiple stores in one join query. However getting an entity by id (here a user or a product) is almost immediate (store.record(id).get(db) or store.records(ids).get(db)) so I think your best bet is to query the order_items store and fetch the users and products by ids.
Basically using 3 requests in a transaction to ensure data integrity should perform the join you want.

Does Group by with order by work ion Dql?

I am trying to execute a DQL (Doctrine) query that retrieve the latest answers of different doctors.
we have table answer, member, Location (doctor table) and many other table connected to the doctor information. However I want to get the latest answer but the condition is one answer for a doctor. I do some search and know something about group by and order by dont work together !
Here is the query in DQL:
$query = Doctrine_Query::create()
->select("a.answer_id as answer_id,m.member_is as member_id,a.member_id")
->from('Answer a')
->leftJoin('a.Member m on m.member_id = a.member_id')
->orderBy('a.date_added DESC')
->groupBy('m.member_id')
->limit(5);
but this query return undesirable results. Can anyone tell me where is the mistake?
Unfortunately you need to perform a subquery for that, which according to this question, DQL does not support. However in that question it's suggested that you send a native query.

Linq to Entities query to select latest, distinct records

I'm trying to retrieve records ordered by a DateTime with Linq to entities, but I need them as distinct records as well.
My table design looks like this:
Where blogItemNodeId is an Umbraco CMS node (the blog item node to which a comment can be created)
The requirement is that a list of blog items can be sorted by how many comments they have. So I need to get the latest comments distinct by blogItemNodeId to display those blog items.
My current linq query looks like this:
var distinctComments = (from c in ctx.BlogComments
orderby c.date
group c by c.blogItemNodeId
into uniqueRecords
select uniqueRecords.FirstOrDefault());
The problem with this query is that it finds the first (hence FirstOrDefault()) record with distinct blogItemNodeId where it should find the one which is newest of all comments with the same blogItemNodeId.
Does anyone know how to achieve this? :-)
Thanks a lot in advance.
EDIT
I managed to get it to work by doing this:
var allComments = (from c in ctx.BlogComments
orderby c.date descending
select c).ToList();
var distinctComments = allComments.GroupBy(x => x.blogItemNodeId).Select(y => y.FirstOrDefault());
But then I have to get all the comments before doing the group which is not very elegant nor performant.
Any help is greatly appreciated!
You could combine both the orderBy statement and the GroupBy statement into one single statement like this:
var distinctComments = ctx.BlogComments.OrderBy(c => c.date)
.GroupBy(x => x.blogItemNodeId)
.Select(y => y.FirstOrDefault());
If the performance is bad, you could cache your DataContext in the HttpCache through a Singleton. See this blogpost for an example. This is based on the Umbraco DataContext but can be easily modified for use with another type of context.

products and configurable_products in postgresql

I have a Product table and a ConfigurableProduct table.
If there are several variations of the same product like a shirt in different colors I create a ConfigurableProduct.
When a user is looking at the catalog he should see a list of products unless there is a ConfigurableProduct, then he should see it with a select box for each variations.
How do I structure the tables for Product and ConfigurableProduct and how do I query the db so I can page through the results?
Thanks
I am going to answer this as if you do not have tables created. I am not sure if that is true though.
The following is a simple example, but I assume you have more data.
products
id
name
configurable_products
id
variation
product_id REFERENCES products(id)
You can just make the configurable products a reference to products.
If you want a listing of products with their configurations then you can do:
select p.name, c.variation
from products p left outer join configurable_products c
on (p.id = c.product_id);
Of course you can just search for all the configurable_products based on the product id too when needed.
As for the paging part of your question you will have to clarify what you mean. You can use limit to limit results if you don't want to get everything at once.