Dbabstraction

12
Google Apps Script Database Abstraction Use different back end databases with the same code contact me on g+

description

Here's how to abstract database access in Google Apps Script so you can use multiple back ends with the same code.

Transcript of Dbabstraction

Page 1: Dbabstraction

Google Apps Script Database AbstractionUse different back end databases with the

same code

contact me on g+

Page 2: Dbabstraction

Why database abstraction• Change backend databases without

recoding

• No need to learn or code for multiple APIs

• Easily copy between different back ends

• Use a spreadsheet, a file, a noSQL database, and others as the back end

• Caching and exponential back off built in

• Fairly simple to add new back ends

Page 3: Dbabstraction

ExamplesOnly 6 methods, queries by example

var result = handle.save({someObject});

var result = handle.query({country:’USA’,state:’NY’},{sort:’name’},{limit:100});

var result = handle.remove({name:’Doe’});

var result = handle.count({size:’big’});

var result = handle.get(id);

var result = handle.update(id,{someObject});

Initial version handles the most common simple queries and data, and are the same for all back ends

Page 4: Dbabstraction

Getting a handle - ScriptDb

A Google Apps ScriptDB var dbHandler = new cDataHandler.DataHandler (‘carriers’,

cDataHandler.dhConstants.DB.SCRIPTDB,

undefined,

'mydb',

ScriptDb.getMyDb());

if (!dbHandler .isHappy()) throw ("failed to get handle");

This will use the executing script’s db as the back end. The data will be siloed under the name ‘carriers’

Page 5: Dbabstraction

Getting a handle - orchestrate.io

Orchestrate.io var orchestrateHandler = new cDataHandler.DataHandler (‘carriers’,

cDataHandler.dhConstants.DB.ORCHESTRATE,

undefined,

'myorchestrate',

JSON.parse(UserProperties.getProperty("orchestrateKeys")));

This will use a collection called ‘carriers’ in orchestrate.io as a back end. The user executing this has his orchestrate API key stored in his user properties.

Page 6: Dbabstraction

Getting a handle - Drive

A JSON file on Google Drive

var driveHandler = new cDataHandler.DataHandler (‘carriers.json’,

cDataHandler.dhConstants.DB.DRIVE,

undefined,

'/datahandler/driverdrive');

This will use /datahandler/datadrive/carriers.json as a container for a single object acting as a database

Page 7: Dbabstraction

Getting a handle - Sheets

Google Sheets var sheetHandler = new cDataHandler.DataHandler (‘carriers’,

cDataHandler.dhConstants.DB.SHEET,

undefined,

'12pTwh5xxx0W4ZnGBiUI3yZY8QFoNI8NNx_oCPynjGYY’);

This will use a sheet called ‘carriers’ in a Google Spreadsheet with the given ID as the back end.

Page 8: Dbabstraction

Getting a handle - parse.com

parse.com var parseHandler = new cDataHandler.DataHandler (‘carriers’,

cDataHandler.dhConstants.DB.PARSE,

undefined,

'myparse',

JSON.parse(UserProperties.getProperty("parseKeys")));

This will use a collection called ‘carriers’ in parse.com as a back end. The user executing this has his parse API and developer key stored in his user properties.

Page 9: Dbabstraction

Getting a handle - fusion

Google Fusion var parseHandler = new cDataHandler.DataHandler

(‘tlc5z6Lek8K7vxxpXNUsOjX3qTbIsdXx9Fo’,

cDataHandler.dhConstants.DB.FUSION,

undefined,

'myfusion’);

This will use a the given fusion table as a back end.

Page 10: Dbabstraction

REST API access

All of this is also available through a Google Apps Script Webapp

webappurl?action=query&driver=scriptdb&query={"region":"Asia"}&siloid=play

This will do a query and return JSON (or JSONP if a callback is specified) corresponding to the query above on a scriptdb backend. This exposes all these different backends to anything that can deal with JSON and do GETS and POSTS. For example, here is how to manipulate a Google Sheet directly from Excel.

Page 11: Dbabstraction

Copying data between backends

Any combination of copying can be done, eg

var source = fusionHandler.query();

if(source .handleCode <0) throw (JSON.stringify(source ));

var result = driveHandler.remove();

if(result.handleCode <0) throw (JSON.stringify(result));

var result = driveHandler.save(source.data);

if(result.handleCode <0) throw (JSON.stringify(result));

This will copy all the data from fusion, delete the existing data from drive, and copy over the data retrieved from fusion - the result will be a JSON file of the fusion data on Google Drive

Page 12: Dbabstraction

More infoYou can find more about this here along with details of how to include this library in your Google apps script project

contact me on g+

see this on Drive

read more