google cloud datastore driver for google apps script db abstraction

18
Google cloud datastore Database abstraction – Google Apps Script Desktop liberation

Upload: bruce-mcpherson

Post on 21-Jan-2015

568 views

Category:

Data & Analytics


1 download

DESCRIPTION

This is the Google Cloud Datastore driver for Apps Script. Gives a standard query and database update interface to multiple back end databases

TRANSCRIPT

Page 1: Google cloud datastore driver for Google Apps Script DB abstraction

Google cloud datastore Database abstraction – Google Apps Script

Desktop liberation

Page 2: Google cloud datastore driver for Google Apps Script DB abstraction

What is the Cloud Datastore A massively scalable cloud based datastore Usually used along with Google App Engine APIs available for Python, Go, Java, Php A JSON API exists Has an SQL like query language (GQL) GQL is query only Only really supports flat level objects Free daily quota can be eaten up fairly

quickly Many capabilities, fairly complex It’s a database Jim, but not as we know it

Page 3: Google cloud datastore driver for Google Apps Script DB abstraction

Datastore with GAS

There is no API for Google Apps Script

Have to use the JSON API, which is complex and limited

Complex queries need pre-specified indexes

It’s conceptually different to normal databases and structures

Oauth2 is mandatoryDatabase abstraction aims to simplify

all that

Page 4: Google cloud datastore driver for Google Apps Script DB abstraction

Abstraction objective

With dbAbstraction, DataStore noSQL queries are the same for all supported backends. So far this includes

Scriptdb Parse.com Fusion Orchestrate.io Fusion Sheets Import.io Drive Properties DataStore

Page 5: Google cloud datastore driver for Google Apps Script DB abstraction

cDataStore driver componenetsfor GAS

Your Script cDataHandler library

cEzyOauth2 library

cCacheHandler library

Your webapp

App template

Your property store

cDataStore driver

Cloud DataStore

cFlattenerlibrary

noSQL queries

Access token

Google oauth2

infrastructure

Query caching

cNamedLocklibrary

Transaction locking

JSON API

Query translation

authentication

Cloud credentials

Access & refresh tokens

(un)Flatten objects

Page 6: Google cloud datastore driver for Google Apps Script DB abstraction

Datastore abstraction

The same as other database backends up to the driver

Caching, locking and oAuth2 built in Complex objects supported through

flattening with dot syntax Complex queries handled by

DataStore when index exists Non indexed queries automatically

handled by driver filtering

Page 7: Google cloud datastore driver for Google Apps Script DB abstraction

Example queries/counts

handler.query ( ); handler.query ({sex:”male”} ); handler.query ({sex:”male”,country:”USA”} ); handler.query ({sex:”male”,network:{google:{usage:”seldom”}}});Handler.query({sex:”male”,”network.google.usage”:”seldom”});Handler.query({sex:”female”,handler.constraint([[c.GT,50],[c.LT,60]])});Handler.query({},{limit:20,sort:”-age”});Handler.query({sex:”female”, age:handler.constraint([[c.GT,50],[c.LT,60]] ,

name:handler.constraint([[c.IN,[”smith”,”jones”]] )}, {sort:”network.google.usage”});handler.count ({sex:”male”} );

Page 8: Google cloud datastore driver for Google Apps Script DB abstraction

Example removes

handler.remove ( ); handler.remove ({sex:”male”} ); handler.remove({query ({sex:”male”,country:”USA”} ); handler.remove({sex:”male”,network:{google:{usage:”seldom”}}});Handler.remove({sex:”male”,”network.google.usage”:”seldom”});Handler.remove({sex:”female”,handler.constraint([[c.GT,50],[c.LT,60]])});Handler.remove({},{limit:20,sort:”-age”});

Page 9: Google cloud datastore driver for Google Apps Script DB abstraction

Example saves

handler.save({name:”john”,age:25,country:”USA”,network:{google:”seldom”,facebook:”often”}} );

handler.save([{name:”john”,age:25,country:”USA”,network:{google:”seldom”,facebook:”often”}}, name:”mary”,age:28,country:”UK”,network:{google:”often”,facebook:”never”}}]);

Page 10: Google cloud datastore driver for Google Apps Script DB abstraction

Example update

var result = handler.query ({network.google:”seldom”},undefined,1, true ); result.data.forEach(function(d) { d.target = true; });handler.update (result.handleKeys, result.data);

var result = handler.query ({network.google:”often”},{sort:”age”,limit:10},1, true); result.data.forEach(function(d) { d.target = true; });handler.update (result.handleKeys, result.data);

Page 11: Google cloud datastore driver for Google Apps Script DB abstraction

Example cache

var resultFromCache = handler.query ({network.google:”seldom”});var resultNoCache = handler.query

({network.google:”seldom”,undefined, 1});

Page 12: Google cloud datastore driver for Google Apps Script DB abstraction

Oauth2 pattern

A standard recipe

function doGet (e) { return doGetPattern(e, constructConsentScreen, doSomething,

'googleDatastore') ;}

1. The name of a function that returns an html string with a user consent dialog. Will only get called the first time doGet() is run to kick off the self maintaining access/refresh token details

2. The name of a function that does what your doGet() would normally do, and returns what it would normally return

3. A unique name against which to store the access/refresh token details in the property store

1 2 3

Page 13: Google cloud datastore driver for Google Apps Script DB abstraction

Credentials

Your datastore credentials and scopes should be stored in your properties store

EzyOauth2 will update your credentials object with refresh and access token information

Every time after the first time run, your access token will be refreshed automatically when necessary

Functions are provided in the pattern for your function to read and write properties, but the libraries never access your properties directly.

Page 14: Google cloud datastore driver for Google Apps Script DB abstraction

First time run

The ongoing pattern is more or less the same for both webapp and non-webapp

doGet() needs to be run at least once to provoke an authorization dialog

Non- webapps need a doGet() function run just once to get the refresh token infrastructure set up. You can delete the doGet() function after that.

Page 15: Google cloud datastore driver for Google Apps Script DB abstraction

The credentials

Code is in the Patterns example, and looks like this. Substitute your own credentials and run

function oneTimeSetProperties () { setAuthenticationPackage_ ({ clientId : “xxx.apps.googleusercontent.com", clientSecret : “xxx", scopes : ['https://www.googleapis.com/auth/datastore','https://www.googleapis.com/auth/

userinfo.email'], service: 'google', packageName: 'googleDatastore' });}

Page 16: Google cloud datastore driver for Google Apps Script DB abstraction

Credentials - properties store

Substitute your credentials and run this , one timefunction oneTimeSetProperties () { setAuthenticationPackage_ ({ clientId : “xxx.apps.googleusercontent.com", clientSecret : “xxx", scopes : ['https://www.googleapis.com/auth/datastore','https://www.googleapis.com/auth/userinfo.email'], service: 'google', packageName: 'googleDatastore' });}

This will get created

Page 17: Google cloud datastore driver for Google Apps Script DB abstraction

Library Keyslibrary key comments

cDataHandler Mj61W-201_t_zC9fJg1IzYiz3TLx7pV4j

Abstracted interface to back end databases, and all known drivers

cCacheHandler MXhfe1Z1GKU-_Qnd94LqcsKi_d-phDA33

Manages caching of query results

cNamedLock MBaYiatjgCSvDcsG6fHIFsyz3TLx7pV4j

Cross script locking of abstract resources

cEzyOauth2 MSaYlTXSVk7FAqpHNCcqBv6i_d-phDA33

Manages oAuth2 credentials and offline refreshing of access tokens

cFlatten MqxKdBrlw18FDd-X5zQLd7yz3TLx7pV4j

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

ezyOauth2 templates https://script.google.com/d/1ll5nvMOZL5YODaj71l0-XSaD0BBciG_zIV2I0Neu3Nz1LaKY6-4WiJAt/edit?usp=sharing

Patterns for web and nonweb apps for easy oath2 authentication. Take a copy of this