Skip to content

Instantly share code, notes, and snippets.

@jparreira
Created September 14, 2015 18:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jparreira/efc40d7e063489c46b44 to your computer and use it in GitHub Desktop.
Save jparreira/efc40d7e063489c46b44 to your computer and use it in GitHub Desktop.
Creates and copies table items from one Realtime Cloud Storage app key to another
var Realtime = require("realtime-storage");
var fromSR;
var toSRCreate;
var toSRCopy;
// Creates destination table and copies items from origin table
function migrate(fromAppKey, fromPrivateKey, toAppKey, toPrivateKey, table)
{
// storageRef to read from table
fromSR = Realtime.Storage.create({
applicationKey: fromAppKey,
privateKey: fromPrivateKey
});
// storageRef to create the destination table
toSRCreate = Realtime.Storage.create({
applicationKey: toAppKey,
privateKey: toPrivateKey
});
// storageRef to write to destination table
toSRCopy = Realtime.Storage.create({
applicationKey: toAppKey,
privateKey: toPrivateKey
});
startProcess(table);
}
function startProcess(table)
{
var fromTable;
var toTable;
if (fromSR && toSRCreate && toSRCopy) {
var fromTable = fromSR.table(table);
// Get table metadata
fromTable.meta(function success(metadata) {
// Create table
// You might need to increase the throughput to allow the copy
// After copy is completed you may use the console to reduce the throughput
var toTableCreate = toSRCreate.table(table);
toTableCreate.create({
key: metadata.key,
provisionType: Realtime.Storage.ProvisionType.Custom,
throughput: { read: 1, write: 50 }
});
console.log("Waiting 15s for table to be created and start copy ...");
setTimeout(function(){
console.log("Starting copy");
var toTable = toSRCopy.table(table);
fromTable.getItems(function success(itemSnapshot) {
if (itemSnapshot != null) {
console.log("Copying item:", itemSnapshot.val());
toTable.push(itemSnapshot.val(),
function success(itemSnapshot) {
console.log("Copied item:", itemSnapshot.val());
},
function error(message) {
console.log('error copying:' + message);
});
} else {
console.log("No more items to copy. Waiting for 'copied item' callbacks.");
console.log("------------------------------------------------------");
}
});
}, 15000);
});
} else {
console.log("Error: There's a storageRef missing. Aborting copy.");
}
}
migrate('<ORIGIN-APP-KEY>','<ORIGIN-PRIVATE-KEY>', '<DESTINATION-APP-KEY>', '<DESTINATION-PRIVATE-KEY>', '<TABLE NAME TO COPY>');
@jparreira
Copy link
Author

usage:

node migration.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment