Skip to content

Instantly share code, notes, and snippets.

@prashanthmadi
Last active May 12, 2020 19:16
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 prashanthmadi/287acd7361567e812d3882ae3c07a115 to your computer and use it in GitHub Desktop.
Save prashanthmadi/287acd7361567e812d3882ae3c07a115 to your computer and use it in GitHub Desktop.
updates offer in Cosmos DB SQL API
var _ = require('lodash');
const { CosmosClient } = require("@azure/cosmos");
const endpoint = "https://cosmosdbtestpr.documents.azure.com:443/";
const key = "xxxx==";
const client = new CosmosClient({ endpoint, key });
const databaseName = "testdb2";
const db = null;
const initialOffer = 1000;
const finalOffer = 600;
async function createDatabase(database) {
const { database: db } = await client.databases.createIfNotExists({ id: database,throughput:initialOffer });
this.db = db;
console.log("created db");
// createIfNotExists returns database object not databaseResponse . need to correct https://docs.microsoft.com/en-us/javascript/api/@azure/cosmos/databases?view=azure-node-latest#createifnotexists-databaserequest--requestoptions-
this.db_resource = (await db.read()).resource._self // dbs/4O9xAA==/
}
async function offerDetails() {
const {resources: offers} = await client.offers.readAll().fetchAll();
_.map(offers, function (item) {
console.log(item.resource + " -> " + item.content.offerThroughput)
});
}
async function changeOffer(resourceUrl,newOffer){
const { resources: offers } = await client.offers
.query({
query: `SELECT * FROM root WHERE root.resource = "${resourceUrl}"`,
})
.fetchAll();
async function replaceOffer(item){
const offer = client.offer(item.id);
await offer.replace(item);
}
_.map(offers, function (item) {
item.content.offerThroughput = newOffer;
replaceOffer(item);
});
}
async function init() {
await createDatabase(databaseName).catch(err => { console.error(err); });
await offerDetails();
await changeOffer(this.db_resource, finalOffer);
await offerDetails();
}
init()
@prashanthmadi
Copy link
Author

image

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