Friday, 30 December 2016

Sails vs Trails

Now a days so many frameworks are populating and some of them are gaining the interest from communities.So sails and trails are also a web framework so we start with basic introduction on both.

Sails vs Trails Node.js


TrailsJS : Trails is a modern, community-driven web application framework for node.js. It builds on the pedigree of Rails and Grails to accelerate development by adhering to a straightforward, convention-based, API-driven design philosophy.


Creator: Travis Webb
GitHub: https://github.com/trailsjs/trails (1431+ stars) 
Age: 1+ year (release on 3rd Dec, 2015)
Support: Express4/5, Hapi, Koa, Restify
+1 Features: ES6, Trailpack, Footprint, Microservices
RealTime: Primus, Socket.io, Engine.io (Externally)
DB: MongoDB, MySql, Postgres, Redis, RethinkDB
ORM: Mongoose, Waterline, Knex & more…

SailsJS : Sails.js is a web framework that makes it easy to build custom, enterprise-grade Node.js apps.It is designed to resemble the MVC architecture from frameworks like Ruby on Rails, but with support for the more modern, data-oriented style of web app development. It's especially good for building realtime features like chat.


Creator: Mike McNeil
GitHub: https://github.com/balderdashy/sails (16k+ stars)     
Age: 4+ year (release on 25th Jun, 2012)
Support: Express3/4
+1 Features: Hooks, Blueprint
RealTime: Socket.io (Inbuilt)
DB: MongoDB, MySql, Postgres, Redis and more…
ORM: Waterline & other

Similarity in both :

Automatic REST APIs from models : Sails uses blueprint while Trails users footprints.

MVC : Both these frameworks are uses MVC pattern in default but with the trailsJS you can do a code with different pattern also. It supports multiple web framework & dynamic file loading.

Support web-sockets for real time : Sails comes with inbuilt socket.io supports like pub-sub & socket-client.In trails you don't have a directly support you can configure it manually.Some trailpacks for real-time support. trailpack-realtime & trailpack-pubsub.

Support multiple databases : Both supports almost all major databases like MongoDB, MySQL, Redis, CouchDB.


Plugin system : Trails has plugins system like trailpack & microservices.Sails comes with plugin system named Hook.

Policies & Services : Both using same architecture and usage for (http://sailsjs.com/documentation/concepts/policies)


How they are differ :

File loading : Sails loads all files under apis & config folders on start up (sails lift). It loads file at startup so it cause of all loading server slowly.So when your project size will be increased so startup time of server also increased. Trails will not loaded automatically. In trails every thing is loaded using index.js file so you can manage with that file.

ORM : Sails default orm is waterline. you can disbale it to use another one. Trails supports multiple ORM's like mongoose etc.

ECMAScript : Sails is built with ECMAScript 5. So It can run with Node ≥ 0.11 and Trails is built with ECMAScript15 (es6). So trails need node ≥ 4.0.

Web Server : Sails uses (built upon) Express3 (deprecated) as a web server so this will be main drawback of sails .Trails’s main advantage is It’s not built upon but built besides.

I hope you like the post please comment and share.




Tuesday, 20 December 2016

Architecture overview of Elasticsearch

If you are building an large application then you should definitely consider Elasticsearch for your datastore.Elasticsearch is not a good choice for the transaction processing system it is used for the search . We treat it as a search server and elasticsearch is built for speed and scalability. You should not be used elasticsearch for your primary data source because it may lose data.

Overview of elasticsearch



You can use elasticsearch for two reasons:

1. Real-Time analytics.
2. Powerful and Intelligent Search.

Elasticsearch is great for analytics applications:

  • Simple Super powerful Query DSL.
  • REST API for indexing data and querying it.
  • Easily configurable of shards and replicas.

Difference between elasticsearch and database.
In elasticsearch an index is like a database in which you can store different type of documents .Type of elastic is like a table in database , document you can call it a row and field in elasticsearch are coloumns in normal database.


Elasticsearch Relational Database
Index Database
Type Table
Document Row
Field Coloumn

Clusters and Nodes: Clusters are a collection of nodes that communicate with each other to read and write to an index. A cluster needs a unique name to prevent unnecessary nodes from joining and a node is single instance of ES usually it runs one instance per machine.It can communicate with network also so you can share it on network for reading and writing operations.



Shards and Indices: Let's take an example we have 1 TB machine it data is on it. If data is rapidlly increasing into machine then we might need a second machine to store some data. so data is divided into two machines which are in same cluster so we called it shards.

Replicas: Replicas are just shards that aren't actively used They're copies of your active shards to allow better availability should one of the nodes in your cluster fall down.

Please comment and share if you like the post.


Saturday, 17 December 2016

SQL vs NoSQL

SQL (Structured Query Language) databases have been a primary data storage mechanism for more than four decades and open source options are such as MySQL, PostgreSQL and SQLite.

SQL vs NoSql



  • The model is of a relational nature
  • Data is stored in tables
  • Suitable for solutions where every record is of the same kind and possesses the same properties
  • Adding a new property means you have to alter the whole schema
  • The schema is very strict
  • ACID transactions are supported
  • Scales well vertically
  • Good for structured data.



NoSQL databases have existed since the 1960s, but have gaining attraction with popular options such as MongoDB,Redis, CouchDB and Apache Cassandra.NoSQL databases that enable storing unstructured and heterogeneous data at scale have gained in popularity.

  • The model is non-relational
  • May be stored as JSON, key-value, etc. (depending on type of NoSQL database)
  • Not every record has to be of the same nature, making it very flexible
  • Add new properties to data without disturbing anything
  • No schema requirements to adhere to
  • Support for ACID transactions can vary depending on which NoSQL DB is used
  • Consistency can vary
  • Scales well horizontally
  • Good for semi structured,complex or nested data.

SQL Database : 

  • MySQL Community Edition
  • MS-SQL Server Express Edition
  • Oracle Express Edition


NoSQL Database :

  • MongoDB
  • CouchDB
  • Redis


Best Laravel package to use in your project

Start you laravel project and confuse for choosing a package stuff.I have made a list of some of the best laravel packages available to solve the same dilemma.The following packages listed below are mostly used and trusted also.

Best laravel library should be used for your project


Laracasts Flash: It's easy flash messages for your laravel app – Creator is Jeffrey Way so no second thought.

Eloquence: Extensions for the Eloquent ORM – Easy to implement and active developer.

Intervention Image: An open source php image handling and manipulation library – Great Documentation and active communtiy.

Entrust: It is role based permissions – Easy to implement.

PHP Vars To Js Transformer: Transform PHP types to JavaScript in the most easy way – Again , Jeffrey Way.

Medialibrary: Associate files with Eloquent models – Active developers and easy to use.

Log viewer: Your error logs at a single route – Neat UI.

JWT Auth: JSON Web Token Authentication for Laravel & Lumen.

Monday, 12 December 2016

Create components in vue.js

Vue : is a progressive framework for building user interfaces. Unlike other monolithic frameworks, Vue is designed from the ground up to be incrementally adoptable.

Create Component in Vue.js


The easiest way to say hello to vue.js is using JSFIDDLE hello world example.


<input type="text" list="idOfDatalist" /> 
 <datalist id="idOfDatalist">
      <option value="element1" />
      <option value="element2" />
</datalist>

As you can see its composed of two elements, the datalist which containes the elements, and the input. isn’t a good example of use of component?

Template :

<template id="auto-complete">
 <input type="text" name="{{id}}" list="{{id}}" :style="styleObject" /> 
 <datalist id="{{id}}">
 <option v-for="framework in frameworks" value="{{framework}}" />
 </datalist>
</template>


In this we need to provide the id and the frameworks which is nothing but a an array.

Component:

Vue.component("auto-complete", {
    props : ['frameworks', 'id'],
    template : "#auto-complete",
})

the component method register a global compenent that can live outside the Vue Instance, we should provide its name and an object of options such as :

Template : which holds the id of the template that we declared before.

Props : its the list of options that will be used by the component, think of it as a box that holds the data sent by the outside world.

Now how to use it.

Declare a vue.js instance and provide a data to it.

new Vue({
 el: "#app",
data: {
    id: 'jsFrameworks',
    list: [
       'JQuery',
       'React',
       'VueJs',
       'AngularJs',
    ]
   }
})

And finally the component is :


<div id="app">
 <auto-complete :frameworks="list" :id="id"></auto-complete>
</div>

I Hope you like the post please comment and share.

Sunday, 11 December 2016

Laravel JWT auth with vue.js

This post is about laravel JSON Web Token based authentication using the new version of vue.js.The vue.js 2.0 has been released you can check here.

Laravel JWT auth with vue.js


Here i'm using laravel 5.2

Install some command line tools:

NPM version 3.10.8
Nodejs version 6.9.1
vue-cli version 2.5.0
git version 2.10.2

User Model : In laravel set your database connection in your .env file and run the migration for the create users , passwords resets etc.I have moved the Users class into a Models folder.


php artisan migrate

mkdir app/Models

mv app/User.php app/Models

Edit app/Models/User.php, change the App namespace to App\Models.

User.php


<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

}


To view the full source please check the repo.

Edit, config/auth.php. Update the authentication drivers user provider for the App\Models\User namespace change as follows.

auth.php


'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],

    ...

You can read the full article here :

http://jimfrenette.com/2016/11/laravel-vuejs2-jwt-auth/?utm_source=wantcode.in

Thursday, 1 December 2016

Full-Text Search using Mysql

Search is very basic functionlity in website.Everyone wants a better search for their website so in this post i will show you how to create a full text search using mysql. All you need is mysql 4.X version or above.MySQL provides full text search capabilities that we can use to implement search functionality.

Full text search in mysql


CREATE TABLE post_articles (
    id INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
    title VARCHAR(200),
    body TEXT,
    FULLTEXT (title,body)
);

Also add some sample data in this table. Execute following insert query.


INSERT INTO post_articles (title,body) VALUES
    ('MySQL Tutorial','DBMS stands for DataBase ...'),
    ('How To Use MySQL Well','After you went through a ...'),
    ('Optimizing MySQL','In this tutorial we will show ...'),
    ('1001 MySQL Tricks','1. Never run mysqld as root. 2. ...'),
    ('MySQL vs. YourSQL','In the following database comparison ...'),
    ('MySQL Security','When configured properly, MySQL ...');

Now natural language full text search like :


SELECT * FROM post_articles WHERE MATCH (title,body) AGAINST ('database');  

his query will search against word "database"  in title and body.

Boolean Full-Text Searches : sometime you want to specify certain keywords in your search criteria.
Also you may want to ignore certain keywords. Boolean Full-Text Search can used to perform a full text search for such requirements.


SELECT * FROM post_articles WHERE MATCH (title,body) AGAINST ('+MySQL -YourSQL' IN BOOLEAN MODE);

This query will fetch all the records which has MySQL keyword but not YourSQL keyword. Notice the + and – that we have specified before the keywords!

+ stands for AND
– stands for NOT

Following are few examples for the boolean search criteria.

‘cricket football’
Find rows that contain at least one of the two words.

‘+cricket +football’
Find rows that contain both words.

‘+cricket hockey’
Find rows that contain the word “cricket”, but rank rows higher if they also contain “hockey”.

‘+cricket -hockey’
Find rows that contain the word “cricket” but not “hockey”.

I hope you like the post please comment and share.


Object Oriented Programming using JavaScript

JavaScript as the world’s most misunderstood language some programmers think that javascript language have lacks the common object-oriented programming concepts.But in this post i will show you how to write a class based javascript comes packed with a rich system of object-oriented programming that many programmers don’t know about.

object oriented programming using javascript


Prototype Based OOP : Conceptually, in class-based OOP, we first create a class to serve as a “blueprint” for objects, and then create objects based on this blueprint.For a real-world analogy, if you were to build a chair, you would first create a blueprint on paper and then manufacture chairs based on this blueprint. The blueprint here is the class, and chairs are the objects. If you wanted to build a rocking chair, you would take the blueprint, make some modifications, and manufacture rocking chairs using the new blueprint.

Lets create a new example of oops based prototype:


var student = Object.create(null);

Object.create(null) creates a new empty object.Now we add some properties and functions to our new object.


student.name = 'Peter';
student.gender = 'Male';
student.description = function() {
 return 'Gender: ' + this.gender + '; Name: ' + this.name;
};

Now student object can be used like:


console.log(student.description());
//Gender: Male; Name: Peter

We can create other, more specific student by using our sample object as a prototype.Think of this as cloning the object just like we took a chair and created a clone in the real world.


var stObj = Object.create(student);

We just created a stObj as a clone of the student. We can add properties and functions to this:


stObj.purr = function() {
 return 'Okay!';
};

We can use our stObj as a prototype and create a few more students:


var stObj1 = Object.create(stObj);
stObj1.name = 'Hello';

var stIbj2 = Object.create(stObj);
stIbj2.name = 'World';

console.log(stIbj2.description());

Class : How to write class based prototype in javascript let's see.


function Person() {
    //function Properties
    this.name = "Mac";
    this.age = "25";
    // start functions
    this.sayHi = function() {
        return this.name + " Says Hi";
    }
}
//Creating person instance
var p = new Person();
alert(p.sayHi());

Constructor : Constructor is a concept that comes under Classes.The constructor is used to assign values to the properties of the Class while creating an object using the new operator.


function Person(name, age) {
    //Now assigning values through constructor
    this.name = name;
    this.age = age;
    //functions
    this.sayHi = function() {
        return this.name + " Says Hi";
    }
}

//Creating person instance
var p = new Person("mac", 25);
alert(p.sayHi());
//Creating Second person instance
var p = new Person("john", 23);
alert(p.sayHi());

Inheritance : Inheritance is a process of getting the properties and function of one class to other class. For example, let’s consider "Student" Class, here the Student also has the properties of name and age which have been used in Person class. 


function studentClass() {}
studentClass.prototype = new Person();
studentClass.prototype = Object.create(Person);
var stobj = new studentClass();
alert(stobj.sayHi());

I hope you like the post please comment and sha

Featured post

How to create dynamic schema in mongodb

Sometime what we need in our application is to create mongodb schema for collection and some of our collection schema are same but names are...

Popular Posts