Table Storage Bindings

Table storage is a NoSQL key-attribute store that you can use to store large amounts of structured, non-relational data. Azure Functions provides input and output bindings for interacting with Azure Table Storage, simplifying the process of reading from and writing to tables.

Supported Operations

The table storage bindings for Azure Functions enable you to:

Configuration

To use the table storage bindings, you need to define them in your function.json file (for JavaScript, Python, and other non-.NET languages) or via attributes (for C#/.NET).

function.json Example (JavaScript)


{
  "scriptFile": "index.js",
  "bindings": [
    {
      "name": "inputTable",
      "type": "table",
      "direction": "in",
      "tableName": "products",
      "partitionKey": "{partitionKey}",
      "rowKey": "{rowKey}",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "outputTable",
      "type": "table",
      "direction": "out",
      "tableName": "products",
      "connection": "AzureWebJobsStorage"
    },
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "name": "res",
      "type": "http",
      "direction": "out"
    }
  ]
}
            

Input Binding Details

The input binding allows you to fetch data from a table. You can specify the tableName, partitionKey, and rowKey. If you omit rowKey, the binding attempts to fetch all entities for the given partitionKey.

Properties:

Note: When using an input binding without a rowKey to retrieve multiple entities, the function parameter should be of a collection type (e.g., an array or list).

Output Binding Details

The output binding is used to insert or update entities in a table. You typically use a collector object (like IAsyncCollector<TEntity> in C# or func.Out[func.TableEntity] in Python) to add entities.

Properties:

Tip: For inserting multiple entities efficiently, consider using batch operations if your SDK supports them, or process them in batches within your function logic.

Entity Structure

Entities in Azure Table Storage consist of a PartitionKey, a RowKey, and a set of named properties. The PartitionKey and RowKey together form the unique identifier for an entity.

Property Type Description
PartitionKey String Required. Used for partitioning data.
RowKey String Required. Unique within a partition.
Timestamp DateTime Automatically managed by Azure Table Storage.
Custom Properties Various (String, Int, Double, Boolean, DateTime, Guid, Binary) User-defined data properties.

Example Usage

Here's a conceptual example of how you might use the bindings to retrieve a product by its ID and then update its price:


// index.js (simplified)
module.exports = async function (context, req, inputTable) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const partitionKey = req.query.partitionKey || req.body.partitionKey;
    const rowKey = req.query.rowKey || req.body.rowKey;
    const newPrice = req.body.price;

    if (!partitionKey || !rowKey) {
        context.res = { status: 400, body: "Please provide partitionKey and rowKey." };
        return;
    }

    // inputTable contains the entity if found, or null
    if (inputTable) {
        context.log(`Found product: ${inputTable.name} - $${inputTable.price}`);

        if (newPrice !== undefined) {
            // Using output binding to update
            context.bindings.outputTable = {
                PartitionKey: partitionKey,
                RowKey: rowKey,
                name: inputTable.name, // Keep existing name
                price: newPrice
            };
            context.res = { body: "Product price updated successfully." };
        } else {
            context.res = { body: inputTable };
        }
    } else {
        context.res = { status: 404, body: "Product not found." };
    }
};
            

Troubleshooting