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

app.js
import primate from '@thewebchimp/primate';

await primate.setup();
await primate.start();

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:

mkdir prisma
touch prisma/schema.prisma

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

// prisma/schema.prisma

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

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:

model User {
  id        Int      @id @default(autoincrement())
  uid       String   @unique @default(cuid())
  username  String   @unique
  email     String   @unique
  firstname String
  lastname  String
  nicename  String
  password  String
  type      String   @default("User")
  status    String   @default("Active")
  language  String   @default("en")
  metas     Json?    @default("{}")
  created   DateTime @default(now())
  modified  DateTime @default(now())

  @@map("user")
}

model Attachment {
  id         Int      @id @default(autoincrement())
  slug       String   @unique
  name       String   @default("")
  attachment String   @default("")
  mime       String   @default("")
  size       Int      @default(0)
  source     String   @default("")
  acl        String   @default("")
  metas      Json?    @default("{}")
  created    DateTime @default(now())
  modified   DateTime @default(now())

  @@map("attachment")
}

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.

touch .env

Add the following content to the .env file:

PORT=1337
ACCESS_TOKEN_SECRET=Whatever
DATABASE_URL=mysql://user:password@host:port/database
AWS_SDK_JS_SUPPRESS_MAINTENANCE_MODE_MESSAGE=1
  • 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:

yarn prisma generate

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.

mkdir entities

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:

yarn app.js

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