/azure-sql-db-node-rest-api

REST API using Node, Azure Functions and Azure SQL

Primary LanguageTSQLMIT LicenseMIT

page_type languages products description urlFragment
sample
nodejs
javascript
tsql
sql
json
azure
vs-code
azure-sql-database
azure-functions
Creating a modern REST API with Node and Azure SQL, Azure Functions and Visual Studio Code
azure-sql-db-node-rest-api

Serverless REST API with Azure Functions, Node and Azure SQL

License

Thanks to native JSON support, creating a serverless REST API with Azure Functions, Azure SQL and Node is really a matter of a few lines of code. Take a look at customer/index.js to see how easy it is!

Wondering what's the magic behind? Azure Functions takes care of running the NodeJS code, so all is needed is to get the incoming HTTP request, handle it, send the data as we receive it - a JSON - to Azure SQL and we're done. Thanks to the native JSON support that Azure SQL provides, it does all the heavy lifting for as so sending data back and forth to the database is as easy as sending a JSON message.

Install Sample Database

In order to run this sample, the WideWorldImporters database is needed. Install WideWorldImporters sample database:

Restore WideWorldImporters Database

Add Database Objects

Once the sample database has been installed, you need to add some stored procedure that will be called from JavaScript. The SQL code is available here:

./sql/WideWorldImportersUpdates.sql

If you need any help in executing the SQL script, you can find a Quickstart here: Quickstart: Use Azure Data Studio to connect and query Azure SQL database

Run sample locally

Make sure you add the information needed to connect to the desired Azure SQL database in the local.settings.json. Create it in the root folder of the sample using the .template file, if there isn't one already. After editing the file should look like the following:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "db_server": "<server>.database.windows.net",
    "db_database": "<database>",
    "db_user": "NodeFuncApp",
    "db_password": "aN0ThErREALLY#$%TRONGpa44w0rd!"
  }
}

where <server> and <database> have been replaced with the correct values for your environment. Once done that, start local Azure Function host with

func start

if you are using Azure Functions Core Tools or using Visual Studio Code Azure Function extension

For more info on other options to run Azure Function locally look here:

Code and test Azure Functions locally

Once the Azure Function HTTP is up and running you'll see something like

Now listening on: http://0.0.0.0:7071
Application started. Press Ctrl+C to shut down.

Http Functions:

        customer: [GET,PUT,PATCH,DELETE] http://localhost:7071/api/customer/{id:int?}

Using a REST Client (such as Visual Studio, Insomnia, Curl or PowerShell's Invoke-RestMethod), you can now call your API, for example:

curl -X GET http://localhost:7071/customer/123

and you'll get info on Customer 123:

[
    {
        "CustomerID": 123,
        "CustomerName": "Tailspin Toys (Roe Park, NY)",
        "PhoneNumber": "(212) 555-0100",
        "FaxNumber": "(212) 555-0101",
        "WebsiteURL": "http://www.tailspintoys.com/RoePark",
        "Delivery": {
            "AddressLine1": "Shop 219",
            "AddressLine2": "528 Persson Road",
            "PostalCode": "90775"
        }
    }
]

Check out more samples to test all implemented verbs here:

cUrl Samples

Debug from Visual Studio Code

Debugging from Visual Studio Code is fully supported, thanks to the Visual Studio Code Azure Function extension

Deploy to Azure

Now that your REST API solution is ready, it's time to deploy it on Azure so that anyone can take advantage of it. A script azure-deploy.sh that uses AZ CLI (so it needs to be executed from a Linux shell. If you don't have one on your machine you can use Azure Cloud Shell) is available within the repo. Just make sure you fill the needed settings into the local.settings.json as mentioned before as the script will read the values from there.

./azure-deploy.sh

It will care of everything for you:

  • Creating a Resource Group (you can set the name you want by changing it directly in the .sh file)
  • Creating a Storage Account
  • Creating Azure Application Insights
  • Create an Azure Function App
  • Deploying repo code to Azure Function

Enjoy!

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.opensource.microsoft.com.

When you submit a pull request, a CLA bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., status check, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

More details in the full Contributing page.