How can I join models in ExtJS? - json

In ExtJS 6.02, I would like to join 2 or more models into a single store.
1 to 1 relationship:
Given users that can only be associated with a single user detail row in DB:
"users": [
{
"id": 1,
"name": "Philip J. Fry",
"userDetail": 1
},
{
"id": 2,
"name": "Hubert Farnsworth",
"userDetail": 2
},
{
"id": 3,
"name": "Turanga Leela",
"userDetail": 3
},
{
"id": 4,
"name": "Amy Wong",
"userDetail": 4
}
]
"usersDetails": [
{
"id": 1,
"email": "jfry#a.com",
"sex": "m"
},
{
"id": 2,
"email": "hubert#a.com",
"sex": "m"
},
{
"id": 3,
"email": "leela#a.com",
"sex": "f"
},
{
"id": 4,
"email": "amy#a.com",
"sex": "m"
}
]
I would like to have this on the store:
"users": [
{
"id": 1,
"name": "Philip J. Fry",
"email": "jfry#a.com",
"sex": "m"
},
{
"id": 2,
"name": "Hubert Farnsworth",
"email": "hubert#a.com",
"sex": "m"
},
{
"id": 3,
"name": "Turanga Leela",
"email": "leela#a.com",
"sex": "f"
},
{
"id": 4,
"name": "Amy Wong",
"email": "amy#a.com",
"sex": "m"
}
]
1 to many relationship:
Given users that can have multiple posts in BD:
"users": [
{
"id": 1,
"name": "Philip J. Fry",
"userDetail": 1
},
{
"id": 2,
"name": "Hubert Farnsworth",
"userDetail": 2
},
{
"id": 3,
"name": "Turanga Leela",
"userDetail": 3
},
{
"id": 4,
"name": "Amy Wong",
"userDetail": 4
}
]
"posts": [
{
"id": 1,
"user": 1,
"title": "Post 1 title",
"body": "Post 1 body"
},
{
"id": 2,
"user": 1,
"title": "Post 2 title",
"body": "Post 2 body"
},
{
"id": 3,
"user": 2,
"title": "Post 3 title",
"body": "Post 3 body"
},
{
"id": 4,
"user": 3,
"title": "Post 4 title",
"body": "Post 4 body"
}
]
I would like to have a store containing:
"items": [
{
"id": 1,
"name": "Philip J. Fry",
"posts": [
{
"id": 1,
"title": "Post 1 title",
"body": "Post 1 body"
},
{
"id": 2,
"title": "Post 2 title",
"body": "Post 2 body"
}
]
},
{
"id": 2,
"name": "Hubert Farnsworth",
"posts": [
{
"id": 3,
"user": 2,
"title": "Post 3 title",
"body": "Post 3 body"
}
]
},
{
"id": 3,
"name": "Turanga Leela",
"userDetail": 3,
"posts": [
{
"id": 4,
"user": 3,
"title": "Post 4 title",
"body": "Post 4 body"
}
]
},
{
"id": 4,
"name": "Amy Wong",
"posts": []
}
]
Many to 1 relationship:
This is actually my actual issue, I have a DB table with multiple items that each can only match a single item in another table.
Should be same as 1 to many.
Many to many relationship:
Basicly same as 1 to many I guess?
I have seem this solution: https://fiddle.sencha.com/#fiddle/1hs9&view/editor it doesn't cover everything and maybe there's a better option.

According to Sencha support:
The concept of joining records doesn't really exist within ExtJS but
you can, by making use of renderers, access and print record
association data.
I have a simple fiddle of your example above showing you how you may
achieve this. Please note that dynamic loading of association (which
you have in your example) makes using renderers a bit tricky, but not
impossible, as the loading of the association info is asynchronous in
nature and the renderer cannot wait for the response. The work around
is to refresh the entire grid when all association stores have
finished loading.
https://fiddle.sencha.com/#fiddle/3d1v&view/editor

Related

Nested json - store values in csv

I am trying to convert a nested json file into csv. It's data from a darts API and the structure is always the same. Nevertheless I got some problems flattening and storing the values in a csv because of the nested structure.
json:
{
"summaries": [{
"sport_event": {
"id": "sr:sport_event:12967512",
"start_time": "2017-11-11T13:15:00+00:00",
"start_time_confirmed": true,
"sport_event_context": {
"sport": {
"id": "sr:sport:22",
"name": "Darts"
},
"category": {
"id": "sr:category:104",
"name": "International"
},
"competition": {
"id": "sr:competition:597",
"name": "Grand Slam of Darts"
},
"season": {
"id": "sr:season:47332",
"name": "Grand Slam of Darts 2017",
"start_date": "2017-11-11",
"end_date": "2017-11-20",
"year": "2017",
"competition_id": "sr:competition:597"
},
"stage": {
"order": 1,
"type": "league",
"phase": "stage_1",
"start_date": "2017-11-11",
"end_date": "2017-11-15",
"year": "2017"
},
"round": {
"number": 1
},
"groups": [{
"id": "sr:league:29766",
"name": "Grand Slam of Darts 2017, Group G",
"group_name": "G"
}]
},
"coverage": {
"live": true
},
"competitors": [{
"id": "sr:competitor:35936",
"name": "Smith, Michael",
"abbreviation": "SMI",
"qualifier": "home"
}, {
"id": "sr:competitor:83895",
"name": "Wilson, James",
"abbreviation": "WIL",
"qualifier": "away"
}]
},
"sport_event_status": {
"status": "closed",
"match_status": "ended",
"home_score": 5,
"away_score": 3,
"winner_id": "sr:competitor:35936"
}
}, {
"sport_event": {
"id": "sr:sport_event:12967508",
"start_time": "2017-11-11T13:40:00+00:00",
"start_time_confirmed": true,
"sport_event_context": {
"sport": {
"id": "sr:sport:22",
"name": "Darts"
},
"category": {
"id": "sr:category:104",
"name": "International"
},
"competition": {
"id": "sr:competition:597",
"name": "Grand Slam of Darts"
},
"season": {
"id": "sr:season:47332",
"name": "Grand Slam of Darts 2017",
"start_date": "2017-11-11",
"end_date": "2017-11-20",
"year": "2017",
"competition_id": "sr:competition:597"
},
"stage": {
"order": 1,
"type": "league",
"phase": "stage_1",
"start_date": "2017-11-11",
"end_date": "2017-11-15",
"year": "2017"
},
"round": {
"number": 1
},
"groups": [{
"id": "sr:league:29764",
"name": "Grand Slam of Darts 2017, Group F",
"group_name": "F"
}]
},
"coverage": {
"live": true
},
"competitors": [{
"id": "sr:competitor:70916",
"name": "Bunting, Stephen",
"abbreviation": "BUN",
"qualifier": "home"
}, {
"id": "sr:competitor:191262",
"name": "de Zwaan, Jeffrey",
"abbreviation": "DEZ",
"qualifier": "away"
}]
},
"sport_event_status": {
"status": "closed",
"match_status": "ended",
"home_score": 5,
"away_score": 4,
"winner_id": "sr:competitor:70916"
}
}
So for each sport_event I would like to store the variables:
"start_time"
from "season" the variable "name"
from "competitors" both "id" and "name"
from "sport_event_status" the "winner_id"
I have already tried to flatten the json file with this code:
import json
f = open(r'path of file.json')
data = json.load(f)
def flatten(data):
for key,value in data.items():
print (str(key)+'->'+str(value))
if type(value) == type(dict()):
flatten(value)
elif type(value) == type(list()):
for val in value:
if type(val) == type(str()):
pass
elif type(val) == type(list()):
pass
else:
flatten(val)
flatten(data)
print(data)
This actually prints out the following:
id->sr:season:47332
name->Grand Slam of Darts 2017
start_date->2017-11-11
end_date->2017-11-20
year->2017
competition_id->sr:competition:597
Now my question is how to store the values I mentioned above in a csv file.
Thanks in advance for your support.
Using jq, you basically just have to transcribe your specification, adding a bit of context and taking care of an embedded array:
.summaries[]
| .sport_event # Your specification:
| [.start_time, # start_time
.sport_event_context.season.name] # from "season" the variable "name"
+ [.competitors[] | .id, .name] # from "competitors" both "id" and "name"
+ [.sport_event_status.winner_id] # from "sport_event_status" the "winner_id"
| #csv
Invocation
E.g.
jq -rf program.jq my.json

How to set alias instead of id , i need category_id without using forloop

$categoryData = $this->compCatObj->find()->select(['CompanyCategory.id', 'CompanyCategory.name','CompanyCategory.restricted'])->contain(['CompanyItems'=>['fields'=>['CompanyItems.id','CompanyItems.company_category_id','CompanyItems.name']]])->toArray();
Response
{
"status": "success",
"message": "List of company categories",
"data": [
{
"id": 1,
"name": "Breakfast gdfgedfgdf",
"restricted": "no",
"company_items": []
},
{
"id": 2,
"name": "Breakfast",
"restricted": "yes",
"company_items": []
}
]
}
i need category_id instead of id. Is there any way to do this without using forloop.
yes you can do this by a small change in your code,
$categoryData = $this->compCatObj->find()->select(['category_id' => 'CompanyCategory.id', 'CompanyCategory.name','CompanyCategory.restricted'])->contain(['CompanyItems'=>['fields'=>['CompanyItems.id','CompanyItems.company_category_id','CompanyItems.name']]])->toArray();
Response
{
"status": "success",
"message": "List of company categories",
"data": [
{
"category_id": 1,
"name": "Breakfast gdfgedfgdf",
"restricted": "no",
"company_items": []
},
{
"category_id": 2,
"name": "Breakfast",
"restricted": "yes",
"company_items": []
}
]
}

JSON Transformation using JQ

I am trying to transform my JSON to different structure using JQ. I am able to partially achieve my new structure, however i get a additional blocks of data.
As i iterate i am able to get the structure in new format. But additional structures are coming.
Code Snippet- https://jqplay.org/s/3gulSlZiWz
JSON
{
"amazon": {
"activeitem": 2,
"createdDate": "2019-01-15T17:36:31.588Z",
"lastModifiedDate": "2019-01-15T17:36:31.588Z",
"user": "net",
"userType": "new",
"items": [
{
"id": 1,
"name": "harry potter",
"state": "sold",
"type": {
"branded": false,
"description": "artwork",
"contentLevel": "season"
}
},
{
"id": 2,
"name": "adidas shoes",
"state": "in inventory",
"type": {
"branded": false,
"description": "Spprts",
"contentLevel": "season"
}
},
{
"id": 3,
"name": "watch",
"state": "returned",
"type": {
"branded": false,
"description": "walking",
"contentLevel": "special"
}
},
{
"id": 4,
"name": "adidas shoes",
"state": "in inventory",
"type": {
"branded": false,
"description": "running",
"contentLevel": "winter"
}
}
],
"product": {
"id": 4,
"name": "adidas shoes",
"source": "dealer",
"destination": "resident"
}
}
}
JQ Query:
.amazon|
{
userType: .userType,
userName: .user,
itemCatalog: {
itemId: .items[].id,
name: .items[].name
}
}
Expected Response:
{
"userType": "new",
"userName": "net",
"itemCatalog": {
"itemId": 1,
"name": "harry potter"
},{
"itemId": 2,
"name": "adidas shoes"
}, {
"itemId": 3,
"name": "watch"
},{
"itemId": 4,
"name": "adidas shoes"
}
}
But getting something weird long duplicated response.
As already pointed out in a comment, the "expected response" is not JSON and probably not what you want anyway. The following would make sense and in any case illustrates how to iterate appropriately:
.amazon
| { userType: .userType,
userName: .user,
itemCatalog: (.items | map({ itemId: .id, name} ))
}
Output
{
"userType": "new",
"userName": "net",
"itemCatalog": [
{
"itemId": 1,
"name": "harry potter"
},
{
"itemId": 2,
"name": "adidas shoes"
},
{
"itemId": 3,
"name": "watch"
},
{
"itemId": 4,
"name": "adidas shoes"
}
]
}

Fetch data from two table in nodejs from mysql in rest Api

js Developers
I am trying to create Rest api in node.js . I want to get data from two table in mysql database. First table is USERS which contain {id, name, email} and Second table is USERINFO which contain {id, mobile , address, user_id} here user_id is work as foreign key that is associate with USERS table. In USERINFO table more than one information.
First table Look like this
Second Table look like this
I got this type of output
[
{
"id": 1,
"name": "abc",
"email": "abc#xxx.com",
"mobile": "11111",
"address": "qqq",
"user_id": 1
},
{
"id": 2,
"name": "XYZ",
"email": "xyz#xxx.com",
"mobile": "22222",
"address": "wwwww",
"user_id": 2
},
{
"id": 3,
"name": "abc",
"email": "abc#xxx.com",
"mobile": "3333333",
"address": "ssssss",
"user_id": 1
},
{
"id": 4,
"name": "XYZ",
"email": "xyz#xxx.com",
"mobile": "444444",
"address": "nnnnn",
"user_id": 2
}]
But i want this type output
[
{
"id": 1,
"name": "abc",
"email": "abc#xxx.com",
"info": [
{
"mobile": "11111",
"address": "qqq",
},
{
"mobile": "3333333",
"address": "ssssss",
}
],
"user_id": 1
},
{
"id": 2,
"name": "XYZ",
"email": "xyz#xxx.com",
"info": [
{
"mobile": "22222",
"address": "wwwww",
},
{
"mobile": "444444",
"address": "nnnnn",
}
],
"user_id": 2
}]
Thanks in Advance

API endpoints with json-server

I'm using json-server to create a test API. I'd like to have make the API endpoint: http://localhost:3000/posts/1/comments
/posts and /posts/1 works fine but I'm not sure why /posts/1/comments doesn't. Am I missing something?
Here is the json
{
"posts": [
{
"id": 1,
"title": "Post 1",
"comments": [
{
"id": 1,
"body": "This is comment 1"
}
]
},
{
"id": 2,
"title": "Post 2",
"comments": []
}
]
}
The comments are a separate object referenced by postId, as follows:
{
"posts": [
{
"id": 1,
"title": "json-server",
"author": "typicode"
}
],
"comments": [
{
"id": 1,
"body": "some comment",
"postId": 1
},
{
"id": 2,
"body": "this is another",
"postId": 1
}
],
"profile": {
"name": "typicode"
}
}
So you can do this :
localhost/posts/1
{
id: 1,
title: "json-server",
author: "typicode"
}
localhost/posts/1/comments
[
{
id: 1,
body: "some comment",
postId: 1
},
{
id: 2,
body: "this is another",
postId: 1
}
]
http://jsonplaceholder.typicode.com/comments