Last active
May 12, 2020 19:16
-
-
Save prashanthmadi/287acd7361567e812d3882ae3c07a115 to your computer and use it in GitHub Desktop.
updates offer in Cosmos DB SQL API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Author
prashanthmadi
commented
Apr 28, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment