Tutorial: Expose the api

Expose the api

A nice use of this proxy, is that it has an easy to use and easy to be serialized api syntax.
This api can be exposed to REST services in a few steps.

The database instance, provides a handle method that received a request and resolved its requested data. The api syntax is as follows:

fetching all entries
GET /users

fetching a single entry
GET /users/56802653a5261282c30acc28

fetching with a query
GET /users?q= + encodeStringified({firstName:'John'})

fetching the count for the query
GET /users/count?q= + encodeStringified({firstName:'John'})

posting data (with form data)
POST /users

updating an entry (with form data)
PUT /users/56802653a5261282c30acc28

deleting an entry
DEL /users/56802653a5261282c30acc28


Here, as an example, we will achive this with the expressJs routing system

        
    var express = require('express')
    var dbproxy = require('mongodb-proxy')        
         
    var app = express()
    var db = dbproxy.create({...})
        
    // listen for all requests under an '/api' location 
    app.all('/api/:collection*', function (req, res, next) {    
        // prepare an info object for the routing function     
        var route = {
            method: req.method,
            collection: req.params.collection,
            path: req._parsedUrl.pathname.substring('/api/'.length + req.params.collection.length),
            query: req.query.q,
            data: req.body,
            req: req,
            res: res
        }
        
        // get the post data 
        var postdata = ""        
        req.on('data', function (postdataChunk) {
            postdata += postdataChunk
        })  
        
        req.on('end', function () {
            var jsonData = JSON.parse(postdata || '{}')
            route.data = jsonData
            
            // pass the work on the proxy 
            db.handle(route, next, function (error, results) {
                if (error) {
                    if (typeof (error) === 'object') {
                        if (error.code && error.messages) {
                            res.status(error.code).send(error.messages)
                        } else {
                            res.status(500).send(error.message)
                        }
                    } else {
                        res.status(500).send(error)
                    }
                } else {
                    res.send(results)
                }
            })
        })
    })
    
    app.listen(3000)
        

Consuming the api with jQuery

    
    // posting 
    $.ajax({
        url: "/api/users",
        type: 'POST',
        dataType: 'json',
        data: JSON.stringify({
            firstName: 'John',
            lastName: 'Smith',
            password: 'test'
        })
    }).error(function (err) {
        throw err;
    }).done(function (results) {
        console.log(results);
    });
    
    // reading 
    $.ajax({
        url: "/api/users?q=" + encodeURIComponent(JSON.stringify({
            id: 'xx',
            $fields: ['firstName'],
            $options: {
                limit: 2
            },
            $single: true,
            $cached: true
        })),
    }).error(function (err) {
        throw err;
    }).done(function (results) {
        console.log(results);
    });
    
    // updating 
    $.ajax({
        url: "/api/users?q=" + encodeURIComponent(JSON.stringify({
            id: 'xx'
        })),
        type: 'PUT',
        dataType: 'json',
        data: JSON.stringify({
            lastName: 'Something else'
        })
    }).error(function (err) {
        throw err;
    }).done(function (results) {
        console.log(results);
    });
    
    // deleting 
    $.ajax({
        url: "/api/users?q=" + encodeURIComponent(JSON.stringify({
            id: 'xx'
        })),
        type: 'DELETE',
        dataType: 'json'
    }).error(function (err) {
        throw err;
    }).done(function (results) {
        console.log(results);
    });
    
    // counting 
    $.ajax({
        url: "/api/users/count?q=" + encodeURIComponent(JSON.stringify({
            firstName: 'John'
        })),
        type: 'GET',
        dataType: 'json'
    }).error(function (err) {
        throw err;
    }).done(function (results) {
        console.log(results);
    });