Quickstarters

Building a Basic CRUD Application with Mithril.js: A Detailed Tutorial

42min

Overview

This guide walks you through creating a CRUD (create, read, update, and delete) application using Mithril.js.

Throughout this tutorial, you'll learn how to configure a Back4app project titled Basic-CRUD-App-MithrilJS to serve as a powerful backend for your web app.

We’ll design an efficient data schema, manage it via Back4app’s admin panel, and integrate your Mithril.js frontend using REST API calls.

What You Will Learn

  • How to set up a CRUD system that reliably handles data using a robust backend.
  • Strategies to build a scalable database design and link it to a Mithril.js application.
  • Utilizing Back4app’s intuitive drag-and-drop admin interface to perform CRUD actions.
  • Deployment methods, including Docker containerization, to get your application live quickly.


Requirements

Before you get started, please confirm you have:

  • A Back4app account and an active project. For assistance, refer to Getting Started with Back4app.
  • A Mithril.js development environment. You can include Mithril via npm or a CDN. Ensure you have Node.js (v14+) installed.
  • Fundamental knowledge of JavaScript, Mithril.js, and RESTful APIs. For additional guidance, check out the Mithril.js documentation.


Step 1 – Initializing Your Project

Setting Up a New Back4app Project

  1. Sign in to your Back4app account.
  2. Click the New App button from your dashboard.
  3. Provide the project name: Basic-CRUD-App-MithrilJS and follow the guided steps.
Create New Project
Create New Project


Once set up, your project will be listed on your dashboard, serving as the backend hub for your application.



Step 2 – Crafting Your Database Schema

Constructing the Data Model

For this CRUD app, you’ll design a couple of key collections. Below is an outline for creating collections with their respective fields to effectively handle CRUD operations.

1. Items Collection

This collection holds the details for each item.

Field

Type

Purpose

_id

ObjectId

Auto-generated unique identifier.

title

String

Name or title of the item.

description

String

Brief summary about the item.

created_at

Date

Timestamp of creation.

updated_at

Date

Timestamp for the latest update.

2. Users Collection

This collection manages user profiles and authentication details.

Field

Type

Purpose

_id

ObjectId

Auto-generated unique identifier.

username

String

User’s unique name.

email

String

User’s unique email address.

password_hash

String

Hashed password for secure authentication.

created_at

Date

Timestamp when the user was registered.

updated_at

Date

Timestamp for any modifications.

You can manually add these collections and fields via the Back4app dashboard by creating new classes and defining each column accordingly.

Create New Class
Create New Class


To add fields, simply select the appropriate data type, provide a name, set defaults, and mark if required.

Create Column
Create Column



Leveraging Back4app’s AI Agent for Schema Automation

The integrated AI Agent on Back4app streamlines schema creation by auto-generating your collections based on a provided prompt.

How to Utilize the AI Agent:

  1. Access the AI Agent from your project dashboard.
  2. Describe your desired schema by detailing the collections and fields.
  3. Review the generated schema and apply the changes.

Sample Prompt:

Text


This tool helps ensure that your database design is both consistent and optimal for handling CRUD operations.



Step 3 – Activating the Admin Interface & CRUD Functionalities

Overview of the Admin Interface

The Back4app Admin Interface is a no-code solution designed to help you manage your backend data effortlessly. Its drag-and-drop design makes performing CRUD operations a breeze.

How to Enable the Admin Interface

  1. Head to the More section in your Back4app dashboard.
  2. Click on Admin App then select Enable Admin App.
  3. Set up your administrator credentials to create your first admin user. This process also configures roles (such as B4aAdminUser) and system collections.
Enable Admin App
Enable Admin App


After activation, log into the Admin Interface to manage your collections.

Admin App Dashboard
Admin App Dashboard


Performing CRUD Operations via the Admin Interface

  • Add New Records: Use the “Add Record” option in any collection (for instance, Items) to create entries.
  • View/Modify Records: Click on a record to inspect details or edit its fields.
  • Remove Records: Utilize the delete function to clear obsolete entries.

This intuitive interface enhances user experience by simplifying routine CRUD actions.



Step 4 – Linking Mithril.js with Back4app

With your backend ready and managed through the Admin Interface, it’s time to connect your Mithril.js frontend to Back4app using REST API calls.

Using REST API Calls with Mithril.js

We will rely on REST API calls to interact with your Back4app backend.

Example: Fetching and Displaying Items

Create a Mithril component that retrieves items from the Items collection:

JS


This component employs Mithril’s lifecycle method oninit to load data when the component is initialized.

Example: Adding a New Item

You can integrate a form to send a POST request to add new items:

JS


You can similarly implement update and delete operations using corresponding HTTP methods (PUT/PATCH for updates and DELETE for removals).



Step 5 – Enhancing Backend Security

Implementing Access Controls

Secure your data by applying Access Control Lists (ACLs) to each object. For instance, to create an item that only the owner can modify:

JS


Setting Class-Level Permissions (CLPs)

Within your Back4app dashboard, adjust CLPs for each collection to define default security policies. This ensures that only authorized users have access to sensitive information.



Step 6 – User Authentication Setup

Configuring User Accounts

Back4app uses the Parse User class to manage authentication. With Mithril.js, you can manage registration and login through REST API calls. Below is an example for user sign-up:

JS


You can adopt a similar method for logging in and managing sessions.



Step 7 – Deploying Your Mithril.js Frontend

Back4app’s Web Deployment feature lets you host your Mithril.js application seamlessly by linking your GitHub repository.

7.1 Building the Production Version

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


This will generate a build folder containing optimized static files.

7.2 Organizing and Committing Your Code

Your repository should have a structure similar to:

Text


Example: src/index.js

JS


Committing and Pushing to GitHub

  1. Initialize Git (if not already):
Bash

  1. Add your files:
Bash

  1. Commit the changes:
Bash

  1. Create a repository on GitHub (e.g., Basic-CRUD-App-MithrilJS-Frontend) and push your code:
Bash


7.3 Linking GitHub with Web Deployment

  1. Log into your Back4app dashboard, navigate to your project (Basic-CRUD-App-MithrilJS), and select Web Deployment.
  2. Connect your GitHub account following the on-screen prompts.
  3. Choose your repository and branch (e.g., main) that contains your Mithril.js source code.

7.4 Setting Up Deployment Configurations

Configure:

  • Build Command: Specify npm run build if the build folder isn’t present.
  • Output Directory: Set this to build to point to your static assets.
  • Environment Variables: Add any necessary API keys or settings.

7.5 Dockerizing Your Application (Optional)

If you prefer containerization, include a Dockerfile:

Dockerfile


7.6 Deploying Your Application

  1. Hit the Deploy button in your Back4app Web Deployment section.
  2. Monitor the build process as Back4app pulls, builds, and deploys your application.
  3. Once complete, you’ll receive a URL for your live Mithril.js app.

7.7 Verifying the Deployment

  1. Open the provided URL in your browser.
  2. Confirm that your application loads, navigations work, and assets are served properly.
  3. Use browser dev tools for troubleshooting if necessary.


Step 8 – Wrapping Up and Future Directions

Well done! You’ve successfully built a basic CRUD app with Mithril.js integrated with Back4app.

You set up a project called Basic-CRUD-App-MithrilJS, defined your database schema for Items and Users, and managed your backend via the Admin Interface.

Furthermore, you connected your Mithril.js frontend using REST API calls and applied security measures to protect your data.

What’s Next?

  • Improve the Frontend: Add features like detailed views, search capabilities, or live updates.
  • Extend Backend Functions: Explore cloud functions, additional API integrations, or role-based permissions.
  • Learn More: Dive deeper into the Back4app documentation and Mithril.js guides for advanced enhancements.

Happy coding and best of luck with your project!