Thursday, June 26, 2014

Working With Parse.com For Mobile Application Data Storage - Javascript API Connecting NoSql Datastore

Nowadays we are always tempt to use mobile applications on Android/iOS/ BBos and Windows 8 and etc...where these apps have to store the data for future reference.Particularly concentrating HTML5 apps powered by javascripts are more powerful and easy to build across different platforms.The User data is better to store in cloud and could be searched with the help of API provided.although Parse stands Easy for me for saving data,Where Bluemix Also supports that ,I have tried Parse since it gives Class-Object approach to save data with few lines of Javascript code.And Application can be easily scalable and Searched efficiently.Not Only mobile applications,Apps that is data-driven can also use this feature for easy storage and tension free and forget about Database design,Support and code for connecting database.Where parse provides simply efficient datastore with Access Tokens and Application ID and save your data.And it is also part of Facebook :)

Working With Parse.com For Mobile Application Data Storage - Javascript API Connecting NoSql Datastore
parse.com - i-visionblog - Mobile apps Storage

Scope:

This Post can Give you a Experience to store data as Objects via Class using Javascript API provided by Parse.com.Not only Data, You could upload files and others too save in the Datastore,with few lines of Javascript Code and Application ID and Secret Code.

Installation :

As Like Normal Javascript include it in normal part and You can Download from Official Parse Site.click to Download

                  <script src="./parse-1.2.18.js"></script>

Understanding the Concept Of Saving Data : 

Storing data on Parse is built around Parse.Object. Each Parse.Object contains key-value pairs of JSON-compatible data. This data is schemaless, which means that you don't need to specify ahead of time what keys exist on each Parse.Object. You simply set whatever key-value pairs you want, and our backend will store it.
Create a Object by extending the Parse.Object.extend and this could create new class and then we could save as Keys must be alphanumeric strings. Values can be strings, numbers, booleans, or even arrays.

example for raw data :

                    email: "s.shivasurya@gmail.com", phone: 9788012345

JavaScript API to Handle Data :

I have Chosen JQuery to Handle my data and operation to perform with Parse javascript functions with callbacks,since it is basically network operation.

1) Create Application in Parse.com  and You may receive Application ID and Secret Keys for various platforms for your application.

Initialize Your Application to Connect Parse :
First Initiative step is to save the Data as objects with particular class in Parse.com.So,Create a Class in parse dashboard.

2) just use Parse.initialize method to configure your application and get connected to the Parse Datastore.

Parse.initialize("Your App ID", "Your Javascript Key");

CRUD functionality:

3) First let us see how to create a Row(Record) with unique object ID that contains User Data, and by default it has Created Time ,Updated Time with ACL(Access Control List).

To Create a record just extend the parse object with suitable created class and create object with that and invoke the function save to save our data in Datastore.Well see the example below.

   var createobject = Parse.Object.extend("users");
   var objectwithdata = new createobject();
   objectwithdata.save({email: email,phone: phonenumber},{
         success: function(object) {
         console.log("success");
         $("#load").hide();
         },
        error: function(model, error) {
         console.log("error");
           }
    });

In above code we have just extended to class user and created object of User class.and we have added few data by invoking a function save() with a callback function with success and Error handlers.since it is time consuming operation to save data in parse.so whenever the return is success a row is added automatically with Object ID ,it is better to retrieve and keep it in HTML5 storage for later queries and other operations.

Retrieve Data : 


   var retreiveobject = Parse.Object.extend("users");
   var retrievedata = new retreiveobject();
   retrievedata.get("Your Object ID for Particular Client",{
         success: function(object) {
         console.log("success");
         console.log(object);
         },
        error: function(model, error) {
         console.log("error");
           }
    });

In above code we have just extended to class user and created object of User class.and we have read few data by invoking a function get() with a callback function with success and Error handlers.

Update Data :


   var retreiveobject = Parse.Object.extend("users");
   var retrievedata = new retreiveobject();
   retrievedata.get("Your Object ID for Particular Client",{
         success: function(object) {
         console.log("success");
         console.log(object);
         retrievedata.set("email","suriya1180@gmail.com");
         retrievedata.set("phonenumber",9876543210);
         retrievedata.save();
         },
        error: function(model, error) {
         console.log("error");
           }
    });

In the above method we had retrieved the data and we just used set() function to update values and finally call save() function to update the data in database.

Delete Object :

   var destroyclass = Parse.Object.extend("users");
   var destroyobject = new destroyclass();
   destroyobject.destroy("Your Object ID for Particular Client",{
         success: function(object) {
         console.log("successfully deleted");
         },
        error: function(model,error) {
         console.log("error");
           }
    });

In the above method we have just called the destroy() function to destroy the row of particular Object ID from Datastore.
There are still Lot arrays,Datatypes,queries,compound query,relating data and so on.visit here to Learn more

My sample :

I have created a class named users with phone numbers and email storing data in parse and when success function runs it is redirected to List.html file in my mobile application.

       //consider jquery.js included
     <script src="js/parse-1.2.18.js"></script>
     <script>
   
        $(document).ready(function(){
   
        $("#login").click(function()
            {
                var email=$("#email").val();
                var phonenumber=$("#phonenumber").val();
                console.log(email);
                console.log(phonenumber);
                $("#search").hide();
                $("#load").show();
                Parse.initialize("My APP ID", "My javascript Secret Key");
                var TestObject = Parse.Object.extend("users");
                var testObject = new TestObject();
                testObject.save({email: email,phone:phonenumber},{
                  success: function(object) {
                   console.log("success");
                  $("#load").hide();
                   window.location="./list.html";
                },
                error: function(model, error) {
                console.log("error");
      }
});        
 }
        );
            });
    </script>


My simulated application results:

I had made a simple form to trigger with sign up button and call parse.save function to save data in Datastore.

Cautions:

1)Always concentrate Errors on Console Log of chrome to minimize the Errors

2)However it is javascript client code it is showing both keys public manner.so just use ACL and permissions,Sessions to prevent Illegal data transactions in Parse Dashboard.

3)Handle the Errors and print the errors to guide the project to success.




My result:

my Class with Data populated on Parse.

Beware of Securing the Data.Just Try out Parse for Mobile apps and make use of Cloud code for efficient scaling.For further doubts/Bugs/reports Mail me s.shivasurya@gmail.com or let us disqus as comment below.Share is care

SHIVA SURYA

I love to work on WAMP/LAMP/XAMPP/Cloud Apps/Eclipse platform/Node.js ,i love to create PHP API centric WEB/MOBILE application.The age changes from Desktop to Smartphone Apps,I too Love to create Native Android and Phonegap apps with JQuery Mobile,Ionic,Bootstrap framework as FrontEnd with REST API service at backend.Nowadays I love JAVA too. :) Contact me via s.shivasurya@gmail.com

0 comments:

Post a Comment

feel free to post your comments!

 

Copyrighted.com Registered & Protected  Z6HO-DGFO-S2CO-JRZGCopyrights Protected (C)2013-2014 i-visionblog ~ s.shivasurya.