Quickstarters
CRUD Samples
How to Build a Basic CRUD App with Ractive.js: A Step-by-Step Guide
39 min
overview in this guide, you will learn how to construct a fully functional crud (create, read, update, delete) application using ractive js this tutorial covers the entire workflow from setting up your back4app project to designing your database schema, and finally integrating a ractive js frontend with your backend the project, dubbed basic crud app ractive , will serve as a robust data management solution for your web application you'll start by initializing a back4app project, configuring collections and fields either manually or with the help of the back4app ai agent next, you'll use the intuitive back4app admin app for straightforward data handling, followed by integrating your ractive js interface with back4app via rest api calls this guide also explains how to secure your backend using advanced access control methods by the end, you'll have a production grade crud app that not only performs essential operations but also supports user authentication and secure data management essential points master building crud applications that adeptly manage data with a reliable backend understand how to design an extensible data schema and connect it with a ractive js frontend utilize a drag and drop management interface (back4app admin app) for effortless crud actions learn deployment strategies including docker containerization for rapid web app launches prerequisites before starting, ensure you have an active back4app account with a new project created refer to getting started with back4app https //www back4app com/docs/get started/new parse app for guidance a ractive js development setup set up your project using a boilerplate or your preferred configuration, ensuring you have node js (v14 or later) installed basic knowledge of javascript, ractive js, and rest apis review the ractive js documentation https //ractive js org/ as needed step 1 – project initialization starting a new back4app project log in to your back4app account select the “new app” option on your dashboard name your project basic crud app ractive and follow the on screen instructions to create it create new project after setup, your project will be visible on the dashboard, ready for further backend configuration step 2 – designing your database schema crafting your data model for this crud application, you'll define several collections to manage your data below are examples of the collections with suggested fields and data types, ensuring the backend is optimized for crud functionalities 1\ collection items this collection holds details for each item field type purpose id objectid auto generated primary identifier title string item title description string brief summary of the item created at date record creation timestamp updated at date last update timestamp 2\ collection users this collection manages user data and authentication field type purpose id objectid auto generated primary identifier username string unique identifier for the user email string user's unique email address password hash string hashed password for secure authentication created at date timestamp for account creation updated at date timestamp for last account update you can manually establish these collections via the back4app dashboard by creating new classes and adding the required columns create new class define fields by selecting the appropriate data type, naming the column, and setting default or required values create column utilizing the back4app ai agent for schema automation the back4app ai agent can automatically generate your schema based on your prompt this efficient tool simplifies project management by rapidly establishing your collections and fields how to operate the ai agent access the ai agent from your project’s dashboard input a detailed prompt describing your desired collections and fields review and apply the auto generated schema to your project sample prompt create these collections for 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 ensures your schema is consistent and tailored to your app’s needs step 3 – activating the admin app & managing crud operations about the admin interface the back4app admin app offers a no code solution to handle your backend data its drag and drop interface allows for effortless crud operations like adding, modifying, or deleting records how to enable the admin app go to the “more” menu on your back4app dashboard select “admin app” and click “enable admin app ” set up your first admin user, which also configures roles (such as b4aadminuser ) and system collections enable admin app once activated, log into the admin app to manage your collections admin app dashboard managing data via the admin app adding entries use the “add record” option in a collection (like items) to input new data viewing/editing records select any record to inspect details or update fields removing entries use the delete function to eliminate obsolete records this user friendly interface streamlines all crud functions step 4 – connecting ractive js with back4app now that your backend is ready, it’s time to connect your ractive js frontend to back4app using rest api calls with ractive js since ractive js does not have a dedicated parse sdk, you will use rest api calls to perform crud operations example fetching items create a ractive component that retrieves and displays data from your back4app items collection items html items list {{#each items}} {{title}} {{description}} edit delete {{/each}} add new item add items js import ractive from "ractive"; const api url = "https //parseapi back4app com/classes/items"; const headers = { "x parse application id" "your application id", "x parse rest api key" "your rest api key", "content type" "application/json" }; const ractive = new ractive({ target "#items container", template "#items template", data { items \[], newtitle "", newdescription "" } }); // function to load items async function loaditems() { try { const response = await fetch(api url, { headers headers }); const result = await response json(); ractive set("items", result results map(item => ({ id item objectid, title item title, description item description }))); } catch (error) { console error("error fetching items ", error); } } ractive on("additem", async function () { const title = ractive get("newtitle"); const description = ractive get("newdescription"); try { await fetch(api url, { method "post", headers headers, body json stringify({ title, description }) }); ractive set({ newtitle "", newdescription "" }); loaditems(); } catch (error) { console error("error adding item ", error); } }); // additional functions for editing and deleting would follow a similar rest approach loaditems(); you can incorporate these api interactions into your ractive js components to handle all crud operations step 5 – strengthening backend security implementing access controls enhance security by setting access control lists (acls) on your objects for instance, to make an item private async function createprivateitem(itemdata, owneruserid) { const data = { title itemdata title, description itemdata description, acl { \[owneruserid] { read true, write true }, " " { read false, write false } } }; try { const response = await fetch(api url, { method "post", headers headers, body json stringify(data) }); const result = await response json(); console log("private item created ", result); } catch (error) { console error("error creating private item ", error); } } configuring class level permissions within the back4app dashboard, adjust class level permissions (clps) for each collection to define default access policies this step ensures that only authorized users can access sensitive data step 6 – managing user authentication configuring user accounts back4app uses parse’s user class to handle authentication implement user registration and login via rest api calls in your ractive js application example user sign up async function signupuser(username, email, password) { const response = await fetch("https //parseapi back4app com/users", { method "post", headers headers, body json stringify({ username, email, password }) }); const result = await response json(); if (result objectid) { alert("user registered successfully!"); } else { alert("error during sign up " + result error); } } implement similar routines for login and session handling additional features like password recovery and email verification can be set up via the back4app dashboard step 7 – deploying your ractive js frontend back4app’s web deployment feature allows you to host your ractive js application by linking it to your github repository 7 1 building your production version open your project folder in a terminal execute the build process (this might be a custom command based on your setup, e g , using rollup or webpack) ensure that your production folder (commonly named dist or build ) contains your static files 7 2 organizing and committing your code your project repository should have a clear structure, for example basic crud app ractive/ \| node modules/ \| public/ \| └── index html \| src/ \| ├── main js \| ├── ractiveconfig js \| └── components/ \| ├── items html \| └── auth html \| package json \| readme md sample file ractiveconfig js // ractiveconfig js // this file can include global configuration for your ractive app export const api url = "https //parseapi back4app com"; export const headers = { "x parse application id" "your application id", "x parse rest api key" "your rest api key", "content type" "application/json" }; committing your code initialize a git repository git init add your files git add commit the changes git commit m "initial commit for ractive js frontend" create a github repository (e g , basic crud app ractive ) and push your code git remote add origin https //github com/your username/basic crud app ractive git git push u origin main 7 3 linking your repository to back4app web deployment log into your back4app dashboard, select your project ( basic crud app ractive ), and choose the web deployment option connect your github account as prompted select the repository and branch (typically main ) that contains your ractive js code 7 4 setting up deployment parameters configure the following build command specify your build command (for example, npm run build ) output directory set it to your production folder (e g , build or dist ) environment variables include any necessary api keys or configuration variables 7 5 containerizing with docker (optional) if you wish to deploy via docker, include a dockerfile in your project \# base build stage using node js from node 16 alpine as build \# set the working directory workdir /app \# copy package files and install dependencies copy package json / run npm install \# copy the entire app and build it copy run npm run build \# production stage using nginx from nginx\ stable alpine copy from=build /app/build /usr/share/nginx/html expose 80 cmd \["nginx", " g", "daemon off;"] 7 6 launching your application click the deploy button in back4app monitor the deployment process as back4app pulls and builds your repository once complete, back4app will provide a url for your hosted ractive js app 7 7 verifying the deployment open the provided url in your browser test all routes, ensuring every asset (css, js, images) loads correctly if issues occur, consult the deployment logs and check your build settings step 8 – wrap up and future directions great work! you’ve successfully built a complete crud application using ractive js and back4app you set up a project named basic crud app ractive , defined precise collections for items and users, and used the intuitive admin app for backend management additionally, you integrated your ractive js frontend with the backend and fortified your data security next steps enhance your ui add features like detailed views, search functions, or real time updates expand your backend integrate advanced backend logic such as cloud functions or third party api services deepen your learning explore the back4app documentation https //www back4app com/docs for more advanced configurations and optimizations with this tutorial, you now have the expertise to create a scalable crud backend for your ractive js application using back4app happy coding!