> ## Documentation Index
> Fetch the complete documentation index at: https://forest-chore-open-api.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Azure Table Storage

<Check>
  This How to is based on the [Medium article](https://avarnon.medium.com/exposing-azure-table-storage-through-forest-admin-2d601752f9b1) by [Andrew Varnon](https://avarnon.medium.com/)
</Check>

The implementation is done using a [Smart Collection](https://docs.forestadmin.com/documentation/reference-guide/collections/create-a-smart-collection) and a CRUD service that will wrap the [Azure Table Storage API](https://docs.microsoft.com/en-us/rest/api/storageservices/table-service-rest-api).

### The Table Storage Definition

You can use the new [Azure Data Explorer](https://azure.microsoft.com/en-us/services/data-explorer/) to create and populate a Table Storage in your [Azure Storage account](https://docs.microsoft.com/en-us/azure/storage/common/storage-account-overview).&#x20;

<img src="https://mintcdn.com/forest-chore-open-api/l9oWVTFSA2iV8NAX/images/legacy/javascript-agents/image%20(515).png?fit=max&auto=format&n=l9oWVTFSA2iV8NAX&q=85&s=fa09c91e06e4a42552600a9ee90c1ee1" alt="" width="1411" height="353" data-path="images/legacy/javascript-agents/image (515).png" />

In our example, we are going to use the Table Customers with the fields:

* **Id**: PartitionKey + RowKey
* **Timestamp** (updated at)
* **Email** as String
* **FirstName** as String
* **LastName** as String

### Install Azure `data-tables` package

```haskell theme={null}
npm install @azure/data-tables --save
```

### Smart Collection definition

### The Azure Data Tables Service Wrapper

```javascript theme={null}
const { TableClient } = require('@azure/data-tables');

const getClient = (tableName) => {
  const client = TableClient.fromConnectionString(
    process.env.AZURE_STORAGE_CONNECTION_STRING,
    tableName
  );
  return client;
};

const azureTableStorageService = {
  deleteEntityAsync: async (tableName, partitionKey, rowKey) => {
    const client = getClient(tableName);
    await client.deleteEntity(partitionKey, rowKey);
  },

  getEntityAsync: async (tableName, partitionKey, rowKey) => {
    const client = getClient(tableName);
    return client.getEntity(partitionKey, rowKey);
  },

  listEntitiesAsync: async (tableName, options) => {
    const client = getClient(tableName);
    var azureResponse = await client.listEntities();

    let iterator = await azureResponse.byPage({
      maxPageSize: options.pageSize,
    });

    for (let i = 1; i < options.pageNumber; i++) iterator.next(); // Skip pages

    let entities = await iterator.next();
    let records = entities.value.filter((entity) => entity.etag);

    // Load an extra page if we need to allow (Next Page)
    const entitiesNextPage = await iterator.next();
    let nbNextPage = 0;
    if (entitiesNextPage && entitiesNextPage.value) {
      nbNextPage = entitiesNextPage.value.filter(
        (entity) => entity.etag
      ).length;
    }

    // Azure Data Tables does not provide a row count.
    // We just inform the user there is a new page with at least x items
    const minimumRowEstimated =
      (options.pageNumber - 1) * options.pageSize + records.length + nbNextPage;

    return { records, count: minimumRowEstimated };
  },

  createEntityAsync: async (tableName, entity) => {
    const client = getClient(tableName);
    delete entity['__meta__'];
    await client.createEntity(entity);
    return client.getEntity(entity.partitionKey, entity.rowKey);
  },

  updateEntityAsync: async (tableName, entity) => {
    const client = getClient(tableName);
    await client.updateEntity(entity, 'Replace');
    return client.getEntity(entity.partitionKey, entity.rowKey);
  },
};

module.exports = azureTableStorageService;
```

### Routes definition

```javascript theme={null}
const express = require('express');
const {
  PermissionMiddlewareCreator,
  RecordCreator,
  RecordUpdater,
} = require('forest-express');
const { RecordSerializer } = require('forest-express');

const router = express.Router();

const COLLECTION_NAME = 'customers';
const permissionMiddlewareCreator = new PermissionMiddlewareCreator(
  COLLECTION_NAME
);
const recordSerializer = new RecordSerializer({ name: COLLECTION_NAME });

const azureTableStorageService = require('../services/azure-table-storage-service');

// Get a list of Customers
router.get(
  `/${COLLECTION_NAME}`,
  permissionMiddlewareCreator.list(),
  async (request, response, next) => {
    const pageSize = parseInt(request.query.page.size) || 15;
    const pageNumber = parseInt(request.query.page.number);

    azureTableStorageService
      .listEntitiesAsync(COLLECTION_NAME, { pageSize, pageNumber })
      .then(async ({ records, count }) => {
        const recordsSerialized = await recordSerializer.serialize(records);
        response.send({ ...recordsSerialized, meta: { count } });
      })
      .catch((e) => {
        console.error(e);
        next(e);
      });
  }
);

// Get a Customer
router.get(
  `/${COLLECTION_NAME}/:recordId`,
  permissionMiddlewareCreator.details(),
  async (request, response, next) => {
    const parts = request.params.recordId.split('|');
    azureTableStorageService
      .getEntityAsync(COLLECTION_NAME, parts[0], parts[1])
      .then((record) => recordSerializer.serialize(record))
      .then((recordSerialized) => response.send(recordSerialized))
      .catch((e) => {
        console.error(e);
        next(e);
      });
  }
);

// Create a Customer
router.post(
  `/${COLLECTION_NAME}`,
  permissionMiddlewareCreator.create(),
  async (request, response, next) => {
    const recordCreator = new RecordCreator(
      { name: COLLECTION_NAME },
      request.user,
      request.query
    );
    recordCreator
      .deserialize(request.body)
      .then((recordToCreate) => {
        return azureTableStorageService.createEntityAsync(
          COLLECTION_NAME,
          recordToCreate
        );
      })
      .then((record) => recordSerializer.serialize(record))
      .then((recordSerialized) => response.send(recordSerialized))
      .catch((e) => {
        console.error(e);
        next(e);
      });
  }
);

// Update a Customer
router.put(
  `/${COLLECTION_NAME}/:recordId`,
  permissionMiddlewareCreator.update(),
  async (request, response, next) => {
    const parts = request.params.recordId.split('|');
    const recordUpdater = new RecordUpdater(
      { name: COLLECTION_NAME },
      request.user,
      request.query
    );
    recordUpdater
      .deserialize(request.body)
      .then((recordToUpdate) => {
        recordToUpdate.partitionKey = parts[0];
        recordToUpdate.rowKey = parts[1];
        return azureTableStorageService.updateEntityAsync(
          COLLECTION_NAME,
          recordToUpdate
        );
      })
      .then((record) => recordSerializer.serialize(record))
      .then((recordSerialized) => response.send(recordSerialized))
      .catch((e) => {
        console.error(e);
        next(e);
      });
  }
);

// Delete a list of Customers
router.delete(
  `/${COLLECTION_NAME}`,
  permissionMiddlewareCreator.delete(),
  async (request, response, next) => {
    try {
      for (const key of request.body.data.attributes.ids) {
        const parts = key.split('|');
        await azureTableStorageService.deleteEntityAsync(
          COLLECTION_NAME,
          parts[0],
          parts[1]
        );
      }
      response.status(204).send();
    } catch (e) {
      console.error(e);
      next(e);
    }
  }
);

module.exports = router;
```
