← Back
Author Avatar

@Tushar-tech7

Published: December 3, 2024

115 Views

Postgres and SQL Database

Postgres and SQL Database
WebDevelopment

Postgres and SQL Database

 
See the difference? Prisma makes the process cleaner and more intuitive!
Step 1 - Types of Databases
 
Here are the main types of databases, each suited to specific use cases:
1️⃣ NoSQL Databases: Schema-less, ideal for unstructured or semi-structured data. (e.g., MongoDB, Firebase).
2️⃣ SQL Databases: Store structured data in tables. Best for most web apps. (e.g., MySQL, PostgreSQL).
3️⃣ Graph Databases: Focus on relationships, great for social networks and recommendation engines. (e.g., Neo4j).
4️⃣ Vector Databases:Stores data in the form of vectors. Useful in machine learning. (e.g.,Pinecone).
 

Why not NoSQL

You might’ve used MongoDB
It’s schemaless properties make it ideal to for bootstraping a project fast.
But as your app grows, this property makes it very easy for data to get curropted
 
You might think that mongoose does add strictness to the codebase because we used to define a schema there.That strictness is present at the Node.js level, not at the DB level. You can still put in erroneous data in the database that doesn’t follow that schema.
 

Why SQL?

SQL databases have a strict schema. They require you to
  1. Define your schema
  1. Put in data that follows that schema
  1. Update the schema as your app changes and perform migrations
 
So there are 4 parts when using an SQL database (not connecting it to Node.js, just running it and putting data in it)
  1. Running the database.
  1. Using a library that let’s you connect and put data in it.
  1. Creating a table and defining it’s schema.
  1. Run queries on the database to interact with the data (Insert/Update/Delete)
 

Creating a database

There are many ways to start a postgres database
  1. Using neondb: https://neon.tech/ is a decent service that let’s you create a server.
  1. Using docker locally:
sudo docker run --name my-postgres -e POSTGRES_PASSWORD=mysecretpassword -d -p 5432:5432 postgres

Connection string

postgresql://postgres:mysecretpassword@localhost:5432/postgres?sslmode=disable

Creating a table and defining it’s schema.

Tables in SQL

A single database can have multiple tables inside. Think of them as collections in a MongoDB database.
SQL stands for Structured query language. It is a language in which you can describe what/how you want to put data in the database.
To create a table, the command to run is -
CREATE TABLE users ( id SERIAL PRIMARY KEY, username VARCHAR(50) UNIQUE NOT NULL, email VARCHAR(255) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP );
 
Do you not think writing SQL commands can sometimes be complex and time-consuming, especially for developers? To make database management easier and more developer-friendly, Prisma comes to the rescue.
 
What is Prisma?
Prisma is a modern ORM (Object-Relational Mapping) tool that simplifies working with databases. Instead of writing raw SQL queries, you can interact with your database using JavaScript/TypeScript code
 

How Prisma Makes Life Easier?

Let’s compare:
  • SQL Query:
SELECT * FROM users WHERE email = 'example@example.com';
  • Prisma Query:
const user = await prisma.user.findUnique({ where: { email: 'example@example.com' }, });
See the difference? Prisma makes the process cleaner and more intuitive!
 
In the next blog, we will dive deep into setting up Prisma and using it to interact with a database step by step. Stay tuned! 🚀

© 2024 Tushar Agarwal. All rights reserved.
Made with By Tushar Agarwal