Quickstarters

How to Develop a CRUD Application with Deno? A Comprehensive Guide

42min

Overview

This guide demonstrates how to create a simple CRUD (create, read, update, delete) application using Deno.

We will leverage Back4app as our backend management platform, configuring it to operate as a reliable database solution and we will employ the API approach to interact with our backend services.

In this tutorial, you will:

  • Set up a Back4app project called Basic-CRUD-App-Deno.
  • Design and configure your database schema with collections and fields tailored for CRUD operations.
  • Utilize the Back4app Admin App to manage your collections via an intuitive, drag-and-drop interface.
  • Connect your Deno frontend with Back4app using REST/GraphQL calls.
  • Secure your backend with robust access control measures.

By the end of this guide, you will have built a production-ready web application that supports essential data operations and user authentication.

Essential Insights

  • Master CRUD functionalities to efficiently handle data.
  • Learn how to design a scalable backend integrated with a Deno-based frontend.
  • Use the Back4app Admin App for seamless data management.
  • Discover deployment strategies, including containerization with Docker.


Prerequisites

Before starting, please ensure you have:

  • A Back4app account and an initialized project. Refer to Getting Started with Back4app if needed.
  • A Deno development setup. Make sure Deno is installed and updated (version 1.10+ is recommended).
  • Familiarity with JavaScript/TypeScript, Deno, and REST API concepts. Consult the Deno Manual for further reading.


Step 1 – Initializing Your Project

Launching a New Back4app Project

  1. Log into your Back4app account.
  2. Click the “New App” button on your dashboard.
  3. Name your project: Basic-CRUD-App-Deno and follow the on-screen instructions.
Create New Project
Create New Project


After the project is created, it will be visible on your dashboard, laying the groundwork for your backend configuration.



Step 2 – Crafting the Database Schema

Structuring Your Data Model

For this CRUD application, you will define multiple collections. Below are sample collections with suggested fields to facilitate basic operations.

1. Collection: Items

This collection holds details for each item.

Field

Type

Purpose

_id

ObjectId

Auto-generated primary key.

title

String

Name of the item.

description

String

Brief summary of the item.

created_at

Date

Timestamp for when the item was added.

updated_at

Date

Timestamp for the latest update.

2. Collection: Users

This collection stores user profiles and authentication data.

Field

Type

Purpose

_id

ObjectId

Auto-generated primary key.

username

String

Unique identifier for the user.

email

String

User's unique email address.

password_hash

String

Securely hashed password.

created_at

Date

Account creation timestamp.

updated_at

Date

Last updated timestamp.

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

Create New Class
Create New Class


You can also add fields by selecting their type, naming them, setting defaults, and marking them as required.

Create Column
Create Column



Employing the Back4app AI Agent for Schema Creation

The Back4app AI Agent simplifies schema generation by allowing you to input a descriptive prompt, which then automatically creates the required collections and fields.

Steps to Use the AI Agent:

  1. Access the AI Agent: Open your Back4app dashboard and locate the AI Agent option.
  2. Input Your Data Model Description: Provide a detailed prompt listing your collections and fields.
  3. Review and Apply: Inspect the generated schema suggestions and implement them in your project.

Sample Prompt:

Text


This approach ensures consistency and efficiency in setting up your backend schema.



Step 3 – Activating the Admin Interface and CRUD Functions

Overview of the Admin Interface

The Back4app Admin App offers a no-code interface to handle your backend data. Its intuitive drag-and-drop design facilitates easy management of CRUD operations.

Activating the Admin App

  1. Go to the “More” menu on your Back4app dashboard.
  2. Select “Admin App” and click on “Enable Admin App.”
  3. Set up your admin credentials by creating an initial admin user, which also configures roles (e.g., B4aAdminUser) and system collections.
Enable Admin App
Enable Admin App


Once activated, log into the Admin App to manage your collections effortlessly.

Admin App Dashboard
Admin App Dashboard


Managing CRUD Operations via the Admin App

In the Admin App, you can:

  • Add Records: Click “Add Record” in any collection (e.g., Items) to create new entries.
  • View/Edit Records: Select records to inspect or modify details.
  • Remove Records: Delete entries that are no longer required.

This straightforward tool streamlines the process of performing basic data operations.



Step 4 – Connecting Deno with Back4app

With your backend configured, it’s time to link your Deno frontend using REST or GraphQL APIs.

Option: Using REST/GraphQL API Calls

We will rely on API calls to interact with Back4app.

Example: Fetching Data via REST in Deno

Create a module (e.g., fetchItems.ts) with the following code:

TypeScript


Integrate such API calls into your Deno application as needed.



Step 5 – Strengthening Your Backend Security

Implementing Access Control Lists (ACLs)

Protect your data by assigning ACLs to each object. For instance, to ensure an item is private:

TypeScript


Configuring Class-Level Permissions (CLPs)

Within the Back4app dashboard, adjust CLPs for each collection to define default access policies, ensuring that only authenticated or authorized users can access or modify sensitive data.



Step 6 – Managing User Authentication

Creating User Accounts

Back4app uses the Parse User class for authentication. In your Deno application, manage user registration and login via REST API calls.

Example: User Registration in Deno

TypeScript


This approach can similarly be applied to login and session management.



Step 7 – Deploying Your Deno Frontend with Web Deployment

Back4app’s Web Deployment feature allows you to host your Deno-based frontend by deploying code directly from a GitHub repository.

7.1 – Building Your Production Version

  1. Open your project directory in the terminal.
  2. Run the build command: For example, if using a bundler like esbuild:

    Bash
    
  3. Confirm the Build Output: Ensure the output directory (e.g., dist) contains the necessary static files like index.html, bundled JavaScript, CSS, and images.


7.2 – Organizing and Committing Your Code

Your repository should include all source files for your Deno frontend. An example structure might be:

Text


Sample File: deps.ts

TypeScript


Sample File: src/app.ts

TypeScript


Committing Your Code to GitHub

  1. Initialize Git:

    Bash
    
  2. Stage all files:

    Bash
    
  3. Commit your changes:

    Bash
    
  4. Push to GitHub: Create a repository (e.g., Basic-CRUD-App-Deno) and push:

    Bash
    


7.3 – Linking GitHub with Back4app Web Deployment

  1. Log into Back4app and navigate to your project.
  2. Click on the Web Deployment feature.
  3. Connect your GitHub account following the prompts.
  4. Select your repository and branch (e.g., main) that holds your Deno code.


7.4 – Configuring Deployment Settings

Specify:

  • Build Command: If no pre-built dist folder exists, set the command (e.g., deno run --allow-read --allow-write build_script.ts).
  • Output Directory: Define the output folder, such as dist.
  • Environment Variables: Add any necessary environment variables (e.g., API keys).


7.5 – Dockerizing Your Deno Application

To deploy with Docker, include a Dockerfile in your project:

Dockerfile


Choose the Docker deployment option in Back4app if you prefer containerization.



7.6 – Launching Your Application

  1. Initiate Deployment: Click the Deploy button in your Back4app dashboard.
  2. Monitor the Process: Back4app will fetch your code, execute build steps, and deploy your app.
  3. Access Your Site: After deployment, a URL will be provided where your Deno application is live.


7.7 – Confirming Deployment Success

  1. Visit the Provided URL: Open the URL in your browser.
  2. Test Your Application: Ensure that all routes, static assets, and API endpoints function correctly.
  3. Troubleshoot: If issues appear, consult the deployment logs and verify your configuration.


Step 8 – Wrapping Up and Future Directions

Congratulations! You have successfully built a basic CRUD application using Deno with Back4app as your backend.

You set up the project Basic-CRUD-App-Deno, structured your database, managed data through the Admin App, connected your Deno frontend via API calls, and secured your data with ACLs and CLPs.

Future Enhancements:

  • Expand the Frontend: Add features like detailed views, search filters, or real-time updates.
  • Advanced Backend Logic: Integrate Cloud Functions or additional API integrations.
  • Deepen Security: Explore role-based access and further authentication measures.

For more details, check the Back4app documentation and explore additional resources.

Happy coding and enjoy building your scalable Deno CRUD application!