Hi @Phil Drew ,
NodeJS tutorial for the SQL API for Azure Cosmos DB is the doc you're looking for query your Cosmos DB from a NodeJS app. You'll add the SDK @azure/cosmos
to your package.json. Then create a js file to handle CRUD operations. You can make this as abstract as you want to be. But to get going
const CosmosClient = require("@azure/cosmos").CosmosClient;
const config = {
endpoint: "<Your Azure Cosmos account URI>",
key: "<Your Azure Cosmos account key>",
databaseId: "Tasks",
containerId: "Items",
partitionKey: { kind: "Hash", paths: ["/category"] }
};
/*
// This script ensures that the database is setup and populated correctly
*/
async function create(client, databaseId, containerId) {
const partitionKey = config.partitionKey;
/**
* Create the database if it does not exist
*/
const { database } = await client.databases.createIfNotExists({
id: databaseId
});
console.log(`Created database:\n${database.id}\n`);
/**
* Create the container if it does not exist
*/
const { container } = await client
.database(databaseId)
.containers.createIfNotExists(
{ id: containerId, partitionKey },
{ offerThroughput: 400 }
);
console.log(`Created container:\n${container.id}\n`);
}
const client = new CosmosClient({config.endpoint, config.key});
const database = new client.database(config.databaseId);
const container = database.container(config.containerId);
// query to return all items
const querySpec = {
query: "SELECT * from c"
};
// read all items in the Items container
const { resources: items } = await container.items
.query(querySpec)
.fetchAll();
items.forEach(item => {
console.log(`${item.id} - ${item.description}`);
});
You can leverage application settings for the config
object instead of hard coding the values in.