How do I return a hash with relational data using parse.com? - parse-platform

I have a Post class and I want to get the products associated to it. I'd like to write a single query that fetches all of the posts with the products associated to them. Below is a rough idea of what I'd like the hash to looks like.
Schema:
Post - objectId, title
PostItem - post(pointer), product(pointer)
Product - objectId, title, price
Output I want
{'results':
[{'objectId':'blah', 'title':'The Post',
'products':
[{'objectId':'14',
'title':'First product',
'price':'20'
},
{'objectId':'26',
'title':'Second product',
'price':'55'
}]
},
{'objectId':'blah2', 'title':'Second post',
[{'objectId':'38',
'title':'Some product',
'price':'54'
},
{'objectId':'26',
'title':'Another Product',
'price':'88'
}]
}]
}
Is this possible to do in one query? How would I do this using curl and parse.com?

If this is a many-to-many relationship, use a Parse relationship.

You'll want to use the include key option alongside pointers. Since it's been nearly a year since you posted, I assume you figured it out, but lurkers can read more about this (in Javascript) here.

Related

Is there a way to create multiple relation at once in hygraph?

I am currently using hygraph as CMS. I have two models, posts and tags. I created many to many relation between them. It works when I use the inbuilt content editor.
The problem is that I cannot do the same with API. I can only create one relation at a time.
So is there a way to create multiple relation at once via API?
I figured this out by hit and trial.
mutation MyMutation {
createContent(
data: {title: "ABC", genres: {connect: [{Genre: {type: "travel"}},{Genre: {type: "food"}}]}}
) {
id
}
}
You have to pass array of individual genres to connect.

Elasticsearch: document relationship

I'm doing a elastic search autocomplete-as-you-type
I'm using cool features like ngram's and other stuff to create needed analyzer.
currently I break my had around indexing following data.
Let say I have Payments type,
each document in this type looks like this
{
..elastic meta data..
paymentId: 123453425342,
providerAccount : {
id: 123456
firstName: Alex,
lastName: Web
},
consumerAccount : {
id: 7575757,
firstName: John,
lastName: Doe
},
amount: 556,
date : 342523454235345 (some unix timestamp)
}
so basically this document represents not only the payment itself but it also shows the relationship of the payment, the 2 entities which related to the payment.
Payment always have its provider and consumer.
I need this data in payment document because I want to show it in UI.
By indexing it like so, it might be a big pain for handling the updates of Consumer or Provider because each time some of them change its properties I have to update all the payments which has this entity.
Another possible solution is to store only id's of this consumers/providers and make a query on payments and then 2 queries for the entities for retrieving needed fields, but i'm not sure about this because i'm doing ajax requests each time a character entered, so here comes the performance question.
I have also looked into parent/child relationship solution which basically fits my case but I wasn't able to figure out if I can retrieve also the parent(consumer/provider) fields while I querying child(payment).
What would you suggest?
Thanks!
Yes, you can retrieve the parent while querying child using has_child.
Considering payment as child and consumer as parent, You can search all the consumers by :
GET /index_name/consumer/_search
{
"query": {
"has_child": {
"type": "payment",
"query": {
// any query on payment table
},
"inner_hits": {}
}
}
}
This would fetch you all the consumer based on the query on child i.e payment in your case.
inner_hits is what you are looking for. This will retrieve you the children as well. But it was introduced in elasticsearch 1.5.0. So version should be greater than elasticsearch 1.5.0.
You can refer https://www.elastic.co/blog/elasticsearch-1-5-0-released.
Your problem is not an issue. I suppose you want tot freeze data after the pay, right? So you don't need to update the accounts data in existing payment documents.
Further: parent/schild is easy for updating, but less efficient with querying. For auto complete, stay using your current mapping!

Why does Eloquent change relationship names on toArray call?

I'm encountering an annoying problem with Laravel and I'm hoping someone knows a way to override it...
This is for a system that allows sales reps to see inventory in their territories. I'm building an editor to allow our sales manager to go in and update the store ACL so he can manage his reps.
I have two related models:
class Store extends Eloquent {
public function StoreACLEntries()
{
return $this->hasMany("StoreACLEntry", "store_id");
}
}
class StoreACLEntry extends Eloquent {
public function Store()
{
return $this->belongsTo("Store");
}
}
The idea here is that a Store can have many entries in the ACL table.
The problem is this: I built a page which interacts with the server via AJAX. The manager can search in a variety of different ways and see the stores and the current restrictions for each from the ACL. My controller performs the search and returns the data (via AJAX) like this:
$stores = Store::where("searchCondition", "=", "whatever")
->with("StoreACLEntries")
->get();
return Response::json(array('stores' => $stores->toArray()));
The response that the client receives looks like this:
{
id: "some ID value",
store_ac_lentries: [
created_at: "2014-10-14 08:13:20"
field: "salesrep"
id: "1"
store_id: "5152-USA"
updated_at: "2014-10-14 08:13:20"
value: "salesrep ID value"
]
}
The problem is with the way the StoreACLEntries name is mutilated: it becomes store_ac_lentries. I've done a little digging and discovered it's the toArray method that's inserting those funky underscores.
So I have two questions: "why?" and "how do I stop it from doing that?"
It has something in common with automatic changing camelCase into snake_case. You should try to change your function name from StoreACLEntries to storeaclentries (lowercase) to remove this effect.

How can I get particular fields from a shopify gem api query

I want to only return certain fields in query.
For example:
ShopifyAPI::Metafield.find(:all,:params=>{:resource => "products"},:fields=>['value'])
I only want to return the value field from the metafield object
You came close. You forgot to actually provide an ID for a resource. In your case, since you're looking for all the values of the metafields assigned to a product, you need to provide the product ID.
ShopifyAPI::Metafield.all(params: {resource: 'products', resource_id: 149523945, fields: 'value'})
=> [
#<ShopifyAPI::Metafield:0x007f80041f3958 #attributes={"value"=>"4999"}, #prefix_options={:resource=>"products", :resource_id=>149523945}, #persisted=true>,
#<ShopifyAPI::Metafield:0x007f80041f36b0 #attributes={"value"=>"Céà"}, #prefix_options={:resource=>"products", :resource_id=>149523945}, #persisted=true>,
#<ShopifyAPI::Metafield:0x007f80041f3408 #attributes={"value"=>"Cíao"}, #prefix_options={:resource=>"products", :resource_id=>149523945}, #persisted=true>,
#<ShopifyAPI::Metafield:0x007f80041f3160 #attributes={"value"=>"1"}, #prefix_options={:resource=>"products", :resource_id=>149523945}, #persisted=true>
]

LINQ to SQL: Selecting a one-to-many relationship

I've got two tables (articles and tags) that have a one-to-many relationship.
I remember seeing somewhere that I can create nested objects/arrays/etc from that relationship. The output of which I would like in a format such as (in JSON format):
{1 : {
id : 1,
title : 'article title',
tags : ({
id : 16,
tagname : 'first tag'
},{
id : 23,
tagname : 'another tag'
})
}
I'm creating an internal knowledge base system with a list view similar to the front page of stack overflow (that's the way I'd like to display the tags).
How would I go about doing this? A subquery? Thanks.
If I understand your question correctly you're looking for the SelectMany function which lets you 'unroll' nested structures like yours. SelectMany is indeed executed in Query syntax by a sub-query:
IEnumerable<Tag> =
from article in Articles
from tag in article.Tags
select tag;
In method form:
IEnumerable<Tag> tags = Articles.SelectMany(a => a.Tags);

Resources