|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +/** |
| 5 | + * @summary Demonstrates how to authenticate and use your database account using AAD credentials with Fabric. |
| 6 | + * |
| 7 | + * Prerequisites: |
| 8 | + * 1. An Azure Cosmos account in fabric environment and database and container created. |
| 9 | + * https://learn.microsoft.com/en-us/fabric/database/cosmos-db/overview |
| 10 | + * 2. Node.js packages (@azure/cosmos + @azure/identity) and login: |
| 11 | + * npm install @azure/cosmos @azure/identity |
| 12 | + * az login |
| 13 | + * |
| 14 | + * Sample - demonstrates how to authenticate and use your database account using AAD credentials with Fabric. |
| 15 | + * Read more about operations allowed for this authorization method: https://aka.ms/cosmos-native-rbac |
| 16 | + * |
| 17 | + * Note: |
| 18 | + * This sample assumes the database and container already exist. |
| 19 | + * It writes one item (PK path assumed to be "/pk") and reads it back. |
| 20 | + */ |
| 21 | + |
| 22 | +import "dotenv/config"; |
| 23 | +import { DefaultAzureCredential } from "@azure/identity"; |
| 24 | +// eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 25 | +// @ts-ignore |
| 26 | +import { CosmosClient } from "@azure/cosmos"; |
| 27 | +import { handleError, finish, logStep } from "./Shared/handleError.js"; |
| 28 | + |
| 29 | +// Configuration - replace with your values |
| 30 | +const endpoint = process.env.COSMOS_ENDPOINT || "<cosmos endpoint>"; |
| 31 | +const databaseId = process.env.COSMOS_DATABASE || "<cosmos database>"; |
| 32 | +const containerId = process.env.COSMOS_CONTAINER || "<cosmos container>"; |
| 33 | + |
| 34 | +// Test item structure |
| 35 | +interface TestItem { |
| 36 | + id: string; |
| 37 | + pk: string; |
| 38 | + name: string; |
| 39 | + description: string; |
| 40 | + runId: string; |
| 41 | +} |
| 42 | + |
| 43 | +function getTestItem(num: number): TestItem { |
| 44 | + return { |
| 45 | + id: `Item_${num}`, |
| 46 | + pk: "partition1", |
| 47 | + name: `Item ${num}`, |
| 48 | + description: `This is item ${num}`, |
| 49 | + runId: crypto.randomUUID(), |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +async function run(): Promise<void> { |
| 54 | + |
| 55 | + logStep("Setting up AAD credentials"); |
| 56 | + |
| 57 | + // AAD auth works with az login |
| 58 | + const credentials = new DefaultAzureCredential(); |
| 59 | + |
| 60 | + logStep("Creating Cosmos client with AAD credentials"); |
| 61 | + |
| 62 | + const client = new CosmosClient({ |
| 63 | + endpoint, |
| 64 | + aadCredentials: credentials, |
| 65 | + aadScope: "https://cosmos.azure.com/.default" |
| 66 | + }); |
| 67 | + |
| 68 | + logStep("Getting database and container references"); |
| 69 | + const database = client.database(databaseId); |
| 70 | + const container = database.container(containerId); |
| 71 | + |
| 72 | + logStep("Creating a test item"); |
| 73 | + // Create item |
| 74 | + const testItem = getTestItem(0); |
| 75 | + const { resource: createdItem } = await container.items.create(testItem); |
| 76 | + console.log(`Created item: ${createdItem?.id}`); |
| 77 | + |
| 78 | + logStep("Reading the item back"); |
| 79 | + // Read item |
| 80 | + const { resource: readItem } = await container.item(testItem.id, testItem.pk).read<TestItem>(); |
| 81 | + console.log("Point read:"); |
| 82 | + console.log(JSON.stringify(readItem, null, 2)); |
| 83 | + |
| 84 | + logStep("Querying for items in the partition"); |
| 85 | + // Query items |
| 86 | + const querySpec = { |
| 87 | + query: "SELECT * FROM c WHERE c.pk = @partitionKey", |
| 88 | + parameters: [ |
| 89 | + { |
| 90 | + name: "@partitionKey", |
| 91 | + value: testItem.pk, |
| 92 | + }, |
| 93 | + ], |
| 94 | + }; |
| 95 | + |
| 96 | + const { resources: items } = await container.items.query<TestItem>(querySpec).fetchAll(); |
| 97 | + console.log(`Found ${items.length} items in partition '${testItem.pk}':`); |
| 98 | + items.forEach((item) => { |
| 99 | + console.log(`- ${item.id}: ${item.name}`); |
| 100 | + }); |
| 101 | + |
| 102 | + logStep("Sample completed successfully"); |
| 103 | + await finish(); |
| 104 | +} |
| 105 | + |
| 106 | +run().catch(handleError); |
0 commit comments