Driver MongoLab

This is the the driver for DB.MONGOLAB described in Database abstraction with google apps script 

The library reference is MPAHw_-cHNDxsYAg263J7Fai_d-phDA33
 

Background to implementation.

MongoLab offers a number of plans for cloud based MongoDB. I discovered they have a .5gb Free Sandbox plan, so I thought I'd give it a go. They recommend using their supplied libraries, but of course there is not one for Google Apps Script, so I used their REST API access to implement enough capabilities to cover all the features of Database abstraction with google apps script. Of course, MongoDB has zillions of other features that I don't use, but in this mode it's a great substitute for ScriptDB and alternative to Parse.com - and the performance is excellent

Authentication

It's simple - just an API key you can get from your MongoLab dashboard

Getting started

  • Sign up with MongoLab
  • Create a hosting plan
  • Create a database
  • Store your API key in your script Properties.

Getting a handle


Like all other drivers it all starts with getting a handle.  In this case, the MongoDB collection is 'customers', and the database I created is called 'xliberation'. The property is the one you stored your API key against, and should like this

{"restAPIKey":"your api key"}

  var userStore = PropertiesService.getScriptProperties();

  var handler = new cDataHandler.DataHandler (
      'customers',                        
      cDataHandler.dhConstants.DB.MONGOLAB,      
      undefined,
      'xliberation',
      JSON.parse(userStore.getProperty("mongoLabKeys")) );
     
  if(!handler.isHappy()) {
    throw ( 'unable to get mongolab handler');
  }

The example above uses the cDataHandler. Nowadays it's better to use the cDbAbstraction interface. An example is shown in the slides below. 

do something in 5 with gas-8-copy between databases


Now you're good to go. The syntax is the same for all back ends. See Some test cases for various backends for examples of various syntaxes, and see Comparing all back ends performance for how MongoLab performs. It's superfast as a scriptDB replacement.

The code

Here's some libraries you'll need or are used internally

library

key

comments

cDataHandler

Mj61W-201_t_zC9fJg1IzYiz3TLx7pV4j

Abstracted interface to back end databases, and all known drivers

cCacheHandler

M3reA5eBxtwxSqCEgPywb9ai_d-phDA33

Manages caching of query results

cNamedLock

Mpv7vUR0126U53sfSMXsAPai_d-phDA33

Cross script locking of abstract resources

cFlatten

MqxKdBrlw18FDd-X5zQLd7yz3TLx7pV4j

Flattens complex objects to 1 level dot syntax objects  so they can be stored/queries in a 2 dimensional space

See more like this in Database abstraction with google apps script



For help and more information join our forum,follow the blog or follow me on twitter .


Comments