Search This Blog

Saturday, December 30, 2017

Node.js looping through 2-dimensional jagged array

consider following jagged array structure which is place holder for routing data collected to to organize express routing details

var sample = {
    'get': [
        ['api/spaceships', 'get all'],
        ['api/spaceships/:id', 'get filtered list']
    ],
    'post': [
        ['api/spaceships', 'get all'],
        ['api/spaceships/:id', 'get filtered list']
    ]
}

javascript code below will loop through and print values

Code:
    console.log('-------------------------------------------------');
    for (var j = 0; j < Object.keys(sample).length; j++) {
        console.log('HTTP Method :' + Object.keys(sample)[j]);
        for (var i = 0; i < sample[Object.keys(sample)[j]].length; i++) {
        console.log('-----------Route--------------------');
        console.log('Key:' + sample[Object.keys(sample)[j]][i][0]);
        console.log('Value:' + sample[Object.keys(sample)[j]][i][1]);
        console.log('-----------Route--------------------');
        }
        console.log('-----------Next Iteration---------------------');
    }


Output:

    -------------------------------------------------
    HTTP Method :get
    -----------Route--------------------
    Key:api/spaceships
    Value:get all
    -----------Route--------------------
    -----------Route--------------------
    Key:api/spaceships/:id
    Value:get filtered list
    -----------Route--------------------
    -----------Next Iteration---------------------
    HTTP Method :post
    -----------Route--------------------
    Key:api/spaceships
    Value:get all
    -----------Route--------------------
    -----------Route--------------------
    Key:api/spaceships/:id
    Value:get filtered list
    -----------Route--------------------
    -----------Next Iteration---------------------

Friday, December 29, 2017

Quick Start : MongoDb on Ubuntu 17.04

MongoDb Installation:

Install MongoDb (with super user)
    apt install  mongodb-server

Status of mongodb service
    systemctl status mongodb
    systemctl restart mongodb
        or
    service mongodb start
    service mongodb stop
    service mongodb status

Auto Start service
    systemctl enable mongodb

Version Check
    mongod --version

Configuration File:
    /etc/mongodb.conf

Getting Started with Mongo:

Start Mongo CLI:
  mongo

Insert JSON:
    db.mycollection.insertMany([
    {fname:"john",mname:"joe",lname:"doe",skill:["c#","javascript","sql"]},
    {fname:"mary",mname:"joe",lname:"doe",skill:["ccna","ccnp","cci","operation research"]},
    ])

View Inserted Json:
        db.mycollection.find({}).pretty();
    Output:
        {
            "_id" : ObjectId("5a4634cf26837e3337746881"),
            "fname" : "john",
            "mname" : "joe",
            "lname" : "doe",
            "skill" : [
                "c#",
                "javascript",
                "sql"
            ]
        }
        {
            "_id" : ObjectId("5a4634cf26837e3337746882"),
            "fname" : "mary",
            "mname" : "joe",
            "lname" : "doe",
            "skill" : [
                "ccna",
                "ccnp",
                "cci",
                "operation research"
            ]
        }
Filter Inserted Json on single property:
        db.mycollection.find({"fname":"john"}).pretty();
    Output:
        {
            "_id" : ObjectId("5a4634cf26837e3337746881"),
            "fname" : "john",
            "mname" : "joe",
            "lname" : "doe",
            "skill" : [
                "c#",
                "javascript",
                "sql"
            ]
        }

Filter with contains:
        db.mycollection.find({skill:{$in:["c#","ccna"]}})
    output:
        { "_id" : ObjectId("5a4634cf26837e3337746881"), "fname" : "john", "mname" : "joe", "lname" : "doe", "skill" : [ "c#", "javascript", "sql" ] }
        { "_id" : ObjectId("5a4634cf26837e3337746882"), "fname" : "mary", "mname" : "joe", "lname" : "doe", "skill" : [ "ccna", "ccnp", "cci", "operation research" ] }

Select few Property instead of all:
        db.mycollection.find({skill:{$in:["c#","ccna"]}},{fname:1,mname:1,lname:1})
    output:
        { "_id" : ObjectId("5a4634cf26837e3337746881"), "fname" : "john", "mname" : "joe", "lname" : "doe" }
        { "_id" : ObjectId("5a4634cf26837e3337746882"), "fname" : "mary", "mname" : "joe", "lname" : "doe" }

Quick Notes : Installing PgAdmin4 on ubuntu 17.04 as Service

On Ubuntu 17.04 Linux Terminal

Switch to Home Directory of non-super user
    cd $HOME

Install dependencies for python virtual environment

    sudo apt-get install virtualenv python-pip libpq-dev python-dev

Create Virtual environment
    virtualenv pgadmin4

Switch to base folder of virtual environment
    cd pgadmin4

Activate Virtual environment   
    source bin/activate

Download wheel for pgAdmin4
    wget https://ftp.postgresql.org/pub/pgadmin/pgadmin4/v2.0/pip/pgadmin4-2.0-py2.py3-none-any.wh

Install Wheel
    pip install pgadmin4-2.0-py2.py3-none-any.wh

Location of executable pgAdmin4.py is bit confusing,its in /usr/local/lib/python2.7/dist-packages/pgadmin4

running in Desktop Mode (optional):
  /usr/local/lib/python2.7/dist-packages/pgadmin4/config.py

    locate server setting section inside it there will be text as follows

        if builtins.SERVER_MODE is None:
            SERVER_MODE = True
        else:
            SERVER_MODE = builtins.SERVER_MODE

     just below it set
        SERVER_MODE = False

Now run pgAdmin4 as follows
    python /usr/local/lib/python2.7/dist-packages/pgadmin4/pgAdmin4.py

    it will ask for creating pgadmin4 username & password.

PgAdmin4 run on http://localhost:5050 where you can login using just provided login details.
Add your postgres server by right clicking on server new.

Creating service for PgAdmin4

create a file at /etc/systemd/system/pgadmin4.service

add following content into it,Here
/home/sangram/pgAdmin4/pgAdmin4 is path to my python virtual environment.

[Unit]
Description=Pgadmin4 Service
After=network.target

[Service]
User=sangram
Group=sangram
WorkingDirectory=/home/sangram/pgAdmin4/pgAdmin4
Environment="PATH=/home/sangram/pgAdmin4/pgAdmin4/bin"
ExecStart=
/usr/local/lib/python2.7/dist-packages/pgadmin4/pgAdmin4.py

PrivateTmp=true

[Install]
WantedBy=multi-user.target 


Now start newly created service as

        sudo systemctl start pgadmin4 

to make service autostart 

         sudo systemctl enable pgadmin4

you can check service status by 


         sudo systemctl status pgadmin4

Now we can check if localhost:5050/ for pgAdmin4.