Urgent.News

What's breaking now, across thousands of outlets.

Tech

Outline: Add Prisma ORM to a Node.js project using PostgreSQL DB

Quick outline to add Prisma ORM to any Node.js or Express project using a PostgreSQL DB. This outline will work if adding Prisma to an existing or new Node.js project. Install required packages and dependencies Navigate to your project directory or create the project directory and install these required dependencies using any package manager you like: npm install prisma@prev @types/node @types/pg…

To add Prisma ORM to a Node.js project with a PostgreSQL database, follow these steps:

1. Begin by installing the necessary packages and dependencies. Navigate to your project directory or create a new one, then run the following commands using your preferred package manager:

```

npm install prisma@prev @types/node @types/pg --save-dev

npm install @prisma/client@7 @prisma/adapter-pg pg dotenv

```

2. Prisma is a CLI tool that allows you to run various commands in the terminal, such as `prisma init`, `migrate`, `pull`, and `studio`. The `@prisma/client` package is the client library used for querying the database, while `@prisma/adapter-pg`, `pg`, and `@types/pg` provide the Postgres driver adapter, driver, and TypeScript type definitions to connect Prisma to the client Postgres database.

3. Initialize Prisma by running this command:

```

npx prisma init --datasource-provider postgresql --output ../generated/prisma

```

This action creates a `prisma` folder containing the `schema.prisma` file, which holds your database connection setup, as well as a `.env` file and a `prisma7.config.ts` file in the root directory.

4. Connect your database by editing the connection details in the `.env` file. It should look like this:

```

DATABASE_URL = postgresql://user:password@localhost:5432/mydb?schema=public

```

Replace the `user`, `password`, and `mydb` with your actual database credentials and name.

5. Next, you have two options:

a. Pull your existing database by running:

```

npx prisma db pull

```

This command updates the `schema.prisma` file based on your current database schema.

b. Alternatively, update the `prisma/schema.prisma` file, generator client configuration, and define your models, such as the `User` model below:

```prisma

generator client {

provider = "prisma-client"

output = "../generated/prisma"

}

datasource db {

provider = "postgresql"

}

model User {

id Int @id @default(autoincrement())

name String

email String @unique

}

```

Add any other tables you may need for your application.

6. Finally, run the migration to apply the changes to your database with this command:

```

npx prisma migrate dev --name init

```

This step will create the necessary database tables based on your Prisma schema, and your Node.js project is now ready to use Prisma ORM with a PostgreSQL database.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

More from Thursday 1 October →