Created
September 8, 2022 15:55
-
-
Save seesharprun/9c7dac174196486315d120fa27136e25 to your computer and use it in GitHub Desktop.
Azure Cosmos DB SQL API + Node.js
This file contains hidden or 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
node_modules/ | |
package-lock.json |
This file contains hidden or 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
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'); | |
} |
This file contains hidden or 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
{ | |
"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