Connecting to a database with Azure Static Web Apps (preview)
Article
03/15/2023
The Azure Static Web Apps database connection feature allows you to access a database from your static web app without writing custom server-side code.
Once you create a connection between your web application and database, you can manipulate data with full support for CRUD operations, built-in authorization, and relationships.
Based on the Data API builder, Azure Static Web Apps takes REST and GraphQL requests and converts them to database queries.
Features supported by database connections include:
Feature
Description
Integrated security
Built-in integration with Azure Static Web Apps authentication and authorization security model. The same role-based security used to secure routes is available for API endpoints.
You can use relational and document databases as your application's database.
Serverless architecture
Connections scale from 0 to 1 worker (during preview).
Database relationships
Supported only via the GraphQL endpoint.
CLI support
Develop locally with the Static Web Apps CLI. Use the --data-api-location option to handle requests to data APIs in development just as they're handled in the cloud.
Supported databases
The following table shows support for different relational and NoSQL databases.
Database connection endpoints that support REST and GraphQL requests.
example.com/*
Static content
When you configure database connections on your website, you can configure the REST or GraphQL suffix of the /data-api/* route. The /data-api prefix is a convention of Static Web Apps and can't be changed.
Configuration
There are two steps to configuring a database connection in Static Web Apps. You need to connect your database to your static web app in the Azure portal, and update your database connections configuration file.
This command starts the SWA CLI in the src directory. The --data-api-location option tells the CLI that a folder named swa-db-connections holds the staticwebapp.database.config.json file.
Note
In development, if you use a connection string to authenticate, use the env() function to read a connection string from an environment variable. The string passed in to the env function must be surrounded by quotes.
Role-based security
When you define an entity in the staticwebapp.database.config.json file, you can specify a list of roles required to access an entity endpoint.
The following configuration fragment requires the admin role to access all actions (create, read, update, delete) on the orders entity.
When you make calls to an endpoint that requires a role, the following conditions are required:
The current user must be authenticated.
The current user must be a member of the required role.
The REST or GraphQL request must include a header with the key of X-MS-API-ROLE and a value of the role name matching what's listed in the entity configuration rules.
For instance, the following snippet shows how to pass the admin role in a request header.
Administer an SQL Server database infrastructure for cloud, on-premises and hybrid relational databases using the Microsoft PaaS relational database offerings.