Quickstarters
CRUD Samples
How to Build a Basic CRUD App with RedwoodJS? A Step-by-Step Guide
37 min
overview in this guide, you'll learn how to create a basic crud (create, read, update, delete) application using redwoodjs this tutorial walks you through the process of setting up your project with back4app as your backend service, designing a robust database schema, and integrating it with a redwoodjs frontend by following these steps, you’ll build a production ready application that manages data effectively while employing modern development techniques essential insights master the creation of a crud application that efficiently handles data with a powerful backend service discover how to architect a scalable database and merge it with a redwoodjs interface to enhance user interaction explore the use of a no code drag and drop management tool—the back4app admin app—to streamline crud operations learn deployment strategies, including containerization with docker, to rapidly launch your web application prerequisites before you start, verify that you have the following an active back4app account with a new project ready if you need guidance, check out getting started with back4app https //www back4app com/docs/get started/new parse app a redwoodjs development environment set up use the redwoodjs cli to create your application ensure node js (version 14 or later) is installed a basic understanding of javascript, redwoodjs, and graphql for more details, visit the redwoodjs documentation https //redwoodjs com/docs/introduction step 1 – initiate your project starting a new back4app project log in to your back4app dashboard select “new app” to begin a new project name your project basic crud app redwoodjs and follow the setup instructions create new project your project will now appear in the dashboard, laying the groundwork for your backend configuration and management step 2 – crafting the database schema structuring your data model for this crud app, you'll design several collections below are examples of two essential collections along with the necessary fields these setups are crucial for enabling reliable crud functionalities 1\ collection items this collection holds details about each item field data type purpose id objectid auto generated unique identifier title string the name or title of the item description string a short summary describing the item created at date record creation timestamp updated at date timestamp of the latest update 2\ collection users this collection stores user credentials and details field data type purpose id objectid auto generated unique identifier username string unique identifier for the user email string user's unique email address password hash string encrypted password for authentication created at date account creation timestamp updated at date timestamp of the latest update you can configure these collections directly in the back4app dashboard by creating new classes and adding appropriate columns create new class define each field by choosing its type, naming it, and setting its default or required status create column employing the back4app ai agent for schema creation the back4app ai agent, accessible from your dashboard, can automatically generate your database schema based on a descriptive prompt this feature significantly accelerates the setup of a consistent schema for your application how to use the ai agent launch the ai agent access the agent from your back4app dashboard or project settings detail your data model enter a comprehensive prompt outlining the collections and fields you require review and implement examine the generated schema suggestions and apply them to your project sample prompt create these collections in my back4app project 1\) collection items \ fields \ id objectid (auto generated primary key) \ title string \ description string \ created at date (auto generated) \ updated at date (auto updated) 2\) collection users \ fields \ id objectid (auto generated primary key) \ username string (unique) \ email string (unique) \ password hash string \ created at date (auto generated) \ updated at date (auto updated) utilizing this ai driven approach not only saves time but also guarantees a well optimized and uniform schema step 3 – activating the admin app & managing crud operations introduction to the admin app the back4app admin app is a user friendly, no code interface that empowers you to handle your backend data effortlessly its drag and drop functionality makes performing crud operations—such as adding, viewing, updating, and deleting records—a breeze how to enable the admin app go to the “more” menu in your back4app dashboard select “admin app” and then click on “enable admin app ” set up your admin credentials by creating an initial admin user, which also establishes roles (e g , b4aadminuser ) and system collections enable admin app after activation, log into the admin app to manage your data admin app dashboard performing crud via the admin app within the admin app, you can add new records use the “add record” button in a collection (e g , items) to insert new data view and modify records click on a record to see its details and make updates remove records select the delete option to eliminate data that is no longer needed this intuitive interface greatly simplifies data management tasks step 4 – integrating redwoodjs with back4app now that your backend is set up and manageable via the admin app, it’s time to connect your redwoodjs frontend with back4app using graphql api integration redwoodjs is built around graphql, making it a perfect match for integrating with back4app via api calls instead of using the sdk, you will interact with your backend using graphql queries and mutations setting up your redwoodjs project create a new redwoodjs application yarn create redwood app basic crud app redwood navigate to your project directory cd basic crud app redwood configure your environment variables in your env file, add your back4app credentials back4app app id=your application id back4app rest api key=your rest api key back4app server url=https //parseapi back4app com fetching data with redwoodjs cells redwoodjs cells simplify data fetching here’s an example of a cell that retrieves items // web/src/components/itemscell/itemscell js export const query = gql` query items query { items itemscollection { id title description } } `; export const loading = () => \<div>loading items…\</div>; export const empty = () => \<div>no items available \</div>; export const failure = ({ error }) => \<div>error {error message}\</div>; export const success = ({ items }) => { return ( \<ul> {items map((item) => ( \<li key={item id}> \<strong>{item title}\</strong> — {item description} \</li> ))} \</ul> ); }; you can integrate similar graphql queries and mutations in your redwoodjs components to perform crud operations step 5 – securing your backend implementing access control protect your data by setting access control lists (acls) directly on objects for example, when creating a private item, you can ensure that only the owner has read/write privileges async function createprivateitem(itemdata, owner) { const response = await fetch(`${process env back4app server url}/classes/items`, { method 'post', headers { 'x parse application id' process env back4app app id, 'x parse rest api key' process env back4app rest api key, 'content type' 'application/json' }, body json stringify({ title itemdata title, description itemdata description, acl { \[owner] { read true, write true }, " " { read false, write false } } }) }); return response json(); } configuring class level permissions within the back4app dashboard, set class level permissions (clps) for each collection to restrict access to authenticated users or specific roles, ensuring your data remains secure step 6 – managing user authentication setting up account management back4app leverages a robust user system, which you can integrate into redwoodjs for user authentication in your redwoodjs app, handle user sign ups and logins through api calls example user registration // web/src/components/auth/signup js import { usestate } from 'react'; const signup = () => { const \[username, setusername] = usestate(''); const \[email, setemail] = usestate(''); const \[password, setpassword] = usestate(''); const handlesignup = async (e) => { e preventdefault(); try { const response = await fetch(`${process env back4app server url}/users`, { method 'post', headers { 'x parse application id' process env back4app app id, 'x parse rest api key' process env back4app rest api key, 'content type' 'application/json' }, body json stringify({ username, email, password }) }); const data = await response json(); if (data error) { alert('sign up failed ' + data error); } else { alert('user successfully registered!'); } } catch (error) { console error('registration error ', error); } }; return ( \<form onsubmit={handlesignup}> \<input type="text" placeholder="username" value={username} onchange={(e) => setusername(e target value)} /> \<input type="email" placeholder="email" value={email} onchange={(e) => setemail(e target value)} /> \<input type="password" placeholder="password" value={password} onchange={(e) => setpassword(e target value)} /> \<button type="submit">sign up\</button> \</form> ); }; export default signup; a similar method can be applied for logging in and maintaining user sessions step 7 – deploying your redwoodjs frontend via web deployment back4app’s web deployment feature makes hosting your redwoodjs frontend seamless follow these steps to push your code to production 7 1 build your production version open your project folder in the terminal run the build command yarn rw build this will generate a web/dist folder containing your optimized static files verify the build output ensure that the web/dist folder contains an index html along with all necessary assets 7 2 organize and commit your source code your repository should host the complete source for your redwoodjs application a typical structure might be basic crud app redwood/ ├── api/ ├── web/ │ ├── dist/ │ ├── src/ │ │ ├── components/ │ │ │ ├── itemscell/ │ │ │ └── auth/ │ │ └── app js │ └── index html ├── env └── package json committing to github initialize git (if needed) git init stage your files git add commit your code git commit m "initial commit for redwoodjs crud frontend" create a github repository (e g , basic crud app redwood ) and push your code git remote add origin https //github com/your username/basic crud app redwood git git push u origin main 7 3 integrate with web deployment access web deployment from your back4app dashboard under your project (basic crud app redwood) connect your github account if you haven’t already, following the prompts select your repository and branch (e g , main ) containing your redwoodjs code 7 4 set deployment parameters build command specify yarn rw build if your repository doesn’t include a pre built distribution output directory configure the output directory as web/dist environment variables include any necessary environment variables, such as your back4app keys 7 5 containerizing with docker if you favor docker for deployment, include a dockerfile in your repository for instance \# use an official node image for building the app from node 16 alpine as builder \# set the working directory workdir /app \# copy dependency definitions copy package json / \# install dependencies run yarn install \# copy the project files copy \# build the redwoodjs app run yarn rw build \# use nginx to serve the static files from nginx\ stable alpine copy from=builder /app/web/dist /usr/share/nginx/html expose 80 cmd \["nginx", " g", "daemon off;"] 7 6 deploying the application press the deploy button in back4app after finalizing your configuration monitor the deployment process back4app will fetch your code, build the project, and deploy it in a containerized environment retrieve your application url once deployed, a url will be provided where your redwoodjs application is hosted 7 7 validate your deployment open the provided url in your browser to confirm that the site loads test application functionality ensure that all pages, routes, and assets load correctly troubleshoot if necessary utilize browser developer tools and back4app deployment logs for any required debugging step 8 – final thoughts and future directions congratulations on building your redwoodjs based crud application using back4app! you have successfully set up a project titled basic crud app redwood , created detailed database collections for items and users, and integrated a clean frontend with robust data management and security what’s next? refine your frontend enhance your redwoodjs application with features like detailed item pages, search functionalities, and real time updates incorporate advanced features consider adding serverless functions, third party integrations, or more complex access controls explore additional resources dive deeper into the back4app documentation https //www back4app com/docs and redwoodjs guides https //redwoodjs com/docs for further improvements by following this tutorial, you now possess the know how to develop a scalable crud backend and seamlessly integrate it with a modern redwoodjs frontend using back4app enjoy your coding journey!