Quickstart: Azure Cosmos DB for Table library for Node.js
APPLIES TO: Table
This quickstart shows how to get started with the Azure Cosmos DB for Table from a Node.js application. The Azure Cosmos DB for Table is a schemaless data store allowing applications to store structured table data in the cloud. You learn how to create tables, rows, and perform basic tasks within your Azure Cosmos DB resource using the Azure SDK for Node.js.
API reference documentation | Library source code | Package (npm) | Azure Developer CLI
Prerequisites
- An Azure account with an active subscription. Create an account for free.
- Azure Developer CLI
- Docker Desktop
- Node.js 22 or newer
Initialize the project
Use the Azure Developer CLI (azd
) to create an Azure Cosmos DB for Table account and deploy a containerized sample application. The sample application uses the client library to manage, create, read, and query sample data.
Open a terminal in an empty directory.
If you're not already authenticated, authenticate to the Azure Developer CLI using
azd auth login
. Follow the steps specified by the tool to authenticate to the CLI using your preferred Azure credentials.azd auth login
Use
azd init
to initialize the project.azd init --template cosmos-db-table-nodejs-quickstart
During initialization, configure a unique environment name.
Deploy the Azure Cosmos DB account using
azd up
. The Bicep templates also deploy a sample web application.azd up
During the provisioning process, select your subscription, desired location, and target resource group. Wait for the provisioning process to complete. The process can take approximately five minutes.
Once the provisioning of your Azure resources is done, a URL to the running web application is included in the output.
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.
Use the URL in the console to navigate to your web application in the browser. Observe the output of the running app.
Install the client library
The client library is available through npm, as the @azure/data-tables
package.
Open a terminal and navigate to the
/src/ts
folder.cd ./src/ts
If not already installed, install the
@azure/data-tables
package usingnpm install
.npm install --save @azure/data-tables
Open and review the src/ts/package.json file to validate that the
@azure/data-tables
entry exists.
Open a terminal and navigate to the
/src/js
folder.cd ./src/js
If not already installed, install the
@azure/data-tables
package usingnpm install
.npm install --save @azure/data-tables
Open and review the src/js/package.json file to validate that the
@azure/data-tables
entry exists.
Object model
Name | Description |
---|---|
TableServiceClient |
This type is the primary client type and is used to manage account-wide metadata or databases. |
TableClient |
This type represents the client for a table within the account. |
Code examples
The sample code in the template uses a table named cosmicworks-products
. The cosmicworks-products
table contains details such as name, category, quantity, price, a unique identifier, and a sale flag for each product. The container uses a unique identifier* as the row key and category as a partition key.
Authenticate the client
This sample creates a new instance of the TableServiceClient
type.
let client: TableServiceClient = new TableServiceClient("<azure-cosmos-db-table-account-endpoint>", credential);
let client = new TableServiceClient("<azure-cosmos-db-table-account-endpoint>", credential);
Get a table
This sample creates an instance of the TableClient
type using the GetTableClient
function of the TableServiceClient
type.
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);
Create an item
The easiest way to create a new item in a table is to derive a new interface from TableEntity
and then create a new object of that type.
export interface Product extends TableEntity {
name: string;
quantity: number;
price: number;
clearance: boolean;
}
const entity: Product = {
rowKey: '70b63682-b93a-4c77-aad2-65501347265f',
partitionKey: 'gear-surf-surfboards',
name: 'Yamba Surfboard',
quantity: 12,
price: 850.00,
clearance: false
};
The easiest way to create a new item in a table is to build a JSON object.
const entity = {
rowKey: '70b63682-b93a-4c77-aad2-65501347265f',
partitionKey: 'gear-surf-surfboards',
name: 'Yamba Surfboard',
quantity: 12,
price: 850.00,
clearance: false
};
Create an item in the collection using the upsertEntity
method from the TableService
instance.
await table.upsertEntity<Product>(entity, "Replace");
await table.upsertEntity(entity, "Replace");
Get an item
You can retrieve a specific item from a table using the getEntity
method, the row key for the item, and partition key of the item.
const response: GetTableEntityResponse<TableEntityResult<Product>> = await table.getEntity<Product>(partitionKey, rowKey);
const entity: Product = response as Product;
const entity = await table.getEntity(partitionKey, rowKey);
Query items
After you insert an item, you can also run a query to get all items that match a specific filter by using listEntities
with an OData filter.
const partitionKey: string = 'gear-surf-surfboards';
const filter: string = `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: `PartitionKey eq '${partitionKey}'`
}
});
Parse the paginated results of the query by using an asynchronous for await
loop on the paginated set of entities
.
for await(const entity of entities) {
// Do something
}
for await(const entity of entities) {
// Do something
}
Clean up resources
When you no longer need the sample application or resources, remove the corresponding deployment and all resources.
azd down