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
- Tables: Collections of entities.
- Entities: Rows in a table, each identified by a
partitionKeyand arowKey. - Properties: Name-value pairs within an entity. Property names must be strings, and values can be of various primitive types.
- Partition Key: Used to group entities within a table. Entities with the same
partitionKeyare stored on the same storage node. - Row Key: A unique identifier for an entity within a partition. Together,
partitionKeyandrowKeyuniquely identify an entity.
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
createTable(tableName?: string): Creates a table if it doesn't exist.deleteTable(tableName?: string): Deletes a table.listTables(options?: ListTablesOptions): Lists all tables in the storage account.getProperties(options?: TableGetPropertiesOptions): Gets table properties.upsertEntity(entity: object, options?: TableSetEntityOptions): Inserts or updates an entity.createEntity(entity: object, options?: TableSetEntityOptions): Inserts a new entity.updateEntity(entity: object, options?: TableUpdateEntityOptions): Updates an existing entity.deleteEntity(partitionKey: string, rowKey: string, options?: TableDeleteEntityOptions): Deletes an entity.getEntity(partitionKey: string, rowKey: string, options?: TableGetEntityOptions): Retrieves a single entity.listEntities(options?: ListEntitiesOptions): Lists entities in a table.executeTransaction(operations: TableTransactionOperation[], options?: TableTransactionOptions): Executes a batch of operations atomically.
For full API details, please refer to the official API documentation.