This folder contains a JavaScript (Node.js) sample that uses the Azure Resource Manager (ARM) / Management Plane SDKs to create and update Azure Cosmos DB resources.
This is useful when your application uses Microsoft Entra ID and you want to manage Cosmos DB resources (accounts, databases, containers, throughput, and RBAC) via an SDK instead of Bicep/PowerShell/Azure CLI.
Important: This sample uses the control plane (resource provider) APIs via ARM. It does not use the Cosmos DB data plane SDK to create ARM resources.
- Create or update a Cosmos DB SQL (NoSQL) account.
- Disables local/key auth (
disableLocalAuth: true) so Entra ID + RBAC is required. - Enables the
EnableNoSQLVectorSearchcapability. - Adds an
ownertag (best-effort) from the signed-in identity.
- Create or update a SQL database.
- Create or update a SQL container with:
- Hierarchical partition key (multi-hash) on
/companyId,/departmentId,/userId. - Indexing policy (consistent), plus a vector index on
/vectors. - Vector embedding definition (1536 dims, cosine distance).
- Unique key on
/userId. - Computed property example (
cp_lowerName). - TTL enabled with no default (container
defaultTtl: -1). - Last-writer-wins conflict resolution (
/_ts). - Autoscale max throughput from configuration.
- Hierarchical partition key (multi-hash) on
- Updates container dedicated throughput by reading current settings first and then:
- Updating autoscale max throughput when the container is autoscale, or
- Updating RU/s when the container is manual throughput.
- Re-reads and prints the applied settings after the update.
- Throws a clear error when the throughput resource doesn’t exist (common for serverless accounts or shared database throughput).
This sample creates two role assignments for the currently signed-in principal:
- Azure RBAC (control plane): assigns the built-in
Cosmos DB Operatorrole at the Cosmos account scope. - Cosmos DB SQL RBAC: assigns the built-in
Cosmos DB Built-in Data Contributorrole.
It also includes a custom Cosmos DB SQL RBAC role definition example (not used by default).
- Runs an interactive menu by default.
- Includes a "Run full sample" menu option.
- This sample is interactive-only by default (it refuses to run when stdin is not a TTY).
- Supports deleting the Cosmos DB account from the menu (requires typing
DELETEto confirm). - Supports deleting the Cosmos DB account from the full run only when
COSMOS_SAMPLE_DELETE_ACCOUNT=true(opt-in safety guard).
- An Azure subscription and a resource group.
- Node.js 18.18+.
- Azure identity available to
DefaultAzureCredential(for example:az login, VS Code sign-in, Managed Identity, etc.). - Permissions:
- To create/update Cosmos resources: typically Contributor on the resource group.
- To create Azure RBAC role assignments: typically Owner or User Access Administrator at the target scope.
Notes:
- These operations require a subscription id, resource group, and an Azure region (
location) for ARM resources. Thislocationis typically the same region as your resource group, and it does not need to match the regions where Cosmos DB data is replicated.
If you want to run/debug this sample from VS Code:
- Open JavaScript.code-workspace
- Ensure you have Node.js installed (18.18+)
-
Open the JavaScript workspace in VS Code:
Open JavaScript.code-workspace. This configures the recommended VS Code settings for this sample.
Copy the sample config and fill in these values:
subscriptionId="..."
location="..."
resourceGroupName="..."
accountName="..."
databaseName="..."
containerName="..."
maxAutoscaleThroughput=1000
This sample expects you to run from the JavaScript/ folder.
cd JavaScript
npm install
cp config.env.sample config.envcd .\JavaScript
npm install
copy config.env.sample config.envFrom the JavaScript/ folder:
npm run startFollow the on-screen menu prompts.
If you want to run specific operations without the interactive menu, pass explicit --run-* flags:
npm run start -- --run-accountnpm run start -- --run-dbnpm run start -- --run-containernpm run start -- --run-throughput --throughput-delta=1000npm run start -- --run-azure-rbacnpm run start -- --run-cosmos-rbac
When stdin is not a TTY (for example, CI), the sample requires these flags and will not open the menu.
Open the workspace file JavaScript.code-workspace and press F5 to run “Node.js: Debug sample”.
In VS Code:
- Use File → Open Workspace from File…
- Select
JavaScript.code-workspace
This keeps each sample's debug configuration independent, so developers don't need to install debug extensions for languages they aren't using.
You can find the source code for the Azure Management SDK for JavaScript for Azure Cosmos DB and additional samples at:
- GitHub: Azure SDK for JS - arm-cosmosdb
- npm: @azure/arm-cosmosdb