DEV Community

Kb Bohara
Kb Bohara

Posted on

Prisma 101 baby.

What is prisma ?

Prisma is an ORM (Object Relational Mapper) that simplifies database interactions for Node.js and TypeScript applications.
ORM is a software tool that connects programming code to database structures.
Don't worry if you don't get it.

Why use prisma?

  1. Automatically generates TypeScript types which gives a lots of benefits like:
  2. Error Prevention
const user = await prisma.user.findUnique({
  where: { id: "123" }, // Error: `id` expects a number, not a string!
});
Enter fullscreen mode Exit fullscreen mode
  • Auto-Completion
  • Seamless Refactoring: If you update your database schema , Prisma updates the generated types and your IDE will highlight where you need changes in your code.
  • Strongly-Typed Relationships: Prisma understands relationships between tables and generates types accordingly.
  • Thats all I care about for prisma 101. ## How to setup prisma?
  • Install the Prisma CLI as a dev dependency in the project.
pnpm add prisma -d
Enter fullscreen mode Exit fullscreen mode
  1. Now initialize prisma in your project.
npx prisma init --datasource-provider mysql # depends on what you use.
Enter fullscreen mode Exit fullscreen mode

This creates a prisma dir with a schema.prisma file and configures mysql as your database.
And that's all you have to do. How easy pg.

schema.prisma

In this file you define model/schema.

model Gf {  
id Int @id @default(autoincrement())  
name String @db.Varchar(255)
Chats Chat[] @relation("GfToChats")
}

model Chat{
id Int @id @default(autoincrement())
msg String @db.Text
gfId Int
gf Gf @relation("GfToChats", fields:[gfId] references:[id])
}
Enter fullscreen mode Exit fullscreen mode

But Before we move into next step let me tell a little about pre-existed code.

// Generator block: Specifies what Prisma should generate after running 
`prisma generate`.
generator client {
  provider = "prisma-client-js" // Generates the Prisma Client for JavaScript/TypeScript.
}
Enter fullscreen mode Exit fullscreen mode

// Datasource block: Defines the connection to your database.
datasource db {
  provider = "mysql" // Specifies the type of database you're using (MySQL in this case).
  url = env("DATABASE_URL")
}
Enter fullscreen mode Exit fullscreen mode

Now what next?

Migration

At this point, you have a Prisma schema but no database yet. Run the following command in your terminal to create the mysql database and the Gf and Chat tables represented by your models:

npx prisma migrate dev --name init
Enter fullscreen mode Exit fullscreen mode

This command did three things:

  1. It created a new SQL migration file for this migration in the prisma/migrations dir.
  2. It executed the SQL migration file against the database.
  3. It ran prisma generate under the hood (which installed the @prisma/client package and generated a tailored Prisma Client API based on your models). ## prisma generate npx prisma generate is a command within the Prisma CLI that reads your Prisma schema file and generates a tailored database client called "Prisma Client" which allows you to interact with your database using type-safe queries, essentially creating the necessary code to perform operations like creating, reading, updating, and deleting data based on your defined data models in the schema; you need to run this command whenever you make changes to your Prisma schema to update the generated client code. You can say it generates a strongly-typed functions to interact with db. "That how I imagine it."

prisma studio

npx prisma studio it gives a web gui to interact with you db.

Prisma Client

Now that the Prisma Client has been generated, you can start using it to interact with your database.

// for not serverless application
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export default prisma;
Enter fullscreen mode Exit fullscreen mode
// for serverless
import { PrismaClient } from "@prisma/client";

const prismaClientSingleton = () => {
  return new PrismaClient();
};

declare const globalThis: {
  prismaGlobal: ReturnType<typeof prismaClientSingleton>;
} & typeof global;

const prisma = globalThis.prismaGlobal ?? prismaClientSingleton();

export default prisma;

if (process.env.NODE_ENV !== "production") globalThis.prismaGlobal = prisma;

Enter fullscreen mode Exit fullscreen mode

Interact with db

Now you get into action and create Gf or chat.

// Create a new Gf
import prisma from @/lib/db
const create Gf = async () =>{
  const gf = await prisma.Gf.create({
    data:{
      name:"rishi",
      email:"mujrawala@gfmail.com",
      Chats: { create: { msg: "Hello" } }
    }
  });
  console.log("Created a new Gg:",gf.name);
}
Enter fullscreen mode Exit fullscreen mode
// breakup
const breakup= async() => {
  const ex = await prisma.Gf.delete({ where: { name: "jainam" }});   
  console.log("Broke up with:", ex.name);
}
Enter fullscreen mode Exit fullscreen mode

Handle Disconnection

Don’t forget to disconnect the Prisma Client when you’re done using it to avoid memory leaks or database connection issues.

try{
  // Try creating new gf, break up or patch up 
}catch{
  // Cant even make a gf, f**king looser
}finally{
  await prisma.$disconnect();
}
Enter fullscreen mode Exit fullscreen mode

Seed Script

A seed script is a script that generates dummy data

// prisma/seed.ts
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();

async function main() {
  await prisma.gf.create({
    data: {
      name: "raashed",
      email:"raaseed@gfmail.com"
    },
  });
}

main().catch(e => {
  throw e;
}).finally(async () => {
  await prisma.$disconnect();
});
Enter fullscreen mode Exit fullscreen mode

More on Migrations

Prisma migrations allow you to manage database schema changes over time, making it easy to apply, track, and rollback changes to your database.
prisma migrate dev vs prisma migrate deploy

  • npx prisma migrate dev: This command is used during development. It automatically applies new migrations to your database and also updates your Prisma Client based on schema changes. It is ideal when you are actively working on your schema and want to quickly apply changes.
  • npx prisma migrate deploy: This command is used for deploying migrations to a production database. Unlike dev, it does not generate new migrations. Instead, it applies all pending migrations created during development. ## prisma db push If you're not using migrations (e.g., in simpler projects or prototypes), you can use npx prisma db push to push your Prisma schema directly to the database. This will create tables based on your Prisma schema but without the tracking and rollback features of migrations. This is useful for quick changes but is not recommended for production environments.

I’d love to hear your thoughts! If you found this tutorial helpful or have any suggestions for improvement, please leave a comment below. Your feedback is greatly appreciated!

If you’d like to connect or discuss further, feel free to reach out to me thekbbohara. I’d be happy to chat about my projects or any other topics related to development!

Happy coding! 👨‍💻

Top comments (0)