Quickstarters
CRUD Samples
How to Create a CRUD App with Gatsby?
34 min
introduction in this walkthrough, you'll discover how to build a crud (create, read, update, delete) web application using gatsby we'll use back4app as your cloud backend to effortlessly manage data this guide walks you through configuring a back4app project, defining a flexible schema, and connecting your gatsby app with rest or graphql api calls to perform crud operations initially, you'll set up a back4app project named basic crud app gatsby this project gives you access to a robust non relational database you will design your data schema by manually creating classes and fields in the back4app dashboard or by leveraging back4app's ai agent next, you'll use the back4app admin app—a drag and drop interface—to handle data management tasks effortlessly finally, you'll integrate your gatsby application with the back4app api (via rest or graphql) to execute secure crud operations by the end of this tutorial, you’ll have developed a production ready gatsby app that performs basic crud operations with secure data handling and user authentication key takeaways learn how to craft a crud application with gatsby backed by a low code backend understand how to structure your backend and link it with a modern gatsby site utilize back4app’s intuitive admin app to manage data creation, reading, updating, and deletion discover deployment options including containerization strategies for your gatsby app prerequisites before you begin, make sure you have a back4app account with an active project need help? see getting started with back4app https //www back4app com/docs/get started/new parse app a node js environment with gatsby cli installed use node js https //nodejs org/ and install gatsby via npm install g gatsby cli familiarity with react, graphql, and rest apis review the react documentation https //reactjs org/docs/getting started html if needed step 1 – setting up your project creating a new back4app project log in to your back4app account click on “new app” from your dashboard name your project basic crud app gatsby and complete the setup steps create new project after creation, your project will appear on the dashboard, ready for backend configuration step 2 – defining your data model configuring your schema for this crud app, you need to set up several classes in back4app below are examples of the essential classes and fields 1\ items class this class stores details about each item field data type description id objectid auto generated unique identifier title string name of the item description string brief description of the item createdat date timestamp when the item was created updatedat date timestamp when the item was last updated 2\ users class this class handles user credentials and authentication field data type description id objectid automatically generated unique id username string unique identifier for the user email string user’s unique email address passwordhash string encrypted password for login createdat date account creation timestamp updatedat date timestamp of the latest account update you can add these classes and fields directly in the back4app dashboard create new class you may define fields by choosing a data type, naming the field, setting defaults, and marking required fields create column using the back4app ai agent for schema generation the back4app ai agent can automatically build your schema based on your specifications this speeds up the setup and ensures your data model supports all crud operations how to use the ai agent open the ai agent sign into your back4app dashboard and find the ai agent under your project settings input your requirements describe the classes and fields you need review and apply the agent will suggest a schema review it and confirm to implement example prompt create the following classes in my back4app project 1\) class items \ fields \ id objectid (auto generated) \ title string \ description string \ createdat date (auto generated) \ updatedat date (auto updated) 2\) class users \ fields \ id objectid (auto generated) \ username string (unique) \ email string (unique) \ passwordhash string \ createdat date (auto generated) \ updatedat date (auto updated) this ai assisted approach ensures a consistent and efficient data schema step 3 – enabling the admin app & managing crud operations overview of the admin app the back4app admin app provides a no code interface for backend data management its user friendly drag and drop features simplify crud operations like adding, viewing, updating, and deleting records activating the admin app go to the “more” menu on your back4app dashboard select “admin app” and click “enable admin app ” configure your admin credentials by setting up the first admin account this step also creates roles (e g , b4aadminuser ) and system classes enable admin app once activated, sign into the admin app to manage your backend data admin app dashboard using the admin app for crud tasks inside the admin app, you can insert records use the “add record” feature in a class (such as items) to create new entries inspect/update records click on any entry to view details or modify information delete records remove outdated or unnecessary records this streamlined interface makes data management quick and intuitive step 4 – connecting your gatsby app with back4app with your backend set up, the next phase is linking your gatsby application to back4app using api calls in gatsby we’ll utilize rest or graphql calls to communicate with back4app a second option is using the sdk fetching data using rest you can use javascript’s fetch api to retrieve data for example, to load items from back4app // src/services/api js export const fetchitems = async () => { try { const response = await fetch('https //parseapi back4app com/classes/items', { headers { 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key', }, }); const data = await response json(); return data results; } catch (error) { console error('error fetching items ', error); return \[]; } }; sending data via rest to add a new item, use a post request // src/services/api js export const createitem = async (title, description) => { try { const response = await fetch('https //parseapi back4app com/classes/items', { method 'post', headers { 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key', 'content type' 'application/json', }, body json stringify({ title, description }), }); const data = await response json(); return data; } catch (error) { console error('error creating item ', error); } }; you can similarly implement update and delete functions using put/post and delete methods integrate these api calls into your gatsby pages or components to manage data dynamically step 5 – securing your backend implementing access controls protect your data by setting access control lists (acls) in back4app for example, to create an item visible only to its owner // example payload for a private item const privateitem = { title 'private title', description 'this item is private', acl { " " { "read" false, "write" false }, "user object id" { "read" true, "write" true } } }; class level permissions (clps) set up clps in your back4app dashboard to enforce default access rules, ensuring only authenticated users can access specific classes step 6 – adding user authentication configuring user accounts in gatsby back4app leverages parse’s built in user class for authentication in your gatsby app, manage user registration and login via api calls example user registration // src/services/auth js export const signup = async (username, password, email) => { try { const response = await fetch('https //parseapi back4app com/users', { method 'post', headers { 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key', 'content type' 'application/json', }, body json stringify({ username, password, email }), }); const data = await response json(); return data; } catch (error) { console error('error during sign up ', error); } }; example user login // src/services/auth js export const login = async (username, password) => { try { const response = await fetch(`https //parseapi back4app com/login?username=${username}\&password=${password}`, { headers { 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key', }, }); const data = await response json(); return data; } catch (error) { console error('login error ', error); } }; this approach can be extended to manage sessions, password resets, and more step 7 – deploying your gatsby application back4app supports a smooth deployment process you can deploy your gatsby app using several methods, including containerization 7 1 building your gatsby site install dependencies run npm install build the site use the gatsby build command gatsby build test locally serve your production build to verify functionality gatsby serve 7 2 project structure overview a typical gatsby project structure might look like basic crud app gatsby/ \| node modules/ \| src/ \| | components/ \| | | itemform js \| | | itemlist js \| | pages/ \| | index js \| | login js \| static/ \| gatsby config js \| package json \| readme md 7 3 dockerizing your gatsby application if you prefer containerized deployments, add a dockerfile \# use an official node js image as the base from node 14 alpine \# set the working directory workdir /app \# copy package files and install dependencies copy package json / run npm install \# copy the remaining source code copy \# build the gatsby site run npm run build \# expose port (adjust if needed) expose 8000 \# serve the built site cmd \["npx", "gatsby", "serve", " h", "0 0 0 0"] 7 4 deploying via back4app web deployment link your repository host your gatsby source code on github configure deployment in the back4app dashboard, select web deployment , connect your repository (e g , basic crud app gatsby ), and pick the branch set build commands specify the build command (e g , npm run build ) and the output directory deploy click deploy and monitor the logs until your site is live step 8 – conclusion and next steps great job! you’ve built a gatsby based crud application integrated with back4app you set up a project named basic crud app gatsby , defined your data model, managed data with the admin app, and connected your gatsby front end to back4app using rest/graphql api calls next steps enhance your app consider adding advanced search, detailed views, or real time updates expand backend capabilities explore cloud functions, third party api integrations, or refined role based access controls learn more visit the back4app documentation https //www back4app com/docs for additional insights and tutorials happy coding and enjoy building your gatsby crud application!