Documentation Home -  API Home  -  Database object

DatabaseKeyList

Represents the set of keys used to index the data in a database object. The keys can be retrieved as an array, and can be sorted by the key name, the date that the value was initially created in the database, or when it was last updated. The sort orderings can be reversed. The number of keys (and thus values) in the database can also be obtained.

function logDatabaseContents() {
   // Retrieve the DatabaseKeyList object
   var index = database.keys();

   // Call the list() method to retrieve the key value array
   var keys = index.list();

   // Iterate over the array and log the keys and associated values
   for( var key in keys ) {
      var value = database.load(key);
      log.info('Key: ' + key + ', Value: ' + value);
   }
}

As shown in the example above this object is commonly used when you want to iterate over the values in the database - by obtaining the keys and then loading and processing their associated values.

Functions

DatabaseKeyList.reverse()

function
Returns
DatabaseKeyListReturns a "reversed" version of the DatabaseKeyList object

Retrieves a reversed version of the DatabaseKeyList object. Subsequent calls to list(), created(), or updated() will return values in the reverse of the documented order and the start and max parameters will work according to this new ordering.

function logLastTenLexicalEntries() {
   var index = database.keys(10);

   var rindex = index.reverse();

   var keys = index.list();
   for( var key in keys ) {
      var value = database.load(key);
      log.info('Key: ' + key + ', Value: ' + value);
   }
}

Calling reverse() on an already-reversed DatabaseKeyList returns the original un-reversed object.

DatabaseKeyList.list(start,max)

function
Parameters
startnumber(optional) The zero based index of the first record to be returned
maxnumber(optional) The maximum number of records to return
Returns
string[]The ordered array of key values

With no parameters all keys in the database are returned in lexical order.

With one parameter a maximum of max keys are returned in lexical order.

With two parameters a maximum of max keys are returned in lexical order starting with record number start according to the same ordering.

DatabaseKeyList.created(start,max)

function
Parameters
startnumber(optional) The zero based index of the first record to be returned
maxnumber(optional) The maximum number of records to return
Returns
string[]The ordered array of key values

With no parameters all keys in the database are ordered according to the key's date of creation.

With one parameter a maximum of max keys are returned in creation order.

With two parameters a maximum of max keys are returned in creation order starting with record number start according to the same ordering.

DatabaseKeyList.updated(start,max)

function
Parameters
startnumber(optional) The zero based index of the first record to be returned
maxnumber(optional) The maximum number of records to return
Returns
string[]The ordered array of key values

With no parameters all keys in the database are ordered according to the date of the key's most recent update.

With one parameter a maximum of max keys are returned in update order.

With two parameters a maximum of max keys are returned in update order starting with record number start according to the same ordering.

DatabaseKeyList.count()

function
Returns
numberThe number of records represented by this DatabaseKeyList object

Returns the number of records that would be returned by calls to list(), created(), or updated().