Launch and Iterate: Parse Data Migrations

Every developer knows: Shipping is just the start. After your app has launched, you’ll still have new features you want to add. Sometimes making these updates requires updating or transforming existing data stored in the cloud. For example, you may have read Brad’s excellent post about how to make your text searchable and asked yourself, “How do I update all of my existing data?”.

You’ve always been able to do so with our powerful REST API. But we’ve recently launched a couple of features that will make it even easier to process your data, using JavaScript running in Node.js. The Parse JavaScript SDK is available through npm, so getting started is as easy as running:

npm install parse

Remember, there are active clients out there constantly creating new data. So before you start updating existing data, you’ll probably want to add a beforeSave handler to process the new data as it comes in. Since this is all JavaScript, you can use the same code for your beforeSave handler that you do in your data migration script.

Then, you will need to iterate over all of the existing objects in your collection to update them. The new each method on Parse.Query objects allows you to do just that. Even if you have tens of thousands of objects or more in a collection, it will return each one of them, giving you an opportunity to modify them as you see fit.

We’ve also added the Parse.Cloud.useMasterKey() method to our node module. By supplying your master key and calling this method, you can transform objects belonging to all of your users without ever compromising their ACL-based security.

For example, if you have Event objects and want to transform their city field into a searchable text field called cityTokens, you could run the following node script:

var _ = require("underscore");
var Parse = require("parse").Parse;

Parse.initialize("YOUR APP ID", "YOUR JAVASCRIPT KEY", "YOUR MASTER KEY");

Parse.Cloud.useMasterKey();

var tokenize = function(str) {
  var words = str.toLowerCase().split(/\b/);
  words = _.filter(words, function(w) { return w.match(/^\w+$/); });
  return words;
};

var query = new Parse.Query("Event");
query.doesNotExist("cityTokens");

return query.each(function(event) {
  console.log("Updating object: " + event.id);
  event.set("cityTokens", tokenize(event.get("city")));
  return event.save();
});

Then just run this script on your machine with node:

$ node migrate.js 
Updating object: 4JsHZTwVBS
Updating object: MnZ4TzBDFT
Updating object: OPndkI2iRk
Updating object: WBuxcx3fHM
Updating object: duyWWKKxu3
Updating object: jPGxPhjjvg
...

At Parse, we love seeing all of the great new features our developers are adding to their apps. These new additions to our API should help you launch them even faster!

Bryan Klimt
May 13, 2013
blog comments powered by Disqus

Comments are closed.

Archives

Categories

RSS Feed Follow us Like us