Skip to content

Instantly share code, notes, and snippets.

@sajeetharan
Created February 6, 2019 07:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sajeetharan/8efe2c9424dfc89d1f58b34627858944 to your computer and use it in GitHub Desktop.
Save sajeetharan/8efe2c9424dfc89d1f58b34627858944 to your computer and use it in GitHub Desktop.
//lets import the necessary packages
var docdb = require("documentdb");
var async = require("async");
// Get the host link which is the documentdb/cosmosdb url from the portal and masterkey is the authentication key
var config = {
host: "https://identityshareddocdb.documents.azure.com:443/",
auth: {
masterKey: "2XEDLvGfedNBDZMAv+EXnZ0SFR4a4MuVt1Fi6WmrRwH7SLcHrr0MvCFm9v+W83qfcJBJXedWjxT47PnNyB1DwA=="
}
};
// Create the CosmosDB client
let client = new docdb.DocumentClient(config.host, config.auth);
// Creating the link
let documentsLink = docdb.UriFactory.createDocumentCollectionUri("IdentityEventLog", "Events");
// Retrive all documents
let selectAll = function(callback) {
var spec = {
query: "SELECT * FROM c",
parameters: []
};
client.queryDocuments(documentsLink, spec).toArray((err, results) => {
callback(err, results);
});
};
// Retrieve and delete all documents
let deleteAll = function() {
selectAll((err, results) => {
if (err) {
console.log(err);
} else {
async.forEach(results, (message, next) => {
client.deleteDocument(message._self, err => {
if (err) {
console.log(err);
next(err);
} else {
next();
}
});
});
}
});
};
// Getting the input whether selectAll or deletAll
let task = process.argv[2];
switch (task) {
case "selectAll":
selectAll((err, results) => {
if (err) {
console.error(err);
} else {
console.log(results);
}
});
break;
case "deleteAll":
deleteAll();
break;
default:
console.log("Commands:");
console.log("selectAll deleteAll");
break;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment