Skip to content

Instantly share code, notes, and snippets.

@seesharprun
Created September 8, 2022 15:55
Show Gist options
  • Save seesharprun/9c7dac174196486315d120fa27136e25 to your computer and use it in GitHub Desktop.
Save seesharprun/9c7dac174196486315d120fa27136e25 to your computer and use it in GitHub Desktop.
Azure Cosmos DB SQL API + Node.js
node_modules/
package-lock.json
import { CosmosClient } from '@azure/cosmos';
const endpoint = '<cosmos-db-sql-endpoint>';
const key = '<cosmos-db-sql-read-key>';
const client = new CosmosClient({ endpoint, key });
const { database } = await client.databases.createIfNotExists({ id: 'cosmicworks' });
console.log(`Database:\t${database.id}`);
const { container } = await database.containers.createIfNotExists({ id: 'products' });
console.log(`Container:\t${container.id}`);
const id = '0093284092384';
var item = await container.item(id).read();
console.log(`Read Item:\t${id}`);
console.log(`Status Code:\t${item.statusCode}`);
if (item.resource) {
console.dir(item.resource);
}
else {
console.log('Not found');
}
{
"main": "app.js",
"type": "module",
"scripts": {
"start": "node app.js"
},
"dependencies": {
"@azure/cosmos": "3.16"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment