Skip to content

Instantly share code, notes, and snippets.

@jparreira
Created April 14, 2015 15:17
Show Gist options
  • Save jparreira/5357ca35d3671a130e99 to your computer and use it in GitHub Desktop.
Save jparreira/5357ca35d3671a130e99 to your computer and use it in GitHub Desktop.
Copying items from a Realtime Cloud Storage table to another table (with the same key schema)
<!DOCTYPE html>
<html>
<head>
<script src="http://storage-cdn.realtime.co/storage/1.0.0/realtime-storage-min.js"></script>
<script src="migration.js"></script>
</head>
<body>
<h1>Realtime Cloud Storage: Copy items
<p>
<input type="button"
onclick="copyItems('YOUR_REALTIME_APPKEY', 'authtoken', 'YOUR_ORIGIN_TABLENAME', 'YOUR_DESTINATION_TABLENAME')"
value="Start copying items" />
</body>
</html>
function copyItems(fromAppKey, token, fromTableName, toTableName)
{
storageRef = Realtime.Storage.create({
applicationKey: fromAppKey,
authenticationToken: token
}, function() {
var fromTableRef = storageRef.table(fromTableName);
var toTableRef = storageRef.table(toTableName);
console.log("Preparing copy");
fromTableRef.getItems(
function success(itemSnapshot) {
if (itemSnapshot && itemSnapshot.val()) {
// copying item
// WARNING: You might need to increase the write throughput on the destination table
console.log("Buffering copy of item: " + JSON.stringify(itemSnapshot.val()));
toTableRef.push( itemSnapshot.val(),
function success(itemSnapshot) {
console.log('Successfully copied item: '+ JSON.stringify(itemSnapshot.val()));
},
function error(message) {
console.log('Error:', message);
}
);
}
else
{
console.log("The copy operations are now buffered. Please wait for completion ...");
}
}, function error(message) {
console.log('error copying:' + message);
});
}, function(error) {
console.log(error);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment