Quickstarters

How to Build a Basic CRUD App with Next.js?

49min

Introduction

In this guide, we’ll walk through building a complete CRUD (create, read, update, delete) application using Next.js for your frontend, paired with Back4app as your backend solution.

You'll set up a project named Basic-CRUD-App-NextJS, design a dynamic database schema, and integrate robust CRUD functionality into your Next.js app.

We will cover everything from configuring your Back4app project and designing collections to connecting your Next.js interface using the Parse SDK or REST/GraphQL methods.

By the end, you’ll have a production-ready web application with secure user authentication and efficient data management.

Key Takeaways

  • Build a full CRUD application with Next.js and Back4app.
  • Learn to design a flexible backend schema tailored to your data needs.
  • Utilize an intuitive, drag-and-drop Admin interface for data management.
  • Discover best practices for deployment, including Docker containerization and GitHub integration.


Prerequisites

Before you begin, ensure you have:

  • A Back4app account with a new project set up. For guidance, refer to Getting Started with Back4app.
  • A Next.js development environment. Use Create Next App or a similar tool. Make sure Node.js (version 14 or later) is installed.
  • A basic understanding of JavaScript, Next.js, and API integrations. Visit the Next.js documentation for an overview if needed.


Step 1 – Setting Up Your Project

Initiate a New Back4app Project

  1. Log in to your Back4app account.
  2. Click the “New App” button on your dashboard.
  3. Enter the project name: Basic-CRUD-App-NextJS and follow the prompts.
Create New Project
Create New Project


Your new project appears in the dashboard, serving as the core for your backend.

Create Your Next.js Application

Open your terminal and run:

Bash


Change to your project directory:

Bash


This command sets up a Next.js project framework ready for customization.



Step 2 – Crafting Your Database Schema

Structuring Your Data Model

For this CRUD app, you will create essential collections. Below are two primary collections with field details that enable the core functionalities.

1. Items Collection

This collection stores details about each item.

Field

Data Type

Description

_id

ObjectId

Auto-generated unique identifier.

title

String

The item’s name or title.

description

String

A brief summary of the item.

created_at

Date

Timestamp when the item is created.

updated_at

Date

Timestamp for the latest update.

2. Users Collection

This collection handles user profiles and authentication data.

Field

Data Type

Description

_id

ObjectId

Auto-generated unique identifier.

username

String

Unique username for the user.

email

String

Unique email address.

password_hash

String

Securely hashed password for user authentication.

created_at

Date

Timestamp when the account was created.

updated_at

Date

Timestamp for the last account update.

You can manually define these collections in the Back4app dashboard by creating a new class for each collection and adding the relevant columns.

Create New Class
Create New Class


Create fields by choosing the appropriate data type, naming the column, and setting defaults or requirements.

Create Column
Create Column



Using the Back4app AI Assistant for Schema Setup

The Back4app AI Assistant streamlines database schema generation. Describe your desired collections and fields, and let the assistant auto-generate the structure.

Steps to Use the AI Assistant:

  1. Open the AI Assistant: Find it in your Back4app dashboard menu.
  2. Describe Your Data Model: Paste a detailed prompt outlining your collections and field requirements.
  3. Review and Apply: Check the generated schema and implement it in your project.

Sample Prompt

Text


This method not only saves time but also ensures your schema is consistent and optimized.



Step 3 – Activating the Admin Interface & CRUD Features

Exploring the Admin Interface

The Back4app Admin Interface offers a no-code solution to manage your backend data. With its intuitive drag-and-drop system, you can easily perform CRUD operations.

Enabling the Admin Interface

  1. Navigate to the “More” section in your Back4app dashboard.
  2. Click on “Admin App” then select “Enable Admin App.”
  3. Configure your admin credentials by setting up your first admin user. This also establishes roles like B4aAdminUser and necessary system collections.
Enable Admin App
Enable Admin App


After activation, log in to the Admin Interface to begin managing your data.

Admin App Dashboard
Admin App Dashboard


Executing CRUD Operations via the Admin Interface

Within the Admin Interface you can:

  • Create Records: Use the “Add Record” button in a collection (e.g., Items) to insert new data.
  • View or Update Records: Click on a record to inspect or modify its fields.
  • Delete Records: Use the delete option to remove obsolete entries.

This streamlined interface makes data management straightforward and efficient.



Step 4 – Connecting Next.js to Back4app

With your backend set up, the next step is to connect your Next.js application.

Option A: Using the Parse SDK

  1. Install the Parse SDK:
Bash

  1. Initialize Parse in Your Next.js App: Create a file (e.g., lib/parseConfig.js):
JS

  1. Integrate Parse in a Next.js Page: For example, create a page to fetch and display items.
JS


Option B: Using REST or GraphQL

If you prefer not to use the Parse SDK, you can manage CRUD operations with REST or GraphQL. For example, to fetch items via REST:

JS


Integrate these API calls into your Next.js components as needed.



Step 5 – Securing Your Backend

Implementing Access Control Lists (ACLs)

Secure your data by assigning ACLs to objects. For instance, to create an item that only its owner can access:

JS


Configuring Class-Level Permissions (CLPs)

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



Step 6 – Implementing User Authentication

Setting Up User Accounts

Back4app leverages Parse’s built-in User class for authentication. In your Next.js app, manage registration and login as shown below:

JS


A similar approach can be used for login and session management. You can also enable additional features such as social logins, email verification, and password recovery via the Back4app dashboard.



Step 7 – Deploying Your Next.js Frontend

Back4app’s deployment options allow you to host your Next.js application effortlessly, whether through GitHub integration or Docker containerization.

7.1 Building Your Production Version

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


This generates a .next folder containing optimized static and server-rendered files.



7.2 Organizing and Uploading Your Code

Your repository should contain all source files for your Next.js app. A typical structure might be:

Text


Example: lib/parseConfig.js

JS


Example: pages/index.js

(Refer to the code snippet provided in Step 4)

Committing Your Code to GitHub

  1. Initialize a Git repository:
Bash

  1. Add all files:
Bash

  1. Commit your changes:
Bash

  1. Create a GitHub repository: For example, name it basic-crud-app-nextjs.
  2. Push your code:
Bash



7.3 Integrating with Back4app Web Deployment

  1. Access Web Deployment: Log in to your Back4app dashboard, navigate to your project, and select the Web Deployment feature.
  2. Connect your GitHub account: Follow the prompts to link your repository.
  3. Select Your Repository and Branch: Choose the repository (e.g., basic-crud-app-nextjs) and the branch (e.g., main) containing your code.


7.4 Configuring Deployment Settings

  • Build Command: If your repository does not include a pre-built .next folder, specify the command (e.g., npm run build).
  • Output Directory: Set the output directory to .next so that Back4app knows where your compiled files reside.
  • Environment Variables: Add any necessary environment variables, such as API keys.


7.5 Containerizing Your Next.js Application with Docker

If you prefer Docker for deployment, include a Dockerfile in your repository:

Dockerfile


Select the Docker deployment option in Back4app to containerize and deploy your application.



7.6 Launching Your Application

  1. Deploy Your App: Click the Deploy button in Back4app.
  2. Monitor the Build Process: Back4app will fetch your code, execute the build command, and deploy your application.
  3. Access Your Live App: Once deployment completes, a URL will be provided where your Next.js application is hosted.


7.7 Verifying Your Deployment

  1. Visit the Provided URL: Open the URL in your browser.
  2. Test Functionality: Ensure that your pages load correctly and that all CRUD operations are working.
  3. Troubleshoot if Necessary: Use browser developer tools and Back4app logs to diagnose any issues.


Step 8 – Conclusion and Next Steps

Congratulations! You have successfully built a robust CRUD application with Next.js and Back4app. You configured your project, designed tailored collections, and connected your Next.js frontend with a secure backend.

Next Steps:

  • Enhance Your Frontend: Expand your Next.js app with advanced features like detailed views, search functionality, or real-time updates.
  • Augment Your Backend: Explore Cloud Functions, third-party integrations, or additional access controls.
  • Keep Learning: Visit the Back4app documentation and Next.js resources for further optimization and customization.

By following this tutorial, you now possess the skills to create a scalable and secure CRUD application using Next.js and Back4app. Happy coding!