Skip to main content

Azure Static Web App–Data API Builder

As a follow-up on the presentation I did at CloudBrew about Azure Static Web Apps I want to write a series of blog posts.

So far I have shown you 2 different possibilities to integrate an API inside your Azure Static Web App:

  1. You have the Managed Function option where you get a built-in Azure Function with a limited set of functionality for free.
  2. You can link your existing API built using Azure Functions, Azure Container Apps, Azure API Management or Azure App Service.

If we look at most applications, these API’s are used to read and write data in a database. If this is also the case for your application, Azure Static Web App has a 3th possibility; the database connection feature.

Database connection feature

The database connection feature allows you to access a database from your static web app without writing custom server-side code. It is based on the Data API Builder project I talked about in a previous post and allows to create a REST and GraphQL api based on your data source.

Both relational(Azure SQL, MySQL and PostgreSQL) as non-relational (Azure Cosmos DB) databases are supported.

It is the perfect fit if you want an API with simple CRUD operations, built-in authorization, and relationships.

Integrate the Data API Builder into your Azure Static Web App

It all starts by creating a swa-db-connections folder at the root of your repository. Inside this folder you should add a staticwebapp.database.config.json file. This file defines the runtime behavior of the database connection.

Remark: You can use a different folder if you want by changing the data_api_location setting in your build pipeline:

Inside the staticwebapp.database.config.json file, we can specify if we want to expose both a REST and GraphQL endpoint, which Tables or Entities can be accessed, which operations are supported and so on…

Here is an example where I allow all CRUD actions on a dbo.Product table:

Remark: If you want to learn more on how to configure the Data API Builder, have a look at my previous post and the Data API Builder documentation.

Once the configuration file is in place, we can go to the Azure Portal and link a database to our static web app:

  • Go to your Azure Static Web App in the Azure Portal.
  • Open the Database connection section in the Settings.
  • Click on the Link existing database link to any of your environments.
  • On the Link existing database screen, enter the following details:
    • A database type(e.g. Azure SQL Database)
    • The subscription and resource group where the database can be found
    • The Resource name of the database instance and the specific database inside the instance
    • The authentication type
    • Depending on the authentication type, you need to enter different details. In our case we need to provide a username and password.
  • Click on Link to complete the process.

Remark: If your Azure Static Web App isn’t able to connect to your database, check that Allow Azure services and resources to access this server is checked in the Networking –> Exceptions section of your database server.

Calling the API

We can now call the API through the /data-api endpoint. As we have set in our configuration that anonymous access is allowed, we can directly call the endpoint without authenticating first.

Here is the call using the REST endpoint:

And here we are using the GraphQL endpoint(notice that we get introspection for free):

Remark: The first call can take a little bit longer so certainly implement a retry strategy.

More information

Connecting to a database with Azure Static Web Apps | Microsoft Learn

Popular posts from this blog

Podman– Command execution failed with exit code 125

After updating WSL on one of the developer machines, Podman failed to work. When we took a look through Podman Desktop, we noticed that Podman had stopped running and returned the following error message: Error: Command execution failed with exit code 125 Here are the steps we tried to fix the issue: We started by running podman info to get some extra details on what could be wrong: >podman info OS: windows/amd64 provider: wsl version: 5.3.1 Cannot connect to Podman. Please verify your connection to the Linux system using `podman system connection list`, or try `podman machine init` and `podman machine start` to manage a new Linux VM Error: unable to connect to Podman socket: failed to connect: dial tcp 127.0.0.1:2655: connectex: No connection could be made because the target machine actively refused it. That makes sense as the podman VM was not running. Let’s check the VM: >podman machine list NAME         ...

Cache stampede: when our cache turned against us

While investigating some performance issues, we ran into an ASP.NET Core API that cached a fairly expensive aggregation query for 60 seconds. Under normal load, that was fine: one request rebuilds the cache, everyone else reads from it. Under peak load, dozens of requests would arrive in that same expiry window, all see a cache miss, and all fire the same expensive query in parallel. The database didn't like that. That was the moment when our caching layer stopped helping and started hurting. A burst of requests comes in at the same time, all miss the cache, and all go hammer the database or the downstream API at once. That's a cache stampede . The cache was supposed to protect our backend, and for a few hundred milliseconds it did the opposite. Why this happens IMemoryCache.GetOrCreate (and its async sibling) looks like it protects you, but it doesn't add any locking on its own. Look at the naive version: public async Task<Report> GetReportAsync(string key) ...

A complex system designed from scratch never works

A few years ago, I worked as an architect on a big mainframe rewrite. I still count it as one of my failures. Not because the technology was wrong, but because I couldn't convince the management team to simplify the approach. Years later, the organization is still struggling to get the new system up and running. I left the project at the time, because I couldn't put my name behind an approach that would take very long and cost a lot of money without a working system to show for it along the way. Gall’s Law That memory keeps coming back to me, because it's a textbook case of Gall's Law playing out in real life. Gall's Law , from John Gall's Systemantics , states it plainly: A complex system that works is invariably found to have evolved from a simple system that worked. A complex system designed from scratch never works, and it cannot be patched to make it work. You have to start over with a simple system that works. What does that mean in practice,...