Hide
Apps Script

Migrating from ScriptDB to another Database

ScriptDB, an experimental NoSQL database technology in Apps Script, was deprecated on May 15, 2014, and will be turned off on November 20, 2014. This guide explains how to export your data from ScriptDB and provides a few options for other databases you can use.

Exporting data from ScriptDB

The function below can export ScriptDB records as a JSON file saved to Google Drive. To use it, copy the code into any project that uses ScriptDB, then run the function exportScriptDb.

After exporting, you will need to convert the JSON into a format appropriate for the new database you select and update your script to use that database instead of ScriptDB.

/**
 * Exports the contents of the ScriptDB database to a series of JSON files.
 * Each export has its own folder, and the files in that folder contain the
 * JSON equivalents of each record, one record per line. If the export
 * function times out before it can complete, this function throws an error
 * instructing you to run the function again so that it can pick up where it
 * left off.
 */
function exportScriptDb() {
  // The name of the folder to export to. Change as needed.
  var EXPORT_FOLDER_NAME = 'Export-' + new Date().toISOString();

  // The name of the property that holds the next page number to export.
  var PAGE_NUMBER_PROPERTY = 'scriptDbExport.pageNumber';

  // The name of the property that holds the ID of the folder to export to.
  var FOLDER_ID_PROPERTY = 'scriptDbExport.folderId';

  // The amount of time, in milliseconds, that the script can run for before
  // it is stopped (5 minutes).
  var TIMEOUT_MS = 5 * 60 * 1000;

  // The number of records to export to a single file.
  var PAGE_SIZE = 1000;

  var properties = PropertiesService.getScriptProperties();

  var folderId = properties.getProperty(FOLDER_ID_PROPERTY);
  var folder;
  if (folderId) {
    folder = DriveApp.getFolderById(folderId);
  } else {
    folder = DriveApp.createFolder(EXPORT_FOLDER_NAME);
    properties.setProperty(FOLDER_ID_PROPERTY, folder.getId());
  }

  var pageNumber = properties.getProperty(PAGE_NUMBER_PROPERTY) || 0;
  var db = ScriptDb.getMyDb();
  var now = new Date();
  var finished = false;

  for (var start = new Date().getTime(); now - start < TIMEOUT_MS;
      now = new Date()) {
    var page = db.query({}).paginate(pageNumber, PAGE_SIZE);
    if (page.getSize() == 0) {
      finished = true;
      break;
    }
    var results = [];
    while (page.hasNext()) {
      var item = page.next();
      results.push(item.toJson());
    }
    var content = results.join('\n');
    var fileName = Utilities.formatString('part-%03d.json', pageNumber);
    folder.createFile(fileName, content, 'application/json');
    pageNumber++;
    properties.setProperty(PAGE_NUMBER_PROPERTY, pageNumber);
  }
  if (finished) {
    Logger.log('Export complete');
    properties.deleteProperty(FOLDER_ID_PROPERTY);
    properties.deleteProperty(PAGE_NUMBER_PROPERTY);
  } else {
    throw 'Export timed out. Run the function again to continue.';
  }
}

Alternative data stores

Apps Script can be used with a variety of other data stores. The table below is far from exhaustive (after all, the URL Fetch service can connect to nearly any cloud database), but it shows some of the more popular options.

Data store Technology Free tier? Accessed via
Apps Script properties Stores string pairs; not a database Yes, with quota limits Properties service
Google Cloud SQL Cloud-hosted SQL database No JDBC service
MySQL, MS SQL, or Oracle Privately hosted SQL database Ask your web host
Parse Core Cloud-hosted NoSQL database Yes URL Fetch service or ParseDb library (not maintained by Google)
MongoDB (via MongoLab) Yes URL Fetch service