I want to create a Gatsby Azure static web app.
What is the best practice for implementing CRUD operations via REST API connection to an Azure SQL database in different Azure Static web app forms?
Please consider the two different types of tables:
- 1:1/1:N relationship tables -> for example: Books-Table
- N:M relationship tables -> for example: Book-Category-Table
Here are my ideas:
1:1/1:N relationship-table
C(reate) book form:
POST (for inserting new book)
R(ead) book(s) form:
GET (for selecting one or all books)
U(pdate) book form:
PUT (for updating the selected book)
D(elete) book form:
DELETE (for deleting the selected book)
N:M relationship table
C(reate) book-category form:
POST (for inserting new book-category combinations)
R(ead) book-category form:
GET (for selecting one or all book-category combinations)
U(pdate) book-category form:
DELETE (for deleting all book-category combinations)
POST (for inserting the updated/new book-category combinations)
D(elete) book-category form:
DELETE (for deleting the relevant book-category combination)
Is there any other function to handle especially updates of N:M relationship table?
Can I send any request which checks which book-category combinations already exists and handles them properly?
I am totally open for different ideas or approaches.
Addition:
My main question is: How can I implement the update of a N:M relationsship. I have a table book (with book_id as primary key) and a table book-category (with surrogate key "book AND category")
For example the book-category-table looks like that: Book 1 - Category 10 Book 1 - Category 11
Now I want to update the relevant categories via form and REST API call. Book 1 - Category 10 Book 1 - Category 20
How can I do that? Do I have to delete all entries for "book = 1" and then insert the new values? I can not update the book, I have to update the book-category table.
The below sample code is to handle REST API calls in Azure Static web apps
The
gatsby-source-rest-apiplugin enables you to fetch data from various REST APIs and use it within your Gatsby application.The below sample is to fetching data from REST APIs and displaying it on your Gatsby site with data sourced from REST APIs using the
gatsby-source-rest-apiplugin.gatsby-source-rest-apipackagenpm install --save gatsby-source-rest-apigatsby-config.js:
posts.js:
seo.js:
Azure Static Web App Output:
Call all your CRUD operations URL via a REST API connection to an Azure SQL database in
gatsby-config.jsand change the logic according to the requirements in Gatsby and deploy to the Azure static web app. Not sure about the functionality to handle updates of N:M relationship table.Yes, the functionality of checking book-category combinations is possible.
Implementing an N:M relationship:
Updated:
Creating a REST API with Node.js and Express.
GET Endpoint:
/api/bookcategories: This endpoint retrieves book categories based on the providedbookidquery parameter.POST Endpoint:
/api/bookcategories: This endpoint adds a new book category. It expects theBookIDandCategoryIDin the request body.DELETE Endpoint:
/api/bookcategories: This endpoint deletes a book category based on the providedbookidandcategoryidquery parameters.Use this documentation to create a Node.js web app on Azure App Service.
I followed this doc to deploy the sample code as an app on Azure.