Quickstarters
CRUD Samples
Building a CRUD Application with Qwik: A Comprehensive Tutorial
41 min
overview in this guide, you'll learn to craft a fully functional crud (create, read, update, delete) application using qwik we’ll demonstrate essential operations for managing data while connecting your app to back4app initially, you'll set up a back4app project titled basic crud app qwik to serve as the backend for data management next, you'll configure a scalable database by defining collections and fields—either manually or with the assistance of the back4app ai agent this ensures that your data model is optimized for handling crud tasks efficiently you'll then utilize the back4app admin app, a user friendly drag and drop interface, to manage your collections seamlessly this tool simplifies data operations by offering an intuitive way to create, update, and delete records finally, you’ll integrate your qwik frontend with back4app using rest or graphql apis, along with implementing robust security controls by the end of this tutorial, your production ready application will not only handle crud operations but also support user authentication and secure data access essential insights master the art of developing crud applications that effectively handle data using a reliable backend discover methods to design an adaptable backend and merge it with a qwik frontend for enhanced user interactivity explore the advantages of a drag and drop admin interface (back4app admin app) to streamline crud functions learn about deployment strategies, including docker containerization, to swiftly launch your web application prerequisites before starting, please ensure you have a back4app account with a new project setup visit getting started with back4app https //www back4app com/docs/get started/new parse app for assistance a qwik development environment set up your project using the qwik documentation https //qwik builder io/docs/getting started/ ensure you have node js (version 14 or later) installed a basic understanding of javascript, qwik, and rest apis consult the qwik docs https //qwik builder io/docs/ if you need a refresher step 1 – project initialization establishing a new back4app project sign in to your back4app account click on “new app” in your dashboard input the project name basic crud app qwik and follow the instructions to create your project create new project your new project will then appear on your dashboard, forming the backend foundation for your application step 2 – designing your database schema crafting your data model for this crud app, you’ll define several collections below are examples outlining the necessary fields and types to help you create an effective database schema for handling crud operations 1\ items collection this collection holds details for each item field data type description id objectid auto generated primary key title string name of the item description string brief details about the item created at date timestamp when the item was created updated at date timestamp of the last update 2\ users collection this collection stores user details and authentication info field data type description id objectid auto generated primary key username string unique identifier for the user email string user’s unique email address password hash string encrypted password for login created at date account creation timestamp updated at date last update timestamp for the user account you can manually add these collections through the back4app dashboard by creating new classes and configuring each column as needed create new class easily add fields by selecting the type, providing a name, and configuring default values and constraints create column leveraging the back4app ai agent for schema creation the back4app ai agent can automatically generate your schema based on a descriptive prompt this feature accelerates project setup and ensures consistency how to operate the ai agent access the ai agent locate it in your back4app dashboard or within your project settings detail your data model provide a prompt that describes the collections and fields you require review and implement evaluate the generated schema suggestions and apply them to your project sample prompt create the following collections in my back4app project 1\) collection items \ fields \ id objectid (auto generated) \ title string \ description string \ created at date (auto generated) \ updated at date (auto updated) 2\) collection users \ fields \ id objectid (auto generated) \ username string (unique) \ email string (unique) \ password hash string \ created at date (auto generated) \ updated at date (auto updated) this approach saves time and guarantees a well structured schema tailored for your application step 3 – activating the admin app & managing crud operations introduction to the admin app the back4app admin app provides a no code interface for managing backend data its intuitive drag and drop functionality lets you easily perform crud operations how to enable the admin app access the “more” menu from your back4app dashboard select “admin app” and then click “enable admin app ” set up your admin credentials by creating an admin user, which will automatically establish roles and system collections enable admin app after activation, sign in to the admin app to manage your data effortlessly admin app dashboard performing crud actions via the admin app add records use the “add record” button within any collection (e g , items) to create new entries view/edit records click on any record to inspect its details or make updates remove records utilize the delete function to eliminate obsolete entries this tool streamlines data operations, ensuring a smooth user experience step 4 – connecting qwik with back4app now that your backend is configured, it's time to integrate your qwik frontend with back4app option using rest or graphql apis we'll employ rest api calls to interact with back4app example fetching items via rest in qwik create a qwik component (e g , src/components/itemslist tsx ) that retrieves and displays items // src/components/itemslist tsx import { component$, usetask$ } from '@builder io/qwik'; export const itemslist = component$(() => { const items = \[]; usetask$(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(); items push( data results); } catch (error) { console error('error retrieving items ', error); } }); return ( \<div> \<h2>items\</h2> \<ul> {items map((item any) => ( \<li key={item objectid}> {item title} — {item description} \</li> ))} \</ul> \</div> ); }); export default itemslist; this component uses the fetch api to call back4app’s rest endpoint, retrieving the list of items for other crud operations (create, update, delete), integrate similar rest calls into your qwik components step 5 – securing your backend implementing access control lists (acls) enhance your data security by setting acls on your objects for instance, to restrict access to a specific item async function createprivateitem(itemdata { title string; description string }, ownerid string) { try { const response = await fetch('https //parseapi back4app com/classes/items', { method 'post', headers { 'content type' 'application/json', 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key' }, body json stringify({ title itemdata title, description itemdata description, acl { \[ownerid] { read true, write true }, ' ' { read false, write false } } }) }); const result = await response json(); console log('created private item ', result); } catch (error) { console error('error creating private item ', error); } } configuring class level permissions (clps) within the back4app dashboard, adjust clps for each collection to enforce default access policies, ensuring only authorized users can interact with sensitive data step 6 – user authentication establishing user accounts back4app utilizes parse’s user class for managing authentication in your qwik app, implement user sign up and login as shown below example sign up component in qwik // src/components/auth tsx import { component$, usestore } from '@builder io/qwik'; export const signup = component$(() => { const store = usestore({ username '', password '', email '' }); const handlesignup = async (e event) => { e preventdefault(); try { const response = await fetch('https //parseapi back4app com/users', { method 'post', headers { 'content type' 'application/json', 'x parse application id' 'your application id', 'x parse rest api key' 'your rest api key' }, body json stringify({ username store username, password store password, email store email }) }); const data = await response json(); alert('user successfully registered!'); } catch (error any) { alert('registration error ' + error message); } }; return ( \<form onsubmit$={handlesignup}> \<input type="text" placeholder="username" value={store username} oninput$={(e) => (store username = (e target as htmlinputelement) value)} /> \<input type="password" placeholder="password" value={store password} oninput$={(e) => (store password = (e target as htmlinputelement) value)} /> \<input type="email" placeholder="email" value={store email} oninput$={(e) => (store email = (e target as htmlinputelement) value)} /> \<button type="submit">register\</button> \</form> ); }); a similar approach can be applied to login and session management additional options such as social authentication, email verification, and password recovery can be configured via back4app step 7 – deploying your qwik frontend via web deployment back4app’s web deployment service enables seamless hosting of your qwik frontend by linking your github repository follow these steps to deploy your site 7 1 create your production build open your project folder in the terminal run the build command npm run build this generates a build folder containing optimized static assets confirm the build ensure the build folder has an index html file along with associated asset directories 7 2 organize and commit your code to github your repository should include the full source for your qwik frontend a typical structure might be basic crud app qwik frontend/ \| node modules/ \| public/ \| └── index html \| src/ \| ├── root tsx \| ├── entry tsx \| └── components/ \| ├── itemslist tsx \| └── auth tsx \| package json \| readme md example src/root tsx // src/root tsx import { component$ } from '@builder io/qwik'; import itemslist from ' /components/itemslist'; export default component$(() => { return ( \<div style={{ padding '2rem' }}> \<h1>crud application\</h1> \<itemslist /> \</div> ); }); committing your code initialize git (if not already done) git init stage your files git add commit your changes git commit m "initial commit of qwik frontend code" create a github repository for instance, name it basic crud app qwik frontend push your code to github git remote add origin https //github com/your username/basic crud app qwik frontend git git push u origin main 7 3 connecting your repository to web deployment access web deployment log into your back4app dashboard, select your project (basic crud app qwik), and navigate to the web deployment section link to github follow on screen prompts to connect your github account select the repository and branch choose your repository (e g , basic crud app qwik frontend ) and the relevant branch (e g , main ) 7 4 deployment configuration specify additional settings build command if a pre built build folder isn’t included, set the command (e g , npm run build ) output directory define the output directory as build environment variables insert any necessary variables like api keys 7 5 containerizing your qwik app with docker (optional) if you opt for docker, include a dockerfile similar to \# use node for building the app from node 16 alpine as build workdir /app copy package json / run npm install copy run npm run build \# serve the built app with nginx from nginx\ stable alpine copy from=build /app/build /usr/share/nginx/html expose 80 cmd \["nginx", " g", "daemon off;"] then, select the docker option in your web deployment settings 7 6 deploying your application click the deploy button once configuration is complete, hit deploy watch the build back4app will pull your code, build it, and deploy the container receive your url after deployment, a url will be provided where your app is hosted 7 7 verifying your deployment visit the url open the provided link in your browser test the functionality check that all routes and assets load correctly debug if necessary use browser developer tools and back4app logs to troubleshoot issues step 8 – wrapping up and future enhancements well done! you have successfully built a full fledged crud app using qwik and back4app you established the project basic crud app qwik , crafted a detailed database schema for items and users, and managed data with the admin app moreover, you connected your qwik frontend to the backend and applied strong security measures next steps expand your frontend enhance your qwik app with features like detailed item views, search functionalities, or real time updates add more functionality consider integrating advanced backend logic, third party apis, or role based access control explore additional resources refer to the back4app documentation https //www back4app com/docs and further qwik tutorials for in depth knowledge on performance improvements and custom integrations by following this tutorial, you now possess the know how to create