Skip to content

Repository files navigation

ABOUT THIS TUTORIAL

This is a tutorial kind of a stuff where you will learn how to create a Next JS(Using Typescript) app with the basic but full fledged login and Sign Up functionalities using PostgreSQL as the database and Prisma as the ORM for Postgres.

Why Docker for building a postgres instance?

Because it is easy, simple and minimal. Requires less time and effort for setting up the instance locally.
Some of the other preffered ORMs for PostgreSQL can be Drizzle, Sequelize, Hibernate etc.

How to run Postgres+Prisma using Docker?

What is Prisma ORM?

Prisma is an open-source Object-Relational Mapping (ORM) tool designed for JavaScript and TypeScript applications. It provides a type-safe way to interact with databases, allowing developers to perform database operations using familiar JavaScript or TypeScript syntax without writing raw SQL queries. Prisma works well with various relational databases, including PostgreSQL, MySQL, SQLite, and SQL Server.

Key Features of Prisma

  1. Type Safety: Generates TypeScript types based on your database schema, helping prevent runtime errors.
  2. Schema Modeling: Prisma uses a declarative schema file (schema.prisma) to define data models, relationships, and configurations, which can then be used to generate migrations for database management.
  3. Query Engine: Provides intuitive methods for CRUD operations (Create, Read, Update, Delete) with powerful filtering, sorting, and pagination capabilities.
  4. Data Validation: Simplifies input validation with its strong typing and model constraints.

Benefits of Using Prisma

  1. Developer Productivity: Type safety, autocompletion, and schema validation reduce bugs and improve efficiency.
  2. Compatibility with Modern Frameworks: Prisma integrates well with frameworks like Next.js, NestJS, and Express, making it a great choice for full-stack applications.

Download the required npm libraries

npm i bcryptjs bcrypt prisma @types/bcryptjs @types/bcrypt @prisma/client

brcyptjs is a library that stores the password in the database in an encrypted format.
Prisma Library is used to connect the backend with postgreSQL DB using the Prisma Client.

After completing the code of docker-compose.yml and Dockerfile, run the commands below

docker-compose up -d --build

Then connect to the databse manually inorder to ensure that the database is accessible

docker-compose exec postgres psql -U postgres -d users

Here, users is the name of the database.

To check whether the docker image of postgres is up or not we can use the following command:

docker ps -a

The above command will show all the images along with their uptime.

Now, we have successfully set up the postgres database docker.
Use the following commands to set up the prisma ORM for connecting the Next.js app to PostgreSQL

npx prisma generate
npx prisma generate --schema=./schemas/schema.prisma
npx prisma migrate dev --name init

Now we have successfully established the connection between Next.js and PostgreSQL
In order to visualize the database in a GUI form, we can use the Prisma studio.
Use the following command to fire prisma studio

npx prisma studio

About

This is a tutorial to understand how to integrate PostgreSQL in your Next JS project with Prisma ORM and Next JS backend. This ia a sample app to create user singup and login functionalities

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages