Azure SDK for JavaScript - Table Storage

Welcome to the documentation for the Azure Table Storage client library for JavaScript. This library allows you to interact with Azure Table Storage, a NoSQL key-attribute data store that allows you to store large amounts of unstructured data.

Getting Started

To use the Table Storage client library, you first need to install it:

npm install @azure/data-tables

Then, you can create a client and perform operations. You'll need your Azure Storage account name and a connection string or account key.

Creating a TableClient


import { TableClient, AzureNamedKeyCredential } from "@azure/data-tables";

async function main() {
    const account = "YOUR_STORAGE_ACCOUNT_NAME";
    const connectionString = "YOUR_CONNECTION_STRING"; // Or use account name and key

    // Using connection string
    const tableClientFromString = TableClient.fromConnectionString(connectionString, "MyTable");

    // Using account name and key
    const accountKey = "YOUR_STORAGE_ACCOUNT_KEY";
    const credential = new AzureNamedKeyCredential(account, accountKey);
    const tableClientFromKey = new TableClient(`https://${account}.table.core.windows.net`, "MyTable", credential);

    // Ensure the table exists (creates it if it doesn't)
    await tableClientFromString.createTable();
    console.log("Table created or already exists.");

    // Proceed with operations...
}

main().catch(err => {
    console.error("Error: ", err);
});
            

Core Concepts

Common Operations

Inserting Entities

You can insert entities using upsertEntity (inserts or updates if exists) or createEntity (inserts only, throws error if exists).

Inserting a single entity


// Assuming tableClient is already created as above

const entity = {
    partitionKey: "users",
    rowKey: "user123",
    name: "Alice Smith",
    email: "alice@example.com",
    age: 30
};

await tableClientFromString.upsertEntity(entity);
console.log(`Entity with rowKey 'user123' inserted/updated.`);
            

Querying Entities

Query entities using listEntities.

Querying entities by partition key


// Assuming tableClient is already created as above

const queryOptions = {
    partitionKey: "users"
};

console.log("Querying entities in 'users' partition:");
for await (const entity of tableClientFromString.listEntities(queryOptions)) {
    console.log(entity);
}
            

Updating Entities

Use upsertEntity to update existing entities. You can also use updateEntity for conditional updates.

Updating an entity


// Assuming tableClient is already created as above

const entityToUpdate = {
    partitionKey: "users",
    rowKey: "user123",
    age: 31, // Update age
    city: "New York" // Add a new property
};

await tableClientFromString.upsertEntity(entityToUpdate);
console.log(`Entity with rowKey 'user123' updated.`);
            

Deleting Entities

Delete an entity using deleteEntity.

Deleting an entity


// Assuming tableClient is already created as above

await tableClientFromString.deleteEntity("users", "user123");
console.log(`Entity with rowKey 'user123' deleted.`);
            

API Reference

TableClient Methods

For full API details, please refer to the official API documentation.

Further Reading