在本快速入門中,您會使用適用於 Node.js 的 Azure SDK 來部署適用於資料表的基本 Azure Cosmos DB 應用程式。 Azure Cosmos DB for Table 是無架構的數據存放區,可讓應用程式將結構化數據表數據儲存在雲端中。 您將瞭解如何使用 Azure SDK for Node.js,在 Azure Cosmos DB 資源內建立數據表、數據列和執行基本工作。
API 參考文件 | 程式庫原始程式碼 | 套件 (npm) | Azure Developer CLI
必要條件
- Azure Developer CLI
- Docker 桌面
- Node.js 22 或更新
如果您沒有 Azure 帳戶,請在您開始之前先建立 免費帳戶。
初始化專案
使用 Azure 開發人員 CLI (azd) 建立適用於資料表帳戶的 Azure Cosmos DB,並部署容器化範例應用程式。 應用程式範例使用用戶端程式庫管理、建立、讀取和查詢樣本資料。
在空的目錄中開啟終端機。
如果您尚未通過驗證,請使用
azd auth login向 Azure 開發人員 CLI 進行驗證。 依照工具指定的步驟,使用您慣用的 Azure 認證向 CLI 進行驗證。azd auth login使用
azd init來初始化專案。azd init --template cosmos-db-table-nodejs-quickstart在初始化期間,請設定唯一的環境名稱。
使用
azd up部署 Azure Cosmos DB 帳戶。 Bicep 範本也會部署範例 Web 應用程式。azd up在布建程式期間,選取您的訂用帳戶、所需的位置和目標資源群組。 等候佈建程序完成。 此流程「大約需要五分鐘」的時間。
Azure 資源佈建完成後,輸出將包含正在執行的 Web 應用程式的 URL。
Deploying services (azd deploy) (✓) Done: Deploying service web - Endpoint: <https://[container-app-sub-domain].azurecontainerapps.io> SUCCESS: Your application was provisioned and deployed to Azure in 5 minutes 0 seconds.請使用主控台中的 URL,以在瀏覽器中導覽至您的 Web 應用程式。 觀察執行中應用程式的輸出。
安裝用戶端程式庫
用戶端程式庫可透過 npm 取得,作為 @azure/data-tables 套件。
開啟終端機,然後導覽至
/src/ts資料夾。cd ./src/ts如果尚未安裝,則請使用
@azure/data-tables來安裝npm install套件。npm install --save @azure/data-tables開啟並檢閱 src/ts/package.json 檔案,以驗證
@azure/data-tables專案是否存在。
開啟終端機,然後導覽至
/src/js資料夾。cd ./src/js如果尚未安裝,則請使用
@azure/data-tables來安裝npm install套件。npm install --save @azure/data-tables開啟並檢閱 src/js/package.json 檔案,以驗證
@azure/data-tables專案是否存在。
匯入程式庫
將 DefaultAzureCredential、 TableServiceClient和 TableClient 類型匯入您的應用程式程式代碼。
import { DefaultAzureCredential } from '@azure/identity';
import { TableServiceClient, TableClient } from '@azure/data-tables';
將所有必要的類型匯入您的應用程式程序代碼。
import { DefaultAzureCredential, TokenCredential } from '@azure/identity';
import { TableServiceClient, TableClient, TableEntityResult, GetTableEntityResponse, TableEntityResultPage, TableEntityQueryOptions } from '@azure/data-tables';
物件模型
| 名稱 | 描述 |
|---|---|
TableServiceClient |
此類型是主要客戶端類型,可用來管理全帳戶元數據或資料庫。 |
TableClient |
此類型代表帳戶內數據表的用戶端。 |
程式碼範例
範本中的範例程式代碼會使用名為 的 cosmicworks-products數據表。 數據表 cosmicworks-products 包含詳細數據,例如名稱、類別、數量、價格、唯一標識碼,以及每個產品的銷售旗標。 容器會使用唯一 標識碼 作為數據列索引鍵,而 類別 目錄作為分割區索引鍵。
驗證用戶端
此範例會建立 型別 TableServiceClient 的新實例。
let client: TableServiceClient = new TableServiceClient("<azure-cosmos-db-table-account-endpoint>", "<credential>");
const credential = new DefaultAzureCredential();
let client = new TableServiceClient("<azure-cosmos-db-table-account-endpoint>", credential);
取得資料表
此範例會使用 TableClient 型別的 GetTableClient 函式,建立 TableServiceClient 型別的實例。
let table: TableClient = new TableClient("<azure-cosmos-db-table-account-endpoint>", "<azure-cosmos-db-table-name>", credential);
let table = new TableClient("<azure-cosmos-db-table-account-endpoint>", "<azure-cosmos-db-table-name>", credential);
建立實體
在數據表中建立新實體最簡單的方法是從 TableEntity 衍生新的介面,然後建立該類型的新物件。
export interface Product extends TableEntity {
name: string;
quantity: number;
price: number;
clearance: boolean;
}
const entity: Product = {
rowKey: 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb',
partitionKey: 'gear-surf-surfboards',
name: 'Yamba Surfboard',
quantity: 12,
price: 850.00,
clearance: false
};
在數據表中建立新專案最簡單的方式是建立 JSON 物件。
const entity = {
rowKey: 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb',
partitionKey: 'gear-surf-surfboards',
name: 'Yamba Surfboard',
quantity: 12,
price: 850.00,
clearance: false
};
使用 upsertEntity 實例中的 方法 TableService ,在數據表中建立實體。
await table.upsertEntity<Product>(entity, "Replace");
await table.upsertEntity(entity, "Replace");
取得實體
您可以使用 getEntity 方法、實體的列鍵以及實體的分區鍵,從資料表中擷取特定實體。
const response: GetTableEntityResponse<TableEntityResult<Product>> = await table.getEntity<Product>(partitionKey, rowKey);
const entity: Product = response as Product;
const entity = await table.getEntity(partitionKey, rowKey);
查詢實體
在插入實體之後,您也可以執行查詢,使用 OData 篩選搭配 listEntities 來取得符合特定篩選條件的所有實體。
const partitionKey: string = 'gear-surf-surfboards';
const filter: string = odata`PartitionKey eq '${partitionKey}'`
const queryOptions: TableEntityQueryOptions = { filter: filter }
const entities: PagedAsyncIterableIterator<TableEntityResult<Product>, TableEntityResultPage<Product>> = table.listEntities<Product>({ queryOptions: queryOptions });
const partitionKey = 'gear-surf-surfboards';
const entities = table.listEntities({
queryOptions: {
filter: odata`PartitionKey eq '${partitionKey}'`
}
});
使用異步for await循環在分頁集合entities上解析查詢的分頁結果。
for await(const entity of entities) {
// Do something
}
for await(const entity of entities) {
// Do something
}
清除資源
當您不再需要範例應用程式或資源時,請移除對應的部署和所有資源。
azd down