How to build an event management application in Amplication
Tags: amplification, nestjs, prisma, mysql
Published: 2023-11-02 00:46:05

In this article, we will guide you through the process of building a fully functional event management platform using Amplication. Amplication is a powerful open-source development platform designed to streamline the creation of scalable and secure Node.js applications.

But first, why choose Amplication as the foundation of your application? One obvious reason is its exceptional speed and efficiency, which can significantly enhance your application's performance and, consequently, the user experience. So, let's dive right into harnessing the capabilities of Amplication to create your event management platform.

## Prerequisites

To successfully follow this tutorial, please ensure you have the following installed on your computer:

**Required Software:**
1. Node.js
2. npm
3. Code Editor (such as Visual Studio Code)
4. Web browser or Postman
5. Docker Compose

Having these tools in place will facilitate your participation in this tutorial.

Finally, you'll need an [Amplication](https://app.amplication.com/login) account for your Amplication development environment. If you don't already have one, simply visit their [website](https://app.amplication.com/login) to create a free account.

## Create your service in Amplication

If you prefer, follow the official Amplication guide on the [page](https://docs.amplication.com/first-service/).

1. **Create Your Amplication Account**

To begin, register for an [Amplication](https://app.amplication.com/login) account. Afterward, authorize your account with GitHub.

2. **Name Your Application**

When choosing a name for your service, focus on its primary function and the problem it solves. A well-named service is easier to recognize and manage.

```markdown
backend
```

3. **Link Your GitHub Repository**

Amplication will automatically synchronize your generated service with a chosen GitHub repository. Learn more at: [sync-with-github](https://docs.amplication.com/sync-with-github).

4. **Customize Your Service**

Amplication can generate GraphQL APIs, REST APIs, and an Admin UI for your services. Choose the components you need.
In our example, we'll enable only the **REST API & Swagger** and continue.

5. **Select Your Repo Style**

Select the storage method for your service in your repository: Monorepo or Polyrepo.

In our example, we'll enable Monorepo. It's recommended if you plan to have multiple services in a single GitHub repository.

```
./apps
  - apps/my-new-service 
  - apps/my-new-service-admin
```

6. **Choose Your Database**

Amplication natively supports PostgreSQL, MongoDB, and MySQL. Select the one that best fits your requirements. In our example, we've chosen MySQL.

7. **Define Your Data Model**

Define your database entities and fields. You can either create them manually or utilize a template offered by Amplication. In our example, we've chosen to start from scratch.

8. **Include Authentication**

Optionally, you can incorporate the required authentication code in your service. If your service doesn't require authentication, you can choose to skip it. In our example, we prefer to include authentication, so we select the 'Include Auth Module' option.

**You're Finished!**

Congratulations! Your first Amplication service is now set up. You'll be directed to a screen where Amplication will begin generating all the required code for your service in real time.

![Amplication page- Service created successfully](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kgroeuzvvcemr2kk7pif.png)


## Creating/Modifying our entities

Once your first service is successfully created, the next steps involve adding entities, roles, and permissions to your service. **Let's now create our event entity and adjust the relationship between the user and event entities.**

1. **Add Event Entity**

Click on "Add Entity," enter the name "Event," and then click "Create Entity."

![Amplication page - Add new entity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7zdf0jila1y1n6z9emh7.png)

2. **Add Entity Fields**

After creating the event entity, add the fields that will represent the event. In our example, we've included the following fields:

- category (String)
- description (String)
- endDate (DateTime)
- startDate (DateTime)
- imageUrl (String, optional)
- locationPoint (String, optional)
- title (String)
- venue (String)
- User (String)

In the User field, navigate to Data Type, select Relation to Entity, then in Related Entity, choose the User entity, and specify _'One Event can be related to one User'_. If desired, you can add a label for the Foreign Key in _'Foreign Key Field Name'_.

![Amplication page - Event entity (User field)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ebzzhpt3g0zui3juqsy.png)

After this, navigate to the User entity. You'll notice that the relationship between the User entity and the Event entity is automatically configured. Just ensure that the relationship is set to _'One User can be related to many Events'_.

![Amplication page - User entity (Event field)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8pg0jtqvbgeowckcp5bn.png)

Navigate to the "Permissions" section and modify the permissions to allow viewing, creating, and searching for users.

![Amplication page - User entity (Edit Permissions)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fbz0cljuyqyhaof6om5x.png)

Once you've completed the entity configurations, commit the changes, and proceed to **VS Code** by clicking on _'Commit changes & build'_.

## Getting started In VS Code

1. **Open the terminal and clone the repository selected in Amplication**.

```
git clone https://github.com/jrsmarcilio/event-management-system.git
```

2. **Enter the folder and change branch:**

```bash
cd event-management-system; git checkout amplication
```

3. **Go to project folder and install npm packages:**

```bash
cd apps/backend; npm i
```

4. **Create the _.env_ file and add the environment variables:**

```bash
PORT=3000

BCRYPT_SALT=10

JWT_SECRET_KEY=secret
JWT_EXPIRATION=1h

DB_PORT=3306
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=amplication
DB_NAME=amplication
DB_URL=mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:3306/${DB_NAME}
```

5. **Create the _docker-compose.dev.yml_ file and add content:**
```yml
version: "3"
services:
  db:
    image: mysql
    command: --default-authentication-plugin=mysql_native_password
    restart: always
    ports:
      - ${DB_PORT}:3306
    environment:
      MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
      MYSQL_DATABASE: ${DB_NAME}
    healthcheck:
      test:
        - CMD
        - mysqladmin
        - ping
        - -h
        - localhost
        - -u
        - ${DB_USER}
      timeout: 45s
      interval: 10s
      retries: 10

volumes:
  mysql: ~
```

6. **Create MySQL container and volume:**

```bash
docker compose -f docker-compose.dev.yml up -d
```

7. **To map your data model to the database schema, you need to use the prisma migrate CLI commands:**

```bash
npx prisma migrate dev --name init
```

7. **Generate Prisma Client with the following command:**

```bash
npx prisma generate
```

8. **Initialize the project:**

```bash
npm start #or yarn start
```

Congratulations for getting this far. You have just built your event management application fully on the Amplication 👏🏼.