이 빠른 시작에서는 DAB(Data API Builder)를 사용하여 로컬 SQL 데이터베이스에 대한 REST 및 GraphQL 엔드포인트를 만듭니다. 시작할 데이터베이스 엔진을 선택합니다.
사전 요구 사항
Data API Builder CLI 설치
NuGet에서 Microsoft.DataApiBuilder 패키지를 .NET 도구로 설치합니다.
최신 버전
dotnet tool install을Microsoft.DataApiBuilder인수를 사용하여 설치하려면--global을 사용하십시오.dotnet tool install --global Microsoft.DataApiBuilder메모
패키지가 이미 설치된 경우에는
dotnet tool update을 사용하여 대신 패키지를 업데이트하십시오.dotnet tool update --global Microsoft.DataApiBuilderdotnet tool list인수를 사용하여 도구가--global설치되어 있는지 확인합니다.dotnet tool list --global
데이터베이스 이미지 끌어오기
팁 (조언)
데이터베이스가 이미 있나요? 데이터베이스 만들기 및 시드로 건너뛰고, 엔진에 대한 SQL 스크립트를 실행한 다음, 고유한 연결 문자열을 사용하여 데이터 API 작성기 구성으로 이동합니다.
데이터베이스 엔진에 대한 Docker 이미지를 다운로드합니다. 이 단계는 연결 속도에 따라 몇 분 정도 걸릴 수 있습니다.
docker pull mcr.microsoft.com/mssql/server:2025-latest
데이터베이스 시작
Docker에서 로컬 데이터베이스 인스턴스를 실행합니다.
docker run --name dab-mssql --env "ACCEPT_EULA=Y" --env "MSSQL_SA_PASSWORD=P@ssw0rd1" --publish 1433:1433 --detach mcr.microsoft.com/mssql/server:2025-latest
팁 (조언)
포트 1433가 이미 사용 중인 경우(예: 로컬 SQL Server 설치에 의해), --publish을/를 1434:1433와 같은 다른 호스트 포트로 변경하고, 이후 단계에서 Server=localhost,1433을/를 Server=localhost,1434로 업데이트하세요.
다음 명령을 실행하기 전에 데이터베이스 엔진이 준비되었는지 확인합니다.
docker exec dab-mssql /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "P@ssw0rd1" -C -Q "SELECT 1"
오류가 반환되면 몇 초 정도 기다렸다가 다시 시도하세요.
데이터베이스 만들기 및 시드
todos 데이터베이스 및 테이블을 만든 다음 샘플 데이터를 추가합니다. Docker를 사용하는 경우 SQL 클라이언트가 필요하지docker exec 않습니다. 컨테이너 내에서 직접 명령을 실행합니다. 사용자 고유의 데이터베이스를 사용하는 경우 원하는 도구에서 SQL 스크립트를 실행합니다.
데이터베이스를 만듭니다.
docker exec dab-mssql /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "P@ssw0rd1" -C -Q "CREATE DATABASE todos;"테이블을 만들고 샘플 데이터를 추가합니다.
docker exec dab-mssql /opt/mssql-tools18/bin/sqlcmd -S localhost -U sa -P "P@ssw0rd1" -C -d todos -Q "CREATE TABLE dbo.todos (id int PRIMARY KEY, title nvarchar(100) NOT NULL, completed bit NOT NULL DEFAULT 0); INSERT INTO dbo.todos VALUES (1, 'Walk the dog', 0), (2, 'Feed the fish', 0), (3, 'Comb the cat', 1);"
팁 (조언)
사용자 고유의 SQL Server를 사용하시겠습니까? 이 스크립트를 직접 실행합니다.
CREATE DATABASE todos;
GO
USE todos;
GO
CREATE TABLE dbo.todos (id int PRIMARY KEY, title nvarchar(100) NOT NULL, completed bit NOT NULL DEFAULT 0);
INSERT INTO dbo.todos VALUES (1, 'Walk the dog', 0), (2, 'Feed the fish', 0), (3, 'Comb the cat', 1);
데이터 API 작성기 구성
DAB 구성 파일을 만들고 Todo 엔터티를 추가합니다.
팁 (조언)
사용자 고유의 데이터베이스를 사용하시겠습니까? 연결 문자열 dab init 을 사용자 고유의 문자열로 바꿉다.
-
SQL Server:
Server=<host>,<port>;Database=todos;User Id=<user>;Password=<password>;TrustServerCertificate=true;Encrypt=true; -
Postgresql:
Host=<host>;Port=5432;Database=todos;User ID=<user>;Password=<password>; -
Mysql:
Server=<host>;Port=3306;Database=todos;User=<user>;Password=<password>;
구성을 초기화합니다.
dab init --database-type "mssql" --host-mode "Development" --connection-string "Server=localhost,1433;Database=todos;User Id=sa;Password=P@ssw0rd1;TrustServerCertificate=true;Encrypt=true;"Todo 엔터티를 추가합니다.
dab add Todo --source "dbo.todos" --permissions "anonymous:*"
dab-config.json 이제 파일은 다음 예제와 유사하게 표시됩니다.
{
"$schema": "https://github.com/Azure/data-api-builder/releases/download/vmajor.minor.patch/dab.draft.schema.json",
"data-source": {
"database-type": "mssql",
"connection-string": "Server=localhost,1433;Database=todos;User Id=sa;Password=P@ssw0rd1;TrustServerCertificate=true;Encrypt=true;"
},
"runtime": {
"rest": {
"enabled": true
},
"graphql": {
"enabled": true
},
"host": {
"mode": "development",
"cors": {
"origins": ["*"]
}
}
},
"entities": {
"Todo": {
"source": "dbo.todos",
"permissions": [
{
"role": "anonymous",
"actions": [
"*"
]
}
]
}
}
}
팁 (조언)
dab init 및 dab add 명령을 건너뛰고 여기에 표시된 내용으로 dab-config.json 파일을 직접 만들 수 있습니다.
API 시작
dab start를 사용하여 도구를 실행하고 엔터티에 대한 API 엔드포인트를 만드십시오.
dab start
출력에는 실행 중인 API의 주소가 포함되어야 합니다.
Successfully completed runtime initialization.
info: Microsoft.Hosting.Lifetime[14]
Now listening on: <http://localhost:5000>
팁 (조언)
이 예제에서 애플리케이션은 포트 localhost에서 실행됩니다. 실행 중인 애플리케이션의 주소와 포트가 다를 수 있습니다.
API 테스트
브라우저를 열고 Todo 엔터티에 대한 REST 엔드포인트로 이동합니다.
http://localhost:5000/api/TodoJSON 응답에는 세 가지 할 일 항목이 모두 포함되어야 합니다.
{ "value": [ { "id": 1, "title": "Walk the dog", "completed": false }, { "id": 2, "title": "Feed the fish", "completed": false }, { "id": 3, "title": "Comb the cat", "completed": true } ] }Swagger 설명서 페이지
/swagger에서로 이동합니다.http://localhost:5000/swagger
웹앱 빌드
일반 HTML 파일을 사용하여 브라우저에서 작업 목록을 표시합니다. REST 또는 GraphQL 엔드포인트를 사용하여 명명된 todo.html 파일을 만듭니다.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Todo App</title>
<style>
body { font-family: sans-serif; max-width: 400px; margin: 2rem auto; }
li.done { text-decoration: line-through; color: gray; }
#error { color: red; }
</style>
</head>
<body>
<h1>Todos</h1>
<ul id="list"></ul>
<p id="error"></p>
<script>
fetch('http://localhost:5000/api/Todo')
.then(r => r.json())
.then(data => {
const ul = document.getElementById('list');
data.value.forEach(todo => {
const li = document.createElement('li');
li.textContent = todo.title;
if (todo.completed) li.className = 'done';
ul.appendChild(li);
});
})
.catch(() => {
document.getElementById('error').textContent =
'Could not reach the API. Make sure DAB is running on http://localhost:5000.';
});
</script>
</body>
</html>
todo.html을(를) 브라우저에서 여세요. 페이지는 모든 할 일 항목을 가져와서 취소선에 표시된 완료된 항목과 함께 목록으로 렌더링합니다.
중요합니다
구성의 cors 설정을 통해 로컬 파일 시스템에서 연 이 HTML 파일이 API를 호출할 수 있습니다. 이 요청이 없으면 브라우저에서 요청을 차단합니다.
정리
완료되면 Docker 컨테이너를 중지하고 제거합니다.
docker stop dab-mssql && docker rm dab-mssql