Thursday, November 2, 2017

React & Redux for beginners: An Intro

Redux is a framework that controls states in a JavaScript app. According to the official site:

  Redux is a predictable state container for JavaScript apps.

Learning a new JavaScript framework in 2017 can be a daunting task. I find the article to be one of the most helpful tutorials out there. It can still be too much for someone starting out in React/Redux so I documented my journey. 

Demystifying Redux

To start with Redux, let’s see various component for Redux
  • Store
  • Reducers
  • Provider
  • Containers
  • Components
  • Actions

 
Store: Manages the states. Mainly there is a dispatch method to dispatch an action. In a Redux app, you can obtain its states via store.getState()

Reducers: Reducers take in actions and update part of application state.
·         We combine all reducers into a single object before updated data is dispatched (sent) to store
·         Your entire applications state (store) is just whatever gets returned from all your reducers

Provider: it connected state and application components

Components: Containers are very similar to components, the only difference is that containers are aware of application state. If part of your webpage is only used for displaying data (dumb) then make it a component. If you need it to be smart and aware of the state i.e. whenever data changes system become aware

Containers: Containers fetch state data and use it to render (display) components.state data will become components props
Containers are similar to components. However, only containers have access to state data in Redux.components are sometimes called "dumb components" or "presentational components"
Actions: A simple, plain JavaScript Object. An action can also be considered as a command to change a state.
  
Redux Example

Let’s make an example application which will list down users and selection on any user will show its details. Application data flow is as follows

 



I have used JetBrains “WebStorm 2017.2.5” IDE for making learning sample, latest Node JS and step one is to build page which will list down users say index.html

index.html

<!DOCTYPE html>

<html lang="en"><head>     <meta charset="UTF-8">     <title>React Webpack</title></head><body>     <div id="root"></div>     <script src="js/bundle.min.js"></script></body></html>


Root is main container which will hold all application components i.e. display or render dynamic page then let’s create file structure for our sample application, as shown in below snapshot



We will be creating #1 component, which will list down #2 different type of data , which is we are referring as reducers one is collection of user then another is details of select users. Obviously action is user selection. Let’s club all this together build First components, its list down all dynamic HTML part of our application so App.js will be as follows

import React from 'react';
import UserList from '../containers/user-list';
import UserDetail from '../containers/user-detail';

require(

require('../../scss/style.scss');


const App = () => (
    <
    <div>         <h2>User List:</h2>         <UserList/>
        <hr />         <h2>User Details:</h2>         <UserDetail/>     </div> );

export default App;


Where App is main component and user list and user details are reducers. Then let’s create reducers index.js, it will hold all reducers of application into one place. In our case where we want to build user list and show user information it will have two reducers’  reducer-users.js & reducer-active-user.js
index.js

import {combineReducers} from 'redux';
import UserReducer from './reducer-users';
import ActiveUserReducer from './reducer-active-user';




const allReducers = combineReducers({

    users: UserReducer,

    activeUser : ActiveUserReducer
});

});
export default allReducers;





So allReducers is main reducer which holds reference of user reducer and Active User reducer.  reducer-users.js & reducer-active-user.js are as follows

export  default function () {
    
    return [
        {
       
        {             id: 1,
       
            first: "Manjul",
       
            last: "Dube",
       
            age: 71,
       
            description: "Manjul Dube",
       
            thumbnail: "http://lh5.googleusercontent.com/s80-c/photo.jpg"
   
        },
] reducer-active-user.js export default function (state=null, action) {

    switch(action.type){
   
        case "USER_SELECTED":
       
            return  action.payload;
   
        break;
    }


    }
    return state;
}
} 

reducer-active-user.js is important to focus as here we have defined action type user selected , it’s doing nothing special just define action call back method.. Action object hold two parts, first is description which say what is performed and second is result of action, so in action index.js will look as follows
index.js

export const selectUser = (user) => {

    console.log("You clicked on user: ", user.first);

    return {
   
        type: 'USER_SELECTED',
   
        payload: user
    }
};

 

So till now basic skeleton is ready, now we need to make containers, they will connect our application state to its component and make them smart state aware parts. Container hold following two important methods mapStateToProps & matchDispatchToProps.. So user-list.js container will look as follows

user-list.js

import React, {Component} from 'react';
import {bindActionCreators} from 'redux';
import {connect} from 'react-redux';
import {selectUser} from '../actions/index'
class UserList extends Component {

    createListItems(){
   
        return this.props.users.map(
            user => {
           
            user => {                 return (
                    <
                    <li
                   
                        key={user.id}
                   
                        onClick={ () => this.props.selectUser(user)}
                    >
   {user.
                    >   {user.first}  {user.last}
                    </
                    </li>                 );            }         );    }    render() {
   
        return (
            <
            <ul>                 {this.createListItems()}
            </
            </ul>         );     }}function mapStateToProps(state){

    return {
   
        users : state.users
   
}
}

    } } function matchDispatchToProps(dispatch){

    return bindActionCreators({selectUser: selectUser}, dispatch);
}

} export default connect(mapStateToProps ,matchDispatchToProps)(UserList);

user-detail.js

import React, {Component} from 'react';
import {connect} from 'react-redux'; class UserDetail extends Component {

    render() {
   
        if (!this.props.user) {
       
            return (<div>Select a user...</div>);
        }
   
        }         return (
            <
            <div>                 <img src={this.props.user.thumbnail}  height="150px;" width="230px;" />                 <h2>{this.props.user.first} {this.props.user.last}</h2>                 <h3>Age: {this.props.user.age}</h3>                 <h3>Description: {this.props.user.description}</h3>             </div>         );     } }function mapStateToProps(state) {

    return {
   
        user: state.activeUser
   
};
}
    }; } export default connect(mapStateToProps)(UserDetail);


Once application is ready, run npm start command that will run your application


If select any user it will give you following result



My goal was simple to understand Redux and manage state, I follow a boilerplate application and created something similar. Though its SPA and just learning sample but consist of all technical components and their interaction. it’s a beginner’s Travel Log, I stared with various blogs, tutorials and downloaded one of boilerplate from GitHub to go through Redux. What do you think? Let me know your inputs and questions that will lead another direction for learning and exploration.. 

Courtesy: Several online blogs, you tube videos, tutorials,GitHub sample code & other resources

Monday, October 2, 2017

MongoDB Overview

MongoDB is a free and open-source, cross-platform, document oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on concept of collection and document, in simple terms JSON (JavaScript Object Notation) format at high level and at low level it’s BSON (Binary JSON). Before we understand we need to understand what it means when we say MongoDB works on concept of collection and document. In this article I’ll try to help you out with setup and basic commands for MongoDB. Let’s begin with understanding what makes MongoDB.

ACID Database Transactions
ACID (Atomicity, Consistency, Isolation and Durability) is a set of properties of database transactions intended to guarantee validity even in the event of errors, power failures, etc. We have used many RDBM databases designed on ACID properties.

CAP Theorem
CAP theorem states that it is impossible for a distributed data store to simultaneously provide more than two out of the following three guarantees: Consistency. Availability. Partition tolerance.



Based combination of consistency, availability and partition tolerance new generation of databases are designed. MongoDB is designed considering consistency and partition tolerance. When we want scalable NoSQL database MongoDB stands very clear in choice.


MongoDB:
MongoDB works on concept of collection and document. We need to understand following basic terms before we start MongoDB

  • Database: Database is a physical container for collections. Each database gets its own set of files on the file system. A single MongoDB server typically has multiple databases.
  • Collection: Collection is a group of MongoDB documents. It is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema. Documents within a collection can have different fields. Typically, all documents in a collection are of similar or related purpose.
  • Document: A document is a set of key-value pairs. Documents have dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and common fields in a collection's documents may hold different types of data.

MongoDB uses Sharding to be partition tolerance it’s the process of storing data records across multiple machines and it is MongoDB's approach to meeting the demands of data growth.

MongoDB Installation:
You can download setup from https://www.mongodb.com/ or opt for MongoDB Atlas, it allows to Deploy, operate, and scale a MongoDB database in the cloud.  Basically database as a Service. Latest version of MongoDB DB is 3.4 at this time... Once you downloaded setup mongodb-win32-x86_64-enterprise-windows-64-3.4.9-signed.msi, simply next clicks will install server and client in your machine




In Bin folder you will get Database Server and Client which is mongod.exe and mongo.exe.

These are exes which will allow you to run shell version of MongoDB.
 --dbpath parameter in MongoDB will allow you to control what directory MongoDB reads and writes its data from.

 

Once data path is configured you are good to go.

MongoDB collection and document:
·         To make collection you need to run db.createCollection command e.g.

>db.createCollection("Products")
{ "ok" : 1 }
·         The basic syntax of insert() command is db.COLLECTION_NAME.insert(document)
>db.Products.insert({itemName: “Pen”, QOH: 1000,Price:5.00 })
For multiple insert you need to pass JSON array... Any document inserted into MongoDB get assigned _id for indexing which you can use while fetching or searching records.

·         The basic syntax of find() command is db.COLLECTION_NAME.find(document)
>db.Products.find()
Similar way you can update and other commands.Refer https://docs.mongodb.com/manual/reference/mongo-shell/

Additional tool
Robo 3T is the free lightweight GUI for MongoDB. It’s formerly known as Robomongo and can be downloaded from https://robomongo.org/


Advantages of MongoDB over RDBMS:
1) Schema less − MongoDB is a document database in which one collection holds different documents. Number of fields, content and size of the document can differ from one document to another.
Structure of a single object is clear
No complex joins.
Deep query-ability. MongoDB supports dynamic queries on documents using a document-based query language that's nearly as powerful as SQL.
Ease of scale-out − MongoDB is easy to scale.Conversion/mapping of application objects to database objects not needed.
Uses internal memory for storing the (windowed) working set, enabling faster access of data.

This is to begin with MongoDB...What do you think? Let me know your suggestions and inputs.



Courtesy: Several online blogs & other resources