Parse & JS: Counting objects

This entry is part 21 of 25 in the series Parse & Javascript

In SQL, we always use select count to get the number of records in a table.

For Parse, we can use the query count function.

<!doctype html>
<head>
    <title>My Parse App</title>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
    <script type='text/javascript' src='http://www.parsecdn.com/js/parse-1.2.17.min.js'></script>
</head>

<body>

<div>
    <div style='display:none' class='error'>
        <h1>Movie not found</h1>
    </div>

    <div style='display:none' class='success'>
        <h1>Movie Found</h1>
    </div>
</div>

<script>
    Parse.initialize('5d3sro43zBGj8AMYqWND5bFYMzAan7f5P5euZm7M', 'hlRZZqAylA7Fmah4Kk6ma8WK7QCT4cHrxjafnnkI');
    var MoviObject = Parse.Object.extend('MoviObject');
    var moviObject = new MoviObject();

    var query = new Parse.Query(MoviObject);

    query.equalTo('releaseYear', 2013);

    query.count({
        success: function(count) {
            // The count request succeeded. Show the count
            document.write("No of movies released in the year 2013:  " + count);
        },
        error: function(error) {
            // The request failed
        }
    })
</script>

</body>
</html>
Try The Code

Parse & JS: Relational data

This entry is part 22 of 25 in the series Parse & Javascript

As with other relational database, objects may have relationships with other objects.

For example, in a blogging application, a Post object may have many Comment objects.

Let’s look at how we can create a one-to-one relationship in Parse.

<!doctype html>
<head>
    <title>Codecrawl.com</title>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
    <script type='text/javascript' src='http://www.parsecdn.com/js/parse-1.2.17.min.js'></script>
</head>

<body>

<script>
    Parse.initialize('kYqz3wRHRa0GDX7viRIVXKDVNIaI0q7Fw6Ms3pv2', 'yPjRYQmwZC2MthUPrer1mU9dDpr84xVQjmEtT7fS');

    // Declare the types.
    var Post = Parse.Object.extend('Post');
    var Comment = Parse.Object.extend('Comment');

    // Create the post
    var myPost = new Post();
    myPost.set('title','I am Hungry');
    myPost.set('content', 'Where should we go for lunch?');


    myPost.save(null, {
        success: function(myComment) {
            // The object was saved successfully.
            document.write('Post Saved Ok');
            document.write('<br>');
            var id = myPost.id;
            
            // Create the comment            
            var myComment = new Comment();
            myComment.set('content', 'Let us do Sushirrito.');
            
            myComment.set('parentId', id);
            myComment.set('parent', myPost);

            myComment.save(null, {
                success: function(myComment) {
                    // The object was saved successfully.
                    document.write('Comment Saved OK');
                },
                error: function(myComment, error) {
                    // The save failed.
                    // error is a Parse.Error with an error code and description.
                    document.write('Comment Saved Not Ok');
                }
            });

        },
        error: function(myComment, error) {
            // The save failed.
            document.write('Post Saved Not Ok');
        }
    });

</script>
Try The Code

After the first run, you can see Post and Comment objects created in the App.

relation1

If you click on Comment object, you will see a parent column with objectid of the parent, which is the Post object.

This is a actually a pointer to the parent.  If you click on it, you will be brought to the parent object.

I have also kept the objectId of the parent in the parentId column of Comment object.

parent

Parse & JS: Retrieving comment using the parentId

This entry is part 23 of 25 in the series Parse & Javascript

From the parent, we could retrieve the objectId.

Since we have this objectId in each comment object, we can retrieve the relevant comment of the Post.

<!doctype html>
<head>
        <title>My Parse App</title>
        
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
        
    <script type='text/javascript' src='http://www.parsecdn.com/js/parse-1.2.17.min.js'></script>
</head>
 
<body>
 
<div>
        
    <div style='display:none' class='error'>
                <h1>Movie not found</h1>          
    </div>
            
    <div style='display:none' class='success'>
                <h1>Movie Found</h1>            
    </div>
</div>
 
<script>
    Parse.initialize('kYqz3wRHRa0GDX7viRIVXKDVNIaI0q7Fw6Ms3pv2', 'yPjRYQmwZC2MthUPrer1mU9dDpr84xVQjmEtT7fS');
    var Comment = Parse.Object.extend('Comment');
    var comment = new Comment();

    var query = new Parse.Query(Comment);

    query.equalTo('parentId', 'NBQjz5Rr2y');

    query.find(
            {
                success: function (comment) {
                    $('.success').show();
                    var content = comment[0].get('content');
                    alert(content);
                }
            }
    );

</script>
 
</body>
</html>
Try The Code

Parse & JS: Return only one object

This entry is part 24 of 25 in the series Parse & Javascript

If you want exactly one result, a more convenient alternative may be to use first instead of using find.

<!doctype html>
<head>
    <title>My Parse App</title>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
    <script type='text/javascript' src='http://www.parsecdn.com/js/parse-1.2.17.min.js'></script>
</head>

<body>

<script>
    Parse.initialize('5d3sro43zBGj8AMYqWND5bFYMzAan7f5P5euZm7M', 'hlRZZqAylA7Fmah4Kk6ma8WK7QCT4cHrxjafnnkI');
    var MoviObject = Parse.Object.extend('MoviObject');
    var moviObject = new MoviObject();

    var query = new Parse.Query(MoviObject);

    query.first(
            {
                success: function (moviObject) {
                    document.write('<h2>First Movie Found:</h2>');
                    var title = moviObject.get('titleName');
                    document.write(title);
                }
            }
    );
</script>
</body>
</html>
Try The Code

As there is only one object, moviObject will not be in array form and so to get the title, we need only to use moviObject.get().

Parse & JS: Queries on String

This entry is part 25 of 25 in the series Parse & Javascript

Use startsWith to restrict to string values that start with a particular string.

Similar to MySQL LIKE operator, this is indexed so it is efficient for large dataset.

<!doctype html>
<head>
    <title>My Parse App</title>
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js'></script>
    <script type='text/javascript' src='http://www.parsecdn.com/js/parse-1.2.17.min.js'></script>
</head>

<body>

<script>
    Parse.initialize('5d3sro43zBGj8AMYqWND5bFYMzAan7f5P5euZm7M', 'hlRZZqAylA7Fmah4Kk6ma8WK7QCT4cHrxjafnnkI');
    var MoviObject = Parse.Object.extend('MoviObject');
    var moviObject = new MoviObject();

    var query = new Parse.Query(MoviObject);

    query.startsWith('titleName', 'Scent');

    query.find(
            {
                success: function (moviObject) {
                    document.write('<h2>Movie Found:</h2>');
                    var title = moviObject[0].get('titleName');
                    document.write(title);
                }
            }
    );
</script>
</body>
</html>
Try The Code

startwith