Issu normalizing data ( entity with children of same entity ) - normalizr

i'm very new to normalizr.
I'm trying to normalize a json api answer that looks like this: http://myjson.com/15st3f
There are some nested elements of same entity. For exemple:
{
"id": 1,
"name": "a",
[...]
"children": [
"id": 2,
"name": "b",
[...]
"children": [
"id": 3,
"name": "c"
]
]
}
How should I start this? I'm a bit confuse. Can it be normalized? Can you have array of same entity, or something like that ? Can I do something like this ? :
const uor = new schema.Entity('uors', {
UorChildren: [ uor ]
})

Use the define instance method:
const uor = new schema.Entity('uors');
uor.define({ children: [ uor ] });

Related

How to cleanly batch queries together in Gremlin

I am writing a GraphQL resolver that retrieves all vertices by a particular edge using the following query (created returns label person):
software {
created {
name
}
}
Which would resolve to the following Gremlin Query for each software node found:
g.V().hasLabel('software').has('name', 'ripple').in('created')
This returns a result that includes all properties of the object:
{
"result": [
{
"#type": "d",
"#rid": "#24:0",
"#version": 6,
"#class": "person",
"in_knows": [
"#35:0"
],
"name": "josh",
"out_created": [
"#32:0",
"#33:0"
],
"age": 32,
"#fieldTypes": "in_knows=g,out_created=g"
}
],
"dbStats": {
...
}
}
I realize that this will fall foul on GraphQL's N+1 query so i'm trying to batch queries together using a Dataloader pattern. (i'm also hoping to do property selections, so i'm not asking the database to return too much info)
So i'm trying to craft a query like so:
g.V().union(
__.hasLabel('software').has('name', 'ripple').
project('parent', 'child').by('id').
by(__.in('created').fold()),
__.hasLabel('software').has('name', 'lop').
project('parent', 'child').by('id').
by(__.in('created').fold())
)
But this results in the following where the props are missing and it just includes the id of the vertices I want:
{
"result": [
{
"parent": "ripple",
"child": [
"#24:0"
]
},
{
"parent": "lop",
"child": [
"#22:0",
"#23:0",
"#24:0"
]
}
],
"dbStats": {
...
}
}
My Question is, how can I have the Gremlin query return all of the props for the found vertices and none of the other props? Should I even been doing batching this way?
For anyone else reading, the query I was trying to write wouldn't work because the TraversalSet created in the .by(_.in('created') can't be cast from a List to an ElementMap as the stream cardinality wouldn't be enforced. (You can only have one record per row, I think?)
My working query would be to duplicate the keys for each row and specify the props needed (the query below is ok for gremlin 3.3 as used in ODB, otherwise if you've got < gremlin 3.4 replace the last by step with be(elementMap('name', 'age')):
g.V().union(
__.hasLabel('software').has('name', 'ripple').
as('parent').
in('created').as('child').
select('parent', 'child').
by(values('name')).
by(properties('id', 'name', 'age').
group().by(__.key()).
by(__.value())),
__.hasLabel('software').has('name', 'lop').
as('parent').
in('created').as('child').
select('parent', 'child').
by(values('name')).
by(properties('id', 'name', 'age').
group().by(__.key()).
by(__.value()))
)
So that you get a result like this:
{"data": [
{
"parent": "ripple",
"child": {
"id": 5717,
"name": "josh",
"age": 32
}
},
{
"parent": "lop",
"child": {
"id": 5709,
"name": "peter",
"age": 35
}
},
{
"parent": "lop",
"child": {
"id": 5713,
"name": "marko",
"age": 29
}
},
{
"parent": "lop",
"child": {
"id": 5717,
"name": "josh",
"age": 32
}
}
]
}
Which would allow you to create a lookup where you concat all results for "lop" and "ripple" into arrays.

Parsing list in ramda

I am working on a project in which i am getting a getting data from backend via
api and i need to parse in using ramda. i am not sure how to do it .can anyone help me parse both the data given below
{
"data": [
{
"id": 60009001,
"userFullName": "",
"gender": "male",
"depositDone": 1,
"familyId": 60009001
}
]
}
[
{
"id": 60009001,
"gender": "male",
}
]
i'm getting this data from backend and i only need the field "familyId" in first case and "gender" in second case.
How can i parse it using ramda.
Something along these lines should satisfy the requirement:
const { project, propOr } = R
const sampleData = {
data: [
{
id: 60009001,
userFullName: "",
gender: "male",
depositDone: 1,
familyId: 60009001
}
]
}
const result = project(['id', 'gender'], propOr([], 'data', sampleData))
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>

Filtering a deep json hierarchy with Linq

Given the json below I want to project into a new type where WorkflowStepName="Translate" and Status="FINISHED".
My Attempt which doesn't work
var y = statuses.Where(s => s.Jobs.Any(j => j.Steps.Any(js => js.WorkflowStepName.Equals("Translate") && js.Status.Equals("ACTIVE"))))
.SelectMany(s => s.Jobs.Select(j => new JJob()
{ ProjectId = s.ProjectId, JobId = j.JobId, Name = j.FileName, TargetLanguage = j.TargetLanguage}));
[
{
"Activity": "ARCHIVED",
"CompletionStatus": "FINISHED",
"Jobs": [
{
"CompletionStatus": "FINISHED",
"FileName": "j1.xlf",
"JobId": 1,
"Steps": [
{
"AutoStatus": null,
"Status": "FINISHED",
"WorkflowStepName": "Review"
}
],
"TargetLanguage": "ar_EG"
}
],
"ProjectId": 11,
"SourceLanguage": "en_US"
},
{
"Activity": "ACTIVE",
"CompletionStatus": "FINISHED",
"Jobs": [
{
"CompletionStatus": "FINISHED",
"FileName": "j2.xlf",
"JobId": 2,
"Steps": [
{
"AutoStatus": null,
"Status": "FINISHED",
"WorkflowStepName": "Translate"
}
],
"TargetLanguage": "zh_TW"
}
],
"ProjectId": 22,
"SourceLanguage": "en_US"
},
{
"Activity": "ACTIVE",
"CompletionStatus": "IN_PROGRESS",
"Jobs": [
{
"CompletionStatus": "FINISHED",
"FileName": "j3.xlf",
"JobId": 3,
"Steps": [
{
"AutoStatus": null,
"Status": "FINISHED",
"WorkflowStepName": "Translate"
}
],
"TargetLanguage": "de_DE"
}
],
"ProjectId": 33,
"SourceLanguage": "en_US"
}
]
I can write a static extension which uses nested foreach loops to achieve the correct thing but just seems like there should be a way with Linq. Have also tried various sub-selects but I need the flattened data such as ProjectId and JobId from the top of the hierarchy.
You could try approach your parent level hierarchy references with this SelectMany() overload:
var y = statuses.SelectMany(s => s.Jobs.SelectMany(j => j.Steps, (job, step) => new {Job = job, Step = step}), (project, jobStep) => new {Project = project, JobStep = jobStep})
.Where(js => js.JobStep.Step.WorkflowStepName.Equals("Translate") && js.JobStep.Step.Status.Equals("FINISHED"))
.Select(j => new JJob { ProjectId = j.Project.ProjectId, JobId = j.JobStep.Job.JobId, Name = j.JobStep.Job.FileName, TargetLanguage = j.JobStep.Job.TargetLanguage});
that basically flattens your hierarchy into a flat list of Tuple<Project, Tuple<Job, Step>> (I'm resorting to anonymous objects but idea remains the same), which you then can query and project as you please.

RxJS distinct on array of objects in property

i am having an observable which returns something like this:
"#odata.context": "here is the context URL",
"value": [
{
"Id": 1,
"Value": "A"
},
{
"Id": 2,
"Value": "B"
},
{
"Id": 3,
"Value": "C"
},
{
"Id": 4,
"Value": "A"
}
]
}
Using RxJS i would like to get into the "value" property and use distinct on it to limit the response to something like this:
"#odata.context": "here is the context URL",
"value": [
{
"Id": 1,
"Value": "A"
},
{
"Id": 2,
"Value": "B"
},
{
"Id": 3,
"Value": "C"
}
]
}
could you help me with it?
It pretty much depends on how exactly you want to use this RxJS chain but I hope this leads you the right direction:
const source = of(data)
.pipe(
mergeMap(d => d.value),
distinct(v => v.Value),
toArray(),
)
.subscribe(console.log);
mergeMap will reiterate the array of values as separate emissions where distinct will ignore duplicate Values. Then toArray() will just collect all results.
Live demo: https://stackblitz.com/edit/rxjs-is1zp4

Update a subdocument list object value in RethinkDB

{
"id": 1,
"subdocuments": [
{
"id": "A",
"name": 1
},
{
"id": "B",
"name": 2
},
{
"id": "C",
"name": 3
}
]
}
How do update a subdocument "A"s "name" to a value of 2 in RethinkDB in either Javascript or Python?
If you can rely of the position of your "A " element you can update like this:
r.db("DB").table("TABLE").get(1)
.update({subdocuments:
r.row("subdocuments").changeAt(0, r.row("subdocuments").nth(0).merge({"name":2}))})
If you can not rely on the position, you have to find it yourself:
r.db("DB").table("TABLE").get(1).do(function(doc){
return doc("subdocuments").offsetsOf(function(sub){return sub("id").match("A")}).nth(0)
.do(function(index){
return r.db("DB").table("TABLE").update({"subdocuments":
doc("subdocuments").changeAt(index, doc("subdocuments").nth(index).merge({"name":2})) })})
})
As an alternative you can use the map function to iterate over the array elements and update the one that matches your condition
r.db("DB").table("TABLE").get(1)
.update({
subdocuments: r.row("subdocuments").map(function(sub){
return r.branch(sub("id").eq("A"), sub.merge({name: 2}), sub)
})
})

Resources