Setting up a primate in 5 minutes

1. Prerequisites

Before starting, ensure that you have the following installed:

  • Node.js (LTS version recommended)

  • Yarn (for package management)

  • A MySQL database (or use a hosted database like DigitalOcean)

Project Initialization

First, create a new directory for your project and initialize it using Yarn

mkdir my-api-project
cd my-api-project
yarn init -y

This will generate a package.json file in your project directory.

3. Install Primate

Add the Primate library to your project.

yarn add @thewebchimp/primate@o1

Next, create a file called app.js in your project root directory and set up Primate as follows

This sets up and starts the Primate server.

4. Setting up Prisma

4.1 Create Prisma schema

Create a directory for Prisma configuration and schema file:

Now, open prisma/schema.prisma and define your database connection and generator:

Here, Prisma will use MySQL as the database provider, and the DATABASE_URL is fetched from the environment variables.

4.2 Prisma models

Define the User and Attachment models in the same schema.prisma file:

These models represent the database tables for User and Attachment.

5. Create Environment File

Create a .env file in the root of your project to store your environment variables, including the database connection string and other necessary secrets.

Add the following content to the .env file:

  • PORT: The port on which your API will run.

  • ACCESS_TOKEN_SECRET: The secret key for token generation.

  • DATABASE_URL: Your MySQL database connection string.

6. Generate Prisma Client

To generate the Prisma client based on the models you defined, run:

This will generate a client that allows you to interact with the database in your Node.js application.

7. Create Entities Directory

Now, create a directory called entities to organize your Prisma models or additional business logic.

You can now start building out your application by adding more models, services, or API routes to handle CRUD operations, authentication, and more.

8. Run the API

Finally, to run your API, use the following command:

Your API will start on the port defined in the .env file (default is 1337), and you should be able to access it at http://localhost:1337.

Last updated