Quickstarters

How to Build a CRUD App with SvelteKit? A Comprehensive Tutorial

41min

Overview

In this guide, you'll create a simple CRUD (create, read, update, delete) application using SvelteKit.

We will demonstrate how to perform standard data operations and manage your web app's backend with Back4app. The tutorial begins with setting up a Back4app project titled Basic-CRUD-App-SvelteKit, which serves as your robust backend.

You'll then design an adaptable database schema by manually creating collections and fields, or by leveraging the Back4app AI Agent. This ensures that your data model is optimized for efficient CRUD operations.

Next, you will use the Back4app Admin App—a no-code, drag-and-drop tool—to manage your collections, streamlining your data management process.

Finally, you'll integrate your SvelteKit frontend with Back4app using REST APIs. Advanced security measures, such as access controls, will also be configured to safeguard your backend.

By the conclusion of this tutorial, you'll have a production-ready web application that supports CRUD operations along with user authentication and dynamic data updates.

Main Points

  • Master the creation of CRUD applications that effectively handle data with a dependable backend.
  • Understand how to structure a scalable database and connect it with a SvelteKit frontend.
  • Learn how to utilize a visual admin tool (Back4app Admin App) to perform data operations seamlessly.
  • Explore deployment strategies, including Docker containerization, to quickly launch your application.


Prerequisites

Before you start, ensure you have:



Step 1 – Project Initialization

Setting Up a New Back4app Project

  1. Log into your Back4app dashboard.
  2. Click the “New App” button on your dashboard.
  3. Input the project name: Basic-CRUD-App-SvelteKit and follow the on-screen prompts to create your project.
Create New Project
Create New Project


After creation, your new project appears on your dashboard, providing a stable platform for backend configuration.



Step 2 – Crafting the Database Schema

Formulating Your Data Model

For our CRUD app, we need to create multiple collections. Here are examples outlining the required collections and their fields to set up your database schema for performing CRUD operations.

1. Items Collection

This collection holds details about each item.

Field

Data Type

Description

_id

ObjectId

Auto-generated primary key.

title

String

Item's title.

description

String

Brief overview of the item.

created_at

Date

Timestamp when the item was created.

updated_at

Date

Timestamp for the latest update.

2. Users Collection

This collection stores user credentials and profile details.

Field

Data Type

Description

_id

ObjectId

Auto-generated primary key.

username

String

Unique identifier for the user.

email

String

Unique user email address.

password_hash

String

Encrypted password for authentication.

created_at

Date

Account creation timestamp.

updated_at

Date

Timestamp for the latest profile update.

You can manually create these collections in the Back4app dashboard by establishing a new class for each and adding the respective columns.

Create New Class
Create New Class


Define each field by selecting the appropriate data type, naming it, setting default values, and marking it as required if necessary.

Create Column
Create Column


Automating Schema Creation with the Back4app AI Agent

The Back4app AI Agent simplifies the schema setup process directly from your dashboard. By inputting a prompt that describes your collections and fields, you can automatically generate your database structure.

How to Use the AI Agent:

  1. Access the AI Agent: Open your Back4app dashboard and navigate to the AI Agent within your project settings.
  2. Describe Your Model: Paste a detailed prompt outlining the collections and fields you require.
  3. Review and Confirm: After submission, review the generated collections and field definitions, then apply them to your project.

Sample Prompt

Text


This approach saves time and ensures your schema is both consistent and optimized.



Step 3 – Activating the Admin App & Executing CRUD Operations

Introducing the Admin App

The Back4app Admin App offers a visual interface to manage your backend effortlessly. With its drag-and-drop features, performing CRUD operations becomes straightforward.

Activating the Admin App

  1. Go to the “More” section in your Back4app dashboard.
  2. Select “Admin App” then click “Enable Admin App.”
  3. Set Up Admin Credentials: Create an initial admin user to establish roles (e.g., B4aAdminUser) and system collections.
Enable Admin App
Enable Admin App


After activation, sign into the Admin App to begin managing your data.

Admin App Dashboard
Admin App Dashboard


Managing CRUD Operations with the Admin App

Inside the Admin App you can:

  • Add Records: Click the “Add Record” button 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: Use the delete option to eliminate entries that are no longer required.

This intuitive interface streamlines your data management process significantly.



Step 4 – Connecting SvelteKit with Back4app

With your backend configured via the Admin App, it's time to link your SvelteKit frontend to Back4app.

Leveraging REST APIs in SvelteKit

You will perform CRUD operations using REST API calls.

Fetching Data Example

Create a SvelteKit endpoint or component that retrieves items from Back4app:

Text


Integrate similar API calls within your SvelteKit components for create, update, and delete operations.



Step 5 – Securing Your Backend

Configuring Access Control Lists (ACLs)

Protect your data by setting ACLs for each object. For instance, to create a private item:

JS


Setting Class-Level Permissions (CLPs)

In the Back4app dashboard, configure CLPs for each collection so that only authorized users can access sensitive data.



Step 6 – Implementing User Authentication

Managing User Accounts

Back4app employs Parse’s User class for authentication. In your SvelteKit app, set up registration and login forms that interact with Back4app via REST API calls.

Example: User Registration

Text


A similar method can be implemented for user login and session management.



Step 7 – Deploying Your SvelteKit Frontend

Back4app’s Web Deployment feature enables you to host your SvelteKit frontend by linking it to a GitHub repository. This section covers how to build your production files, push your source code, and deploy your site.

7.1 Creating a Production Build

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

    Bash
    

    This generates a build (or output) folder with optimized static assets.

  3. Verify the Build: Ensure the build folder contains an index.html and all necessary asset directories.


7.2 Structuring and Uploading Your Code

Your repository should have the complete SvelteKit source code. A typical structure might be:

Text


Sample API File: src/lib/api.js

JS


Committing to GitHub

  1. Initialize Git (if not done):

    Bash
    
  2. Add your files:

    Bash
    
  3. Commit your changes:

    Bash
    
  4. Create a GitHub repository: Name it, for example, Basic-CRUD-App-SvelteKit.
  5. Push your code:

    Bash
    


7.3 Linking Your GitHub Repo with Web Deployment

  1. Open Web Deployment: Log into Back4app, navigate to your project (Basic-CRUD-App-SvelteKit), and select the Web Deployment option.
  2. Connect to GitHub: Follow the prompts to link your GitHub account so that Back4app can access your repository.
  3. Choose Your Repository and Branch: Select your SvelteKit repo (e.g., Basic-CRUD-App-SvelteKit) and the branch containing your code (usually main).


7.4 Setting Up Your Deployment

Provide these additional settings:

  • Build Command: If there’s no pre-built folder, set the command (e.g., npm run build).
  • Output Directory: Specify the folder (typically build or output) that holds your static files.
  • Environment Variables: Add any necessary variables such as API keys.


7.5 Dockerizing Your SvelteKit Application

If Docker is your deployment choice, include a Dockerfile in your repository:

Dockerfile


Configure Web Deployment to use Docker if preferred.



7.6 Deploying Your Application

  1. Click the Deploy Button: Once configuration is complete, hit the Deploy button.
  2. Monitor Deployment: Back4app will retrieve your code, execute the build, and deploy your app in a container.
  3. Get Your URL: Upon successful deployment, Back4app provides a URL for your hosted SvelteKit app.


7.7 Validating Your Deployment

  1. Visit the URL: Open the provided URL in your browser.
  2. Test Functionality: Confirm that your app loads correctly, routes work, and all assets are properly served.
  3. Debug if Needed: Use browser developer tools to inspect errors, and review Back4app logs for troubleshooting.


Step 8 – Wrap-Up and Future Directions

Kudos on building a complete CRUD application with SvelteKit and Back4app!

You set up a project called Basic-CRUD-App-SvelteKit, crafted detailed collections for Items and Users, and managed your backend via the Admin App.

Additionally, you integrated your SvelteKit frontend through REST APIs and applied stringent access controls.

Future Enhancements:

  • Expand Your Frontend: Enhance your SvelteKit app with detailed views, search capabilities, and real-time notifications.
  • Add Advanced Features: Implement server-side logic (e.g., Cloud Functions), integrate third-party APIs, or enable role-based permissions.
  • Deepen Your Knowledge: Visit the Back4app documentation and explore more tutorials for performance improvements and custom integrations.

This tutorial has equipped you with the skills needed to create a scalable CRUD backend for your SvelteKit application using Back4app. Enjoy coding and happy building!