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.
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.
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.
- Type Safety: Generates TypeScript types based on your database schema, helping prevent runtime errors.
- 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.
- Query Engine: Provides intuitive methods for CRUD operations (Create, Read, Update, Delete) with powerful filtering, sorting, and pagination capabilities.
- Data Validation: Simplifies input validation with its strong typing and model constraints.
- Developer Productivity: Type safety, autocompletion, and schema validation reduce bugs and improve efficiency.
- Compatibility with Modern Frameworks: Prisma integrates well with frameworks like Next.js, NestJS, and Express, making it a great choice for full-stack applications.
npm i bcryptjs bcrypt prisma @types/bcryptjs @types/bcrypt @prisma/clientbrcyptjs 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 --buildThen connect to the databse manually inorder to ensure that the database is accessible
docker-compose exec postgres psql -U postgres -d usersHere, 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 -aThe 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 initNow 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