LogoGalsenext
Guide et Tutoriel

Prisma

---

Prisma Configuration

Galsenext uses Prisma as an ORM (Object-Relational Mapping) to interact with the database. Prisma simplifies data management and interaction with your PostgreSQL database.

Prisma Instance Configuration

In Galsenext, Prisma is configured to operate optimally both in development and production modes. The prisma.ts file located in the /src/lib/ directory is configured to create a unique instance of Prisma that is reused throughout the application, optimizing performance and minimizing database connection issues.

// @ts-nocheck
 
import { PrismaClient } from "@prisma/client";
 
export const prisma: PrismaClient =
  global.prisma ??
  new PrismaClient({
    log:
      process.env.NODE_ENV === "development"
        ? ["query", "error", "warn"]
        : ["error"],
  });
 
if (process.env.NODE_ENV !== "production") {
  global.prisma = prisma;
}
  • Creating a Single Instance of Prisma: The Prisma instance is stored in the global variable global.prisma to prevent multiple instances from being created in a development environment, which could cause database connection errors. This approach is particularly useful with Next.js, which uses an automatic module refresh system in development mode.
  • Prisma Logs Configuration: In development mode (process.env.NODE_ENV === "development"), Prisma is configured to display query, error, and warning logs (["query", "error", "warn"]). In production, only error logs are enabled (["error"]) to avoid exposing sensitive information.
  • Environment Check: The code checks if the environment is not in production (process.env.NODE_ENV !== "production"). If so, it assigns the Prisma instance to global.prisma to ensure that only one instance is used throughout the application's lifecycle.

Initial Prisma Setup

The Prisma schema file prisma/schema.prisma is already configured for basic authentication. Here’s how to configure and use Prisma:

1. Initializing Prisma

When you have installed Galsenext, Prisma is already included. You need to set up your database by following these steps:

Database Migration

To apply the Prisma schema to your database, run the following command:

npx prisma migrate dev --name init

This command will create the necessary tables for authentication and prepare your database.

2. Modifying the Prisma Schema

The default schema is sufficient to handle authentication. However, you can modify this file according to your needs. For example, if you want to add models for specific features, edit the prisma/schema.prisma file:

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String
  author    User     @relation(fields: [authorId], references: [id])
  authorId  Int
  createdAt DateTime @default(now())
}

Applying Changes

After modifying the schema.prisma file, you need to run a new migration to apply these changes to your database:

npx prisma migrate dev --name add-post-model

This command will update the database by adding the new model or applying other changes you've made to the schema.

Points to Check

On this page