Google ScriptDB: How to retrieve a Key Value Pair using a variable?

327 views Asked by At

I am using Google UI Apps and ScriptDB, and I have a map object similar to this:

myMapObject = {record_id: projectA,
               apple : 316,
               orange : 956,
               banana : 536}

I am wondering how to use a variable in a query to retrieve the key value pair data. Or, is it even possible? For example:

var userChoice = e.parameter.choice;
var userLimit = e.parameter.limit;
var result = db.query({userChoice : db.greaterThan(userLimit)})

Now, I know using a variable works for the conditional statement, db.greaterThan(userLimit). However, I haven't been able to use a varriable (i.e. "userChoice") in the way I have written it for the key value.

Note, that this is only a simplified example of the code and I am not looking for a way to restruction the map object. What I would like to know is, if it is possible to use a varriable in some way to perform the query. Thanks for your help.

2

There are 2 answers

2
Henrique G. Abreu On BEST ANSWER

You can use a variable as property, but it's a different syntax. Like this:

var query = {};
query[userChoice] = db.greaterThan(userLimit);
//edit as requested in the comment
query.record_id = 'projectA'; //or query['record_id'] = ...
var result = db.query(query);
0
Mogsdad On

Yes, it's possible to use a variable to perform a query. Remember, though: The key value must be present in the object you're querying. In your example, userChoice is not a property of myMapObject, so it won't match.

Here's an example related to those in the ScriptDb Service Documentation. The findOlderThan() function accepts a parameter age which is used in a query of people objects. Those people objects have 4 properties, type, name, age, and single. Therefore, a query that expects to extract people must reference one or more of those properties. Since ScriptDb is agnostic about the objects you store in it, we use of the type property as a convention to identify object types - and we do that in every query. We could just query for age, but that could also find objects that aren't people, which we aren't interested in.

function findOlderThan( age ) {
  var db = ScriptDb.getMyDb();
  var people = [];  // array to hold results

  // Find people with an age greater than given value.
  var results = db.query({
    type: 'person',
    age: db.greaterThan(age)
  });
  while (results.hasNext()) {
    var item = results.next();
    people.push(item);
  }
  return people;
}

function test_findOlderThan() {
  deleteAll(); // from developers.google.com/apps-script/guides/script-db/patterns#delete_all_items_in_the_database
  loadDb();
  Logger.log(JSON.stringify(findOlderThan(20))); // 3 results
  Logger.log(JSON.stringify(findOlderThan(25))); // 2 results
  Logger.log(JSON.stringify(findOlderThan(50))); // 1 result
  Logger.log(JSON.stringify(findOlderThan(100))); // no result
}

function loadDb() {
  var db = ScriptDb.getMyDb();
  var item = db.save({
    type: 'person',
    name: 'fred',
    town: 'Shelbyville',
    age: 25,
    single: true
  });
  item = db.save({
    type: 'person',
    name: 'joe',
    town: 'Springfield',
    age: 45,
    single: false
  });
  item = db.save({
    type: 'person',
    name: 'sam',
    town: 'Springfield',
    age: 90,
    single: true
  });
}