Relationship with json-server - json

I'm building a fake API with json-server and I came across a problem when passing a query. I can show the parameters of a relationship when I use _expand, but when it is a relationship within a child it does not work.
For example, I have this json that I access by passinghttp://localhost:3000/schedules:
{
id: 1,
date: "2020-04-25T14:20:00-03:00",
status: "Novo serviço",
usersId: 5,
providersId: 1,
servicesId: 1,
},
Now to show the relationship with the user I pass the following query:
http://localhost:3000/schedules?_expand=users
It returns as follows:
{
id: 1,
date: "2020-04-25T14:20:00-03:00",
status: "Novo serviço",
usersId: 5,
providersId: 1,
servicesId: 1,
users: {
id: 5,
name: "Carla Pedroso",
photo: "https://cdn.pixabay.com/photo/2016/01/19/17/48/woman-1149911_960_720.jpg",
rating: 5,
addressesId: 1,
},
},
Well, my question is how I can show the JSON of the addressesId, because I already tried using_expand but without success.

change the schedule property "usersId" to "userId",
should work with
http://localhost:3000/schedules?_expand=user
and the response should be something like that:
{
id: 1,
date: "2020-04-25T14:20:00-03:00",
status: "Novo serviço",
providersId: 1,
servicesId: 1,
userId: 5,
user: {
id: 5,
name: "Carla Pedroso",
photo: "https://cdn.pixabay.com/photo/2016/01/19/17/48/woman-1149911_960_720.jpg",
rating: 5,
addressesId: 1
}
}

With json-server you can add the parent and children of an element to the result, but it will only bring one level, so it will not bring grandparents or grandchildren's.
In your case instead of looking at the schedules you can look at the users and ask to expand the addresses and embed the schedules
http://localhost:3000/users?_expand=addresses&_embed=schedules

Related

Angularjs filter through objectarray of objects

I want to filter an array of objects by another object, and show them if they have as a property either one of the second object's entries.
My array of objects to filter:
accounts = [
0: {id: 24, name: 'name24', company: company1, location:location1, ...}
1: {id: 25, name: 'name25', company: company2, location:location1, ...}
2: {id: 26, name: 'name26', company: company1, location:location2, ...}
3: {id: 27, name: 'name27', company: company3, location:location1, ...}
4: {id: 28, name: 'name28', company: company2, location:location3, ...}
..................................................
]
My filtering object:
companies = {
company1: 1,
company2: 0,
company3: 1,
............
}
In my companies object, I have all the companies with a value of 1 for selected and 0 for not selected.
I already have a filter for the location, but data.locations contains only one entry.
<div class="description">
<div ng-repeat="accounts in data.accounts | filter:{location_id: data.location.id">....</div>
</div>
More clearly, I want to filter and get the accounts that belong to companies which have the value of 1 in my companies object if it's possible with a method like the one I use for filtering the location, without creating a custom filter.

Accessing an included object

I have the following JSON Object, which is the result of a loopback model (Classifications), with a relationship with another model (Labels).
My call to get the classifications is:
modClassification.findOne({
where: { id: classificationid },
include: 'labels' },
function( err, classification ){ ...
And this returns classification with something like
{ id: 'b01',
title: 'population',
country_id: 1,
labels:
[ { column_id: 'b1',
classification_id: 'b01',
classification_title: 'population',
dsporder: 1,
label: 'Total_Persons_Males',
country_id: 1,
id: 1 },
{ column_id: 'b2',
classification_id: 'b01',
classification_title: 'population',
dsporder: 2,
label: 'Total_Persons_Females',
country_id: 1,
id: 2 } ] }
which is what I would expect.
I now need to loop over the labels and access it's properties, but this is where I am stuck.
classification.labels[0] = undefined..
I have tried looping, each and whatever I can find online, but can't seem to get to each labels properties.
Can someone tell me what I am doing wrong/need to do?
Thanks
When you are including related models inside a findOne call, you need to JSONify the result before accessing the related records:
classification = classification.toJSON()
Then you should be able to access the included label items as you expect.
See https://docs.strongloop.com/display/public/LB/Include+filter, specifically the "Access included objects" section.
Note this does not work the same when you retrieve more than one result in an array. In that case you'll need to perform toJSON() on each item in the array.

ActiveRecord shows the same record with different attributes depending on how it is referenced

I'm debugging some of my Rspec tests and have run into something very strange in my DB. Basically I have two models, Invited and Group. Invited belongs_to a Group.
So I have two different variables that are referencing the same Invited record. Theoretically, those two variables should belong_to the same Group record. And indeed, all the ID's in my records all match, except for a single attribute in the Group record is mismatched. Here is some console output demo:
[54] pry> invited
=> #<Invited id: 1, email: "purpledinosaur#yahoo.com", confirmation_token: "3460b1f237f0e7cb97eb3a325bd79cbdb0696405", subscribed: false, groups_training_level_id: 1, group_id: 1, created_at: "2015-12-20 01:47:03", updated_at: "2015-12-20 01:47:03">
[55] pry> inv
=> #<Invited id: 1, email: "purpledinosaur#yahoo.com", confirmation_token: "3460b1f237f0e7cb97eb3a325bd79cbdb0696405", subscribed: false, groups_training_level_id: 1, group_id: 1, created_at: "2015-12-20 01:47:03", updated_at: "2015-12-20 01:47:03">
[56] pry> Group.all
=> [#<Group id: 1, name: "Brandi", start_at: "2015-12-19 05:00:00", expire_at: "2015-12-27 04:59:59", program_id: 3, speciality_id: 3, created_at: "2015-12-20 01:47:02", updated_at: "2015-12-20 01:47:02">]
[57] pry> invited.group
=> #<Group id: 1, name: "Brandi", start_at: "2015-12-19 05:00:00", expire_at: "2015-12-27 04:59:59", program_id: 2, speciality_id: 3, created_at: "2015-12-20 01:47:02", updated_at: "2015-12-20 01:47:02">
[58] pry> inv.group
=> #<Group id: 1, name: "Brandi", start_at: "2015-12-19 05:00:00", expire_at: "2015-12-27 04:59:59", program_id: 3, speciality_id: 3, created_at: "2015-12-20 01:47:02", updated_at: "2015-12-20 01:47:02">
[59] pry> Group.where(program_id: 2)
=> []
As you can see, invited and inv are both referencing the same record. Only one Group record exists, with id: 1. When I do invited.group and inv.group, the Group with the right id shows up, but the program_ids are different! I am perplexed by this, and I'm hoping someone can shed some light here. I am happy to provide more info, please do not hesitate to ask.
I am on Rails 3.2 and mysql2 (0.3.18).
Edit to include more info:
I can reproduce this consistently, although the steps are a bit complex. invited is generated by FactoryGirl:
let!(:invited) { FactoryGirl.create(:invited, email: sub.email, group: group) }
sub references a model that contains an email. inv is later retrieved using ActiveRecord like this:
inv = Invited.includes(group: :program).where(email: sub.email)[0]
Sure enough, Group.count returns 1.
(It is a bit hard to answer this question without more information but I'll give it a go anyway.)
Clearly the program_id is different in inv and invited. The only way this could happen as far as I can tell is that some code is actually changing either the one in memory invited or the content in the database that later turn up as inv. The object you have in memory will have whatever content it had when you loaded it into memory plus whatever state you have changed on it since it was loaded. This will often be different from what is in the database.
I would have a look at invited right after it is created and also look for changes to group instances. Maybe you are changing a group reference somewhere in the test. Or you have some hooks that alters the model (a common source of frustration when it comes to rails).

How to access particular JSON data in this case?

I know i can retrieve total units like this in python using simplejson.
jsondata = json.loads(r.text)
jsondata['data']['total_units']
But how do i go on and fetch data of status field?
{
status: 'ok',
data: {
total_units: 1,
unit_info: [{
type: 'car',
status: 'Blue car',
id: '20513'
}]
}
}
jsondata['data']['unit_info'][0]['status']
You need to index the list in the 'unit_info' key in order to get at the dict inside it.

How would I store this data in redis properly?

Collection: years
Entry 1
{
_id: ObjectId("4f2fd2ee4516ed6ceebbe17c"),
shows: [
{
day: 1,
hasVideos: false,
month: 1,
showid: 1252187000,
venue: "Someplace"
},
{
day: 2,
hasVideos: false,
month: 2,
showid: 1252188010,
venue: "Philly"
}
],
year: 1988
}
Entry 2
{
_id: ObjectId("4f2fd2ee4516ed6ceebbe17c"),
shows: [
{
day: 19,
hasVideos: false,
month: 1,
showid: 1252187999,
venue: "Hunt's"
},
{
day: 21,
hasVideos: false,
month: 1,
showid: 1252188030,
venue: "Hunt's"
}
],
year: 1987
}
Now, I created a set called years in my redis store. This is populated with strings in the form of year:1988. I created a hash called year:1988. But this is wrong. I want to nest an array of these show hashes.
How can nest a hash two sets deep? Should I just continue the pattern and create a set in the form of 1988:01:01 and 1988:02:02 to store the first entry?
Thanks
If you only want to be able to query by date (or year) you should use a Sorted Set as lookup.
Sorted Sets on redis.io
What you want to do is use the show's Id or the serialized show as the member and the date the show aired, or took place as the score.
Note that if you only store the shows Id you need to perform another lookup to get the shows' details.
Now you can perform range queries which seems to be what you want:
ZRANGEBYSCORE
ZREVRANGEBYSCORE