Query scriptDB from GAS

Following on from Query parse.com from GAS,  we'll query the colortable  created in Updating ScriptDB. As you would expect, this is just a few lines of code.


scriptdb specific code


It needs a few basic utility functions as used all over on this site from the mcpher shared library.


It's worth a few words on the queryArray() method of scriptdb silo. In order to harmonize the number of fetches required to get a lot of data between parse and scriptdb (so they can be compared), I'm using this method to handle getting the data in batches, up to the limit specified. This also gets over any default limits by either approach. Here's the code - its simply returns an array of size (limit) or everything matching if limit is 0. It handles the batching up process.

cScriptDbSiloItem.prototype.queryArray = function(ob, optLimit) {
  
  // this is the max I'll take in one go to be compatible with parse.com
  var maxLimit = 1000;
  var limit = fixOptional ( optLimit, 0);
  var results =[], q = this.querySet(ob);
  while(true) {
    var qr =  this.db()
                .query(q)
                .limit(limit > 0 ? limit-results.length : maxLimit).startAt(results.length);
    // we're done
    if (!qr.hasNext()) return results;
    // still have pushing to do
    while(qr.hasNext() && (limit==0 || results.length < limit)) results.push(qr.next());
  }
  
};

The results

Here's a live query. You can get jSonp also by specifying a callback parameter. This will get the first two entries from the dulux color scheme, this time using scriptdb as the provider

https://script.google.com/macros/s/AKfycbzSdgK85uGHdQ9m076QkPV0B9a2kkgh7JHDmV8kzRgtkriSIwTn/exec?limit=2&scheme=dulux&provider=scriptdb

For more about parse.com, see Parse.com
For help and more information join our forum,follow the blog or follow me on twitter .

You want to learn Google Apps Script?

Learning Apps Script, (and transitioning from VBA) are covered comprehensively in my my book, Going Gas - from VBA to Apps script, All formats are available from O'ReillyAmazon and all good bookshops. You can also read a preview on O'Reilly

If you prefer Video style learning I also have two courses available. also published by O'Reilly.
Google Apps Script for Developers and Google Apps Script for Beginners.


For help and more information join our forumfollow the blogfollow me on twitter
Comments