
By Ilya Bohaslauchyk | Article Rating: |
|
August 3, 2017 12:00 PM EDT | Reads: |
523 |
When you work with React app, it normally needs some data from the server to store it for immediate use (e.g., show it on the page). If the app works with some complex relational database the work than may be a bit challenging. In this article I am going to describe the issues with organizing the data, which we faced during our work on one of DashBouquet projects, and I will also speak about our solutions.
To make it easier, we will have a look at an example. Let's think of an app with multiple pages where each page needs certain amount of data from the server. In case there are enough pages, the amount of data would become too massive to be fetched at once while loading the app. So the data is requested for every page load. And for every page we most probably have a partition where we can save page-related data, so it would be a good idea to put there fetched data too.
In our example the app has few pages. First page would be list of teachers in a university and second page would have a list of students. When the user clicks on the teacher name, they will get to a page of a certain teacher with lists of the students and courses. While clicking on the student, the user will get to his page with list of teachers and courses.
The ER diagram would look like this:
We can decide what data we need on which page:
To describe my example I will use the stack that we deployed in our project. It includes Loopback for back-end, React, Redux and Redux-saga for front-end and Axios for interaction with the server.
It is clear that same data may be required for different pages. For example we will have all teachers on Teachers page and some of them on a page of a single student. Where do we store them? Maybe we don't always need to fetch the teachers on Single student page? But if we decide not to fetch the teachers, where do we take the data? If we decide to fetch on both pages where do we put the data to avoid duplication? When the data is spread across the whole store it becomes very confusing, especially if the app is big (more than four pages) and has great amount of data.
Keeping all the data in one place may help you a lot. But there is a question: how would you fetch it? For sure we don't want to fetch the entities separately, because this way we would easily reach the point of 30-50 requests per page and this would make page loading awfully slow. We want to try and get as much data as possible in one request (e.g. using an include filter in case of Loopback). In the response for teachers we get something like this:
[
{
students: [
{
id,
name,
studentCourses: [
{
studentId,
courseId,
grande,
},
...
],
},
...
],
courses: [
{
id,
name,
},
...
],
},
...
]
In case you use Loopback as well, have a look at this doc where you can read about how to combine data in queries here.
In this case our actions would spare us two requests but this is not very useful if we speak about storing on front-end. First of all, what would happen if something changes in the database (like adding new courses for a teacher)? Instead of refetching the courses, we would have to refetch both courses and teachers. Secondly, we might need courses inside of a student entity instance, but when we make a query to the server, we don't include these courses to avoid duplication.
To deal with these issues we can start with Normalizr - a utility that normalizes data with nested objects, just like in our case. No need to describe it in detail: you can find all information here. The point is that after applying some simple manipulations to the result of Normalizr's work we get data that we can keep in store.
We need to define a couple of sagas. If you haven't used redux-saga in your projects yet, I think this should convince you to do so.
The first saga will fetch data:
const urls = {
teachers: '/teachers,
students: '/students’,
courses: '/courses',
};
export function* fetchModel(model, ids, include) {
const url = urls[model];
const where = {id: {inq: ids}};
const filter = {where, include};
const params = {filter};
return yield get({url, params});
}
The second will store the data:
export function* queryModels(modelName, ids, include]) {
const singleModelSchema = schema[modelName];
const denormalizedData = yield fetchModel(modelName, ids, include);
const normalizedData = normalize(denormalizedData, [singleModelSchema]);
const {entities} = normalizedData;
yield put(addEntities(entities));
}
And a reducer will add new pieces of data to the already fetched ones:
case ADD_ENTITIES: {
const models = action.entities;
const newState = cloneDeep(state);
return mergeWith(newState, models);
}
Having done all that we can query the data from the server in this manner:
export function* fetchTeachers() {
yield queryModels('teachers', ids, [
{
relation: 'students',
scope: {
include: ['studentCourses'],
}
},
{
relation: 'courses',
}
]);
}
Normalizr uses a normalization schema as described here.
Eventually we end up with the state that looks like this:
state: {
...
models: {
teachers: {...},
sudents: {...},
courses: {...},
studentCourses: {...},
},
...
}
This is actually a pretty little copy of a part of our database in the store. There is no need to dispatch plenty of actions to put fetched data to different sections of the store and to remember where every piece of data should be stored. It is done in queryModels saga and we always know where the fetched data is going to be put.
After that we can use it in any page of the app combing it in selectors as required.
In our case, if needed, we can get an object for teacher as complicated as this:
{
students: [
{
id,
name,
studentCourses: [...],
courses: [...],
},
...
],
courses: [
{
id,
name,
sudents: [...],
teachers: [...],
},
...
],
}
There is another way as well. We can describe an all-purpose API to denormalize the data before using it. The problem here is that we need an API to denormalize data, because the denormalize function that comes along with Normalizr package would only denormalize data to a shape it was when it came from the server, which is not exactly what we want. As described above, we got courses only within teacher entities, though we might need them anywhere else. In case of larger projects I believe you may want to spend time on a custom denormalization function. However, this is a different topic and we may cover it in the next article.
For me this approach became quite a relief. The main advantage here is that you always know where to find what you need. And in case you decide that it would be better to aggregate data in another way, you don't need to mess with the queries again, you just change a selector a little. In general, managing data in store requires much less effort with this approach.
Download Show Prospectus ▸ Here
DevOps at Cloud Expo taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, will feature technical sessions from a rock star conference faculty and the leading industry players in the world.
Must Watch Video: Recap of @DevOpsSummit New York Javits Center
The widespread success of cloud computing is driving the DevOps revolution in enterprise IT. Now as never before, development teams must communicate and collaborate in a dynamic, 24/7/365 environment. There is no time to wait for long development cycles that produce software that is obsolete at launch. DevOps may be disruptive, but it is essential.
Nutanix DevOps Booth at @DevOpsSummit New York Javits Center
DevOps at Cloud Expo will expand the DevOps community, enable a wide sharing of knowledge, and educate delegates and technology providers alike. Recent research has shown that DevOps dramatically reduces development time, the amount of enterprise IT professionals put out fires, and support time generally. Time spent on infrastructure development is significantly increased, and DevOps practitioners report more software releases and higher quality. Sponsors of DevOps at Cloud Expo will benefit from unmatched branding, profile building and lead generation opportunities through:
- Featured on-site presentation and ongoing on-demand webcast exposure to a captive audience of industry decision-makers.
- Showcase exhibition during our new extended dedicated expo hours
- Breakout Session Priority scheduling for Sponsors that have been guaranteed a 35 minute technical session
- Online advertising in SYS-CON's i-Technology Publications
- Capitalize on our Comprehensive Marketing efforts leading up to the show with print mailings, e-newsletters and extensive online media coverage.
- Unprecedented PR Coverage: Editorial Coverage on DevOps Journal
- Tweetup to over 75,000 plus followers
- Press releases sent on major wire services to over 500 industry analysts.
For more information on sponsorship, exhibit, and keynote opportunities, contact Carmen Gonzalez by email at events (at) sys-con.com, or by phone 201 802-3021.
Most Popular Video: Sheng Liang's Containers Talk
@DevOpsSummit at Cloud Expo taking place October 31 - November 2, 2017, Santa Clara Convention Center, CA, and is co-located with the 21st International Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world.
@DevOpsSummit 2017 Silicon Valley
(October 31 - November 2, 2017, Santa Clara Convention Center, CA)
@DevOpsSummit 2018 New York
(June 12-14, 2018, Javits Center, Manhattan)
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo, October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
Track 1. Enterprise Cloud | Cloud-Native
Track 2. Big Data | Analytics
Track 3. Internet of Things | IIoT | Smart Cities
Track 4. DevOps | Digital Transformation (DX)
Track 5. APIs | Cloud Security | Mobility
Track 6. AI | ML | DL | Cognitive
Track 7. Containers | Microservices | Serverless
Track 8. FinTech | InsurTech | Token Economy
Speaking Opportunities
The upcoming 21st International @CloudExpo | @ThingsExpo, October 31 - November 2, 2017, Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY announces that its Call For Papers for speaking opportunities is open. Themes and topics to be discussed include:
- Agile
- API management
- APM
- Application delivery
- Cloud development
- Configuration automation
- Containers
- Continuous delivery
- Continuous integration
- Continuous testing
- DevOps anti-patterns
- DevOps for legacy systems
- DevOps skills and training
- DevOps system architecture
- Docker
- Enterprise DevOps
- Identity and access
- IT orchestration
- Kubernetes
- Load testing
- Microservices
- Mobile DevOps
- Monitoring
- Network automation
- Quality assurance
- Release automation
- Serverless
- Scrum
- Service virtualization
- Teaming
- Test automation
- WebOps, CloudOps, ChatOps, NoOps
Submit your speaking proposal today! ▸ Here
Cloud Expo | @ThingsExpo 2017 Silicon Valley
(October 31 - November 2, 2017, Santa Clara Convention Center, CA)
Cloud Expo | @ThingsExpo 2018 New York
(June 12-14, 2018, Javits Center, Manhattan)
Download Show Prospectus ▸ Here
Every Global 2000 enterprise in the world is now integrating cloud computing in some form into its IT development and operations. Midsize and small businesses are also migrating to the cloud in increasing numbers.
Companies are each developing their unique mix of cloud technologies and services, forming multi-cloud and hybrid cloud architectures and deployments across all major industries. Cloud-driven thinking has become the norm in financial services, manufacturing, telco, healthcare, transportation, energy, media, entertainment, retail and other consumer industries, and the public sector.
Cloud Expo is the single show where technology buyers and vendors can meet to experience and discus cloud computing and all that it entails. Sponsors of Cloud Expo will benefit from unmatched branding, profile building and lead generation opportunities through:
- Featured on-site presentation and ongoing on-demand webcast exposure to a captive audience of industry decision-makers.
- Showcase exhibition during our new extended dedicated expo hours
- Breakout Session Priority scheduling for Sponsors that have been guaranteed a 35-minute technical session
- Online advertising in SYS-CON's i-Technology Publications
- Capitalize on our Comprehensive Marketing efforts leading up to the show with print mailings, e-newsletters and extensive online media coverage.
- Unprecedented PR Coverage: Editorial Coverage on Cloud Computing Journal.
- Tweetup to over 75,000 plus followers
- Press releases sent on major wire services to over 500 industry analysts.
For more information on sponsorship, exhibit, and keynote opportunities, contact Carmen Gonzalez by email at events (at) sys-con.com, or by phone 201 802-3021.
The World's Largest "Cloud Digital Transformation" Event
@CloudExpo | @ThingsExpo 2017 Silicon Valley
(Oct. 31 - Nov. 2, 2017, Santa Clara Convention Center, CA)
@CloudExpo | @ThingsExpo 2018 New York
(June 12-14, 2018, Javits Center, Manhattan)
Full Conference Registration Gold Pass and Exhibit Hall ▸ Here
Register For @CloudExpo ▸ Here via EventBrite
Register For @ThingsExpo ▸ Here via EventBrite
Register For @DevOpsSummit ▸ Here via EventBrite
Sponsorship Opportunities
Sponsors of Cloud Expo | @ThingsExpo will benefit from unmatched branding, profile building and lead generation opportunities through:
- Featured on-site presentation and ongoing on-demand webcast exposure to a captive audience of industry decision-makers
- Showcase exhibition during our new extended dedicated expo hours
- Breakout Session Priority scheduling for Sponsors that have been guaranteed a 35 minute technical session
- Online targeted advertising in SYS-CON's i-Technology Publications
- Capitalize on our Comprehensive Marketing efforts leading up to the show with print mailings, e-newsletters and extensive online media coverage
- Unprecedented Marketing Coverage: Editorial Coverage on ITweetup to over 100,000 plus followers, press releases sent on major wire services to over 500 industry analysts
For more information on sponsorship, exhibit, and keynote opportunities, contact Carmen Gonzalez (@GonzalezCarmen) today by email at events (at) sys-con.com, or by phone 201 802-3021.
Secrets of Sponsors and Exhibitors ▸ Here
Secrets of Cloud Expo Speakers ▸ Here
All major researchers estimate there will be tens of billions devices - computers, smartphones, tablets, and sensors - connected to the Internet by 2020. This number will continue to grow at a rapid pace for the next several decades.
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend @CloudExpo | @ThingsExpo, October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-4, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
Delegates to Cloud Expo | @ThingsExpo will be able to attend 8 simultaneous, information-packed education tracks.
There are over 120 breakout sessions in all, with Keynotes, General Sessions, and Power Panels adding to three days of incredibly rich presentations and content.
Join Cloud Expo | @ThingsExpo conference chair Roger Strukhoff (@IoT2040), October 31 - November 2, 2017, Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, for three days of intense Enterprise Cloud and 'Digital Transformation' discussion and focus, including Big Data's indispensable role in IoT, Smart Grids and (IIoT) Industrial Internet of Things, Wearables and Consumer IoT, as well as (new) Digital Transformation in Vertical Markets.
Financial Technology - or FinTech - Is Now Part of the @CloudExpo Program!
Accordingly, attendees at the upcoming 21st Cloud Expo | @ThingsExpo October 31 - November 2, 2017, Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, will find fresh new content in a new track called FinTech, which will incorporate machine learning, artificial intelligence, deep learning, and blockchain into one track.
Financial enterprises in New York City, London, Singapore, and other world financial capitals are embracing a new generation of smart, automated FinTech that eliminates many cumbersome, slow, and expensive intermediate processes from their businesses.
FinTech brings efficiency as well as the ability to deliver new services and a much improved customer experience throughout the global financial services industry. FinTech is a natural fit with cloud computing, as new services are quickly developed, deployed, and scaled on public, private, and hybrid clouds.
More than US$20 billion in venture capital is being invested in FinTech this year. @CloudExpo is pleased to bring you the latest FinTech developments as an integral part of our program, starting at the 21st International Cloud Expo October 31 - November 2, 2017 in Silicon Valley, and June 12-14, 2018, in New York City.
@CloudExpo is accepting submissions for this new track, so please visit www.CloudComputingExpo.com for the latest information.
About SYS-CON Media & Events
SYS-CON Media (www.sys-con.com) has since 1994 been connecting technology companies and customers through a comprehensive content stream - featuring over forty focused subject areas, from Cloud Computing to Web Security - interwoven with market-leading full-scale conferences produced by SYS-CON Events. The company's internationally recognized brands include among others Cloud Expo® (@CloudExpo), Big Data Expo® (@BigDataExpo), DevOps Summit (@DevOpsSummit), @ThingsExpo® (@ThingsExpo), Containers Expo (@ContainersExpo) and Microservices Expo (@MicroservicesE).
Cloud Expo®, Big Data Expo® and @ThingsExpo® are registered trademarks of Cloud Expo, Inc., a SYS-CON Events company.
Published August 3, 2017 Reads 523
Copyright © 2017 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Ilya Bohaslauchyk
Ilya Bohaslauchyk is a front-end developer at DashBouquet Development.
![]() Aug. 3, 2017 03:45 PM EDT Reads: 1,795 |
By Yeshim Deniz ![]() Aug. 3, 2017 03:45 PM EDT Reads: 1,610 |
By Liz McMillan ![]() Aug. 3, 2017 03:35 PM EDT |
By Yeshim Deniz ![]() Aug. 3, 2017 02:15 PM EDT Reads: 2,834 |
By Yeshim Deniz ![]() Aug. 3, 2017 01:15 PM EDT Reads: 3,654 |
By Carmen Gonzalez ![]() Aug. 3, 2017 01:00 PM EDT Reads: 1,136 |
By Pat Romanski ![]() Aug. 3, 2017 12:45 PM EDT Reads: 1,652 |
By Elizabeth White ![]() Aug. 3, 2017 11:45 AM EDT Reads: 880 |
By Pat Romanski ![]() Aug. 3, 2017 11:45 AM EDT Reads: 651 |
By Carmen Gonzalez ![]() Aug. 3, 2017 11:15 AM EDT Reads: 811 |
By Yeshim Deniz ![]() Aug. 3, 2017 11:00 AM EDT Reads: 2,722 |
By Elizabeth White ![]() Aug. 3, 2017 10:45 AM EDT Reads: 1,199 |
By Yeshim Deniz ![]() Aug. 3, 2017 09:45 AM EDT Reads: 2,968 |
By Yeshim Deniz ![]() Aug. 3, 2017 08:30 AM EDT Reads: 3,839 |
By Yeshim Deniz ![]() Aug. 3, 2017 08:00 AM EDT Reads: 1,413 |
By Elizabeth White ![]() Aug. 3, 2017 07:15 AM EDT Reads: 3,064 |
By Pat Romanski ![]() Aug. 3, 2017 05:45 AM EDT Reads: 1,439 |
By Elizabeth White ![]() Aug. 3, 2017 04:45 AM EDT Reads: 2,314 |
By Elizabeth White ![]() Aug. 3, 2017 04:45 AM EDT Reads: 1,383 |
By Yeshim Deniz ![]() Aug. 3, 2017 03:45 AM EDT Reads: 923 |