Quickstarters

How to Create a Basic CRUD App with Preact?

48min

Overview

In this guide, you'll develop a CRUD (create, read, update, and delete) application using Preact.

We will walk you through building a system that handles basic data operations, from initializing a Back4app project named Basic-CRUD-App-Preact to managing data effortlessly. This project provides a robust backend and a sleek, minimal frontend built with Preact.

We'll begin by setting up a new project on Back4app, then craft an efficient database schema—either manually or with the help of the Back4app AI Assistant. This design will support all essential CRUD functionalities.

Next, we introduce the Back4app Admin App, a drag-and-drop interface that simplifies data management. You can quickly add, modify, or remove records using this tool.

Finally, you will connect your Preact frontend to Back4app using REST/GraphQL , ensuring your backend is secure with advanced access rules.

By the end of this tutorial, you'll have a production-ready web application featuring user authentication and robust data management—all built with Preact and powered by Back4app.

Main Insights

  • Master building CRUD applications that efficiently manage data using a dependable backend.
  • Learn to design a scalable data model and integrate it with a Preact frontend for an optimal user experience.
  • Discover how the drag-and-drop Back4app Admin App simplifies create, read, update, and delete operations.
  • Understand deployment strategies, including containerization with Docker, to quickly launch your application.


Prerequisites

Before starting, please make sure you have:

  • A Back4app account and an initialized project. For guidance, check out Getting Started with Back4app.
  • A Preact development environment. Use tools like Preact CLI or equivalent, ensuring Node.js (v14 or later) is installed.
  • Basic knowledge of JavaScript, Preact, and REST APIs. Refer to the Preact documentation if needed.


Step 1 – Setting Up Your Project

Initializing a Back4app Project

  1. Sign in to your Back4app account.
  2. Select the “New App” option on your dashboard.
  3. Name your project: Basic-CRUD-App-Preact and follow the setup prompts.
Create New Project
Create New Project


After creation, your project will be visible on your Back4app dashboard, laying the groundwork for backend configuration.



Step 2 – Crafting Your Database Schema

Structuring Your Data Model

For our CRUD app, you'll need several collections. Below are examples that outline the collections and fields essential for supporting CRUD operations.

1. Items Collection

This collection keeps details about each item.

Field

Data Type

Purpose

_id

ObjectId

Auto-generated primary key.

title

String

The name of the item.

description

String

A short description of the item.

created_at

Date

The creation timestamp.

updated_at

Date

Last update timestamp.

2. Users Collection

This holds user credentials and profile details.

Field

Data Type

Purpose

_id

ObjectId

Auto-generated primary key.

username

String

Unique username.

email

String

Unique email address.

password_hash

String

Hashed password for authentication.

created_at

Date

Account creation timestamp.

updated_at

Date

Last account update timestamp.

You can create these collections manually via the Back4app dashboard by adding a new class for each collection and defining the appropriate fields.

Create New Class
Create New Class


You can add columns by selecting a data type, naming the column, and setting default values and required flags.

Create Column
Create Column


Using the Back4app AI Assistant for Schema Creation

The AI Assistant available in Back4app can auto-generate your schema based on a prompt describing your collections and fields. This feature expedites the setup and ensures consistency.

Steps to Use the AI Assistant:

  1. Open the AI Assistant from your Back4app dashboard.
  2. Input a detailed description of your data model.
  3. Review the auto-generated collections and field definitions, then apply them to your project.

Sample Prompt:

Text


This approach saves time and guarantees a well-structured schema.



Step 3 – Activating the Admin App & Managing CRUD Operations

Introducing the Admin App

The Back4app Admin App provides a no-code interface to manage your backend data effortlessly. Its drag-and-drop functionality enables you to perform CRUD operations without hassle.

Activating the Admin App

  1. Go to the “More” menu in your Back4app dashboard.
  2. Select “Admin App” and click “Enable Admin App.”
  3. Set up your first admin user, which will also configure roles like B4aAdminUser and system collections.
Enable Admin App
Enable Admin App


After activation, log in to the Admin App to manage your data.

Admin App Dashboard
Admin App Dashboard


Performing CRUD Actions with the Admin App

Within the interface you can:

  • Add Records: Use the “Add Record” option in any collection (e.g., Items) to create new entries.
  • View and Edit Records: Select a record to see its details or modify its fields.
  • Remove Records: Delete entries that are no longer needed.

This intuitive tool enhances user experience by streamlining data management.



Step 4 – Connecting Your Preact Frontend to Back4app

With the backend in place, it’s time to integrate your Preact frontend.

Option A: Utilizing the Parse SDK

  1. Install the Parse SDK:
Bash

  1. Set up Parse in your Preact project. Create a file (e.g., src/parseConfig.js):
JS

  1. Integrate Parse in a Preact component. For instance, build a component to fetch and display items:
JS


Option B: Leveraging REST or GraphQL

If the Parse SDK is not viable, use REST or GraphQL for CRUD operations. For instance, to fetch items via REST:

JS


Integrate these API calls within your Preact components as required.



Step 5 – Enhancing Backend Security

Using Access Control Lists (ACLs)

Secure your data by assigning ACLs to your objects. For instance, to create a private item:

JS


Class-Level Permissions (CLPs)

Within the Back4app dashboard, adjust CLPs for each collection to restrict access to authenticated or authorized users only.



Step 6 – Implementing User Authentication

Configuring User Accounts

Back4app utilizes Parse’s User class for managing authentication. In your Preact application, manage user sign-up and login as follows:

JS


A similar method can be used for login and managing sessions. Additional features like social authentication and password recovery can be set up via the Back4app dashboard.



Step 7 – Launching Your Preact Frontend with Web Deployment

Back4app’s Web Deployment feature lets you host your Preact application by linking a GitHub repository. In this part, you'll prepare your production build, commit your code, integrate with Web Deployment, and deploy your site.

7.1 Build Your Production Files

  1. Open your project directory in a terminal.
  2. Run the build command:
Bash


This generates a build folder with optimized static assets (HTML, JS, CSS, images). 3. Confirm that the build folder includes an index.html file and necessary assets.



7.2 Organize and Commit Your Source Code

Your repository should contain the complete Preact frontend source code. A sample directory structure:

Text


Example Files

src/parseConfig.js

JS


src/App.js

JS


Committing Your Code to GitHub

  1. Initialize a Git repository (if not already done):
Bash

  1. Stage your source files:
Bash

  1. Commit your changes:
Bash

  1. Create a new GitHub repository, for example, Basic-CRUD-App-Preact-Frontend.
  2. Push your code:
Bash



7.3 Connecting Your Repository with Web Deployment

  1. Go to the Web Deployment section in your Back4app dashboard, open your project (Basic-CRUD-App-Preact), and select Web Deployment.
  2. Link your GitHub account as prompted, so Back4app can access your repository.
  3. Select the repository (e.g., Basic-CRUD-App-Preact-Frontend) and choose the appropriate branch (e.g., main).


7.4 Deployment Configuration

Specify additional settings:

  • Build Command: If a pre-built build folder is absent, set the build command (e.g., npm run build). Back4app will run this command during deployment.
  • Output Directory: Define the output folder as build so that Back4app identifies the static files.
  • Environment Variables: Add any necessary API keys or settings required by your application.


7.5 Containerizing Your Preact Application with Docker

If you prefer Docker, include a Dockerfile in your repository similar to:

Dockerfile


Select the Docker deployment option in your Back4app Web Deployment settings.



7.6 Deploying Your Application

  1. Click the Deploy button once configuration is complete.
  2. Monitor the build process as Back4app fetches your code, builds, and deploys your application.
  3. Once finished, Back4app will provide a URL where your application is hosted.


7.7 Verifying Your Deployment

  1. Visit the provided URL to see your live site.
  2. Test all functionalities to ensure every feature, including routes and assets, loads correctly.
  3. If issues occur, review browser console logs and deployment logs in Back4app.


Step 8 – Wrap-Up and Future Directions

Great work! You've successfully developed a full CRUD application using Preact and Back4app.

You set up a project named Basic-CRUD-App-Preact, defined robust database collections, and used the Admin App for efficient data management.

You then connected your Preact frontend to your backend and implemented strict access controls.

What’s Next?

  • Enhance your Preact app with additional features such as detailed views, search capabilities, and real-time updates.
  • Incorporate more advanced backend logic like Cloud Functions or third-party API integrations.
  • Consult the Back4app documentation and other resources for deeper insights into optimization and custom configurations.

By following this tutorial, you’ve gained the skills to build a scalable, secure CRUD backend for your Preact applications using Back4app. Enjoy coding and exploring new features!