Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//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