React. Streaming JSON file - javascript

I don't have any backend logic and database. For data I just use a json file of humans.
Here it is:
[
{
"id": 0,
"name": "Andrew"
},
{
"id": 1,
"name": "Daniel"
},
{
"id": 2,
"name": "John"
},
{
"id": 3,
"name": "Frank"
}
]
Can I somehow stream this JSON file in React (i.e. on client side) to, for example, retrieve only the first two notes and return the array? I tried FS module but it only works for SSR. Or there's no way I can runtime stream it getting desired data? I just don't wanna return the whole array since I'm trying to imitate a backend database

Related

How to navigate to the last child of a hierarchical data stored in SQL database based on the JSON passed in the request body in nodeJS?

I have a need to get the last child of a hierarchy based on the JSON data passed from the request body.
The request body looks like this:
{
"hierarchy": [
{
"id": 1,
"name": "A",
"level": 0,
"labels": [
"grandparent",
"parent",
"child"
],
"categories": [
{
"id": 2,
"name": "B",
"level": 1,
"action": "kill",
"categories": [
{
"id": 3,
"name": "C",
"level": 2,
"action": "kill",
"categories": []
}
]
}
],
"action": "kill"
}
]
}
The data will be stored in the SQL database like this:
Based on the data sent from the req body, I need to navigate to the last child in the database , in this case: C and then delete them.
Currently Im using this:
const killfamily = async (id, transaction, header = {}) => {
//need recursive method here
await family.destroy({ where: { parent_id: id }, transaction: transaction });
const result = await family.destroy({ where: { fid: id }, transaction: transaction });
return result;
Current approach is not working as im trying to delete the parent first. I want to achieve this using recursive method i.e Go to the inner most/ last child and perform destroy on it.
Any help is appreciated. Thank you

Sorting after displaying deep JSON object

I have a dynamic JSON object that has multi-level depth to support a folder structure.
The object is being stored in a state, looking similar to this:
{
"_contents": [
{
"name": "filename_1",
"path": "filename_1"
},
{
"name": "filename_2",
"path": "filename_2"
}
],
"folder_1": {
"_contents": [
{
"name": "filename_1",
"path": "folder_1/filename_1"
},
{
"name": "filename sdafasdfasdfs",
"path": "sasfasdfas"
}
]
},
"folder_2": {
"_contents": [],
"folder_2_1": {
"_contents": [
{
"name": "filename1.md",
"path": "folder_2/folder_2_1/filename1.md"
}
],
"folder_2_1_1": {
"_contents": [
{
"name": "filename1.md",
"path": "folder_2/folder_2_1/folder_2_1_1/filename1.md"
}
]
}
}
}
}
I then pass my object into a react component that generate a UI folder view.
Using this module : https://www.npmjs.com/package/react-nested-file-tree
The issue I'm running into is that after I pass my object state into the node component, the order of the files are being lost.
They go in 1, 2, 3 and come out in the UI as 2,3,1 (or some other random order).
This seems to be a problem in general with mapping through deep JSON objects. But I haven't found a way to prevent or change this?
Is there any way I can preserve the order of my JSON object, or sort after the UI has been built? I've looked into other modules that generate UI's, and I'm getting similar results.
EDIT: I should note that I'm restricted to building my JSON object to fit into the format of whatever module I'm using.

How to fetch data from json in Angular2 (TypeScript) without HTTP?

I have task (auth):
All data should be stored in LocalStorage.
Application should be written without back-end languages or services
You should create a json file with 10 test users and get it content while application forming.
Save user to LocalStorage.
I create a json file with data of users, but i don't understand how to fetch it in my service.
{
"Users": [
{
"Id": 1,
"name": "FirstUser",
"email": "1stemail#gmail.com",
"password": "test123"
},
{
"Id": 2,
"name": "2thUser",
"email": "2themail#gmail.com",
"password": "test123"
}
]
}
I tried some tricks like
"import as data from '../shared/users.json'
"const users = data.Users" (error on Users)
Do you have idea "how to" ?
I think it should be import * as data from '../shared/users.json but your system.config might add .ts or .js extension at the end of it.
The way I prefer to do it is to export it as a variable inside a ts file
export var data = {
"Users": [
{
"Id": 1,
"name": "FirstUser",
"email": "1stemail#gmail.com",
"password": "test123"
},
{
"Id": 2,
"name": "2thUser",
"email": "2themail#gmail.com",
"password": "test123"
}
]
}
then import that variable inside your component
import { data } from '../shared/users';
Full plunker example: http://plnkr.co/edit/CGtxYJkcjYt2cEzrbL00?p=info

Filter store by Id has coincidences with other store in Sencha Touch

I am working in a Sencha touch app, and I am finding problems to filter correctly the next scenario:
I have this API:
In this area we have different templates number related with the Id, data is saved in a store call (for example: templates):
{
"id": "0100100001",
"templates": {
"id": "0000000187", ---> customerId
"name": "TEST01",
"validFrom": "\/Date(1360710000000)\/",
"validTo": "\/Date(253402300799000)\/"
},
{
"id": "0000000188",
"name": "S_TEST_01",
"validFrom": "\/Date(1360710000000)\/",
"validTo": "\/Date(253402300799000)\/"
}
}
In other Json/store called "Survey" we save the next data,
"id": "0000104747",
"templateId": "0000000955",
"templateName": "SURVEYAPP TEST1",
"customerId": "0100100001",
Question is: How can I evaluate if the customerId has a template created in the "Surveys" store?
Thank you in advance.

Modifying Embers expected json api response

I'm new to Ember and from what I understand it has a very specific way it excepts its json api response to look like. Like so:
{
"post": {
"id": 1,
"title": "Node is not omakase",
"comments": [1, 2, 3]
},
"comments": [{
"id": 1,
"body": "But is it _lightweight_ omakase?"
},
{
"id": 2,
"body": "I for one welcome our new omakase overlords"
},
{
"id": 3,
"body": "Put me on the fast track to a delicious dinner"
}]
}
Now the api I've already built has an json response that looks like so:
{
"data": {
"id": 1,
"name": "Pansy Bednar",
"links": [
{
"rel": "self",
"uri": "/pansy-bednar15"
}
],
"players": {
"data": [
{
"id": 2,
"name": "Nicholas O'Reilly",
"position": "cad",
"age": 23,
"value": "640",
"links": [
{
"rel": "self",
"uri": "/team/nicholas-o-reilly71"
}
]
}
]
}
}
}
The api is pretty big and is working fine with the mobile app. So a code re write would be to costly I would just choose another js framework even though I like Ember the best.
So my question is is there any way I can adapt the expected json response in ember. If yes how hard is it? Worth the time or should I just go for Angular or Aurelia.
or am I completely wrong and there is no one expected response to ember?
The thing you can do with ember is to write your very own adapter for this there are already many questions+answers out there:
https://stackoverflow.com/a/17938593/1581725
https://stackoverflow.com/a/24411550/1581725
...
And there is also this blog entry here: http://eviltrout.com/2013/03/23/ember-without-data.html it's about using ember without ember-data.
Found this little gem called normalizePayload - maybe this would also work for your case: https://stackoverflow.com/a/21790093/1581725

Categories