PRISMA ?

Juno_Dev1Β·2025λ…„ 8μ›” 2일

πŸ“Œ Concept: Prisma

πŸ” Intro

  • Prisma
  • schema in prisma
  • prisma model

βœ… Who is Prisma? what is Prima?

Earlier, we learned that ORM tends to play the role of bridge between RDB and Object language. ORMs have introduced several issues (less type safety, complicated queries,Mismatch between the data model and the code).The prisma addresses these shortcomings, focused on type safety and development productivity.

"On top of being an ORM, it provide us with a complex tool kit for accessing database.

βš™οΈ Core

install Prisma

  1. Prisma CLI
npm install -g prisma // without install we can use npx

install client

npm install  @prisma/client

prisma initialise

Creating prisma directory, make .env file and schema.prisma files in the project root.

the constitution of prisma

prisma is mainly consisted with 3 main factors.

  • schema

    • using .prisma extention file defining schema as intuitive language and simplified.
    • Not only does Schema plays the blueprint of database, but provide information to create the prisma client.
    • only one schema file can manages Migration, defining relation, etc.
  • client

    • It is the auto generatated Type-safe database query builder based on the Prisma Schema.

    • It commonly use when interacting with a database from Node.js or TypeScript code.
      * without complicated queries or raw query strings, we can use HTTP methods.

    • Autocomplete and type inspection.

      • In TypeScript projects, these features help catch errors during development, which improves code safety and productivity.
  • prisma Migrate

    • It is the managemnet tool to adopt modifcations.

    • when developer updates the schema file, Prisma Migrate tracks changes of database, generates essential migration scripts.

    • It makes data schema synchronization easy between the development and production environmnet.

🧠 code

prisma client


// μ˜ˆμ‹œ: schema.prisma 파일
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        Int      @id @default(autoincrement())
  email     String   @unique
  name      String?
  posts     Post[]   // User와 PostλŠ” 1:N 관계
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  content   String?
  published Boolean  @default(false)
  author    User     @relation(fields: [authorId], references: [id])
  authorId  Int      // μ™Έλž˜ ν‚€
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

accessing data by Prisma


import {PrismaClient} from "@prisma/client";

const prisma = new PrismaClient

async function main(){
	// create new user
  	const newUser = await prisma.user.create({
    	data:{
        	name: 'Alice',
          	email: 'Alice@email.com',	
        },
    })
    console.log('created user:'newUser);
  
  
  // access all users
  const allUser = await prisma.user.findMany();
  console.log('
}

πŸ’‘ Good to know

When you visualise Prisma Table, it is easy for developer to confuse understanding or writing code. So I am fully recommend to use 3 logical statement and relationships...

Example:

  1. Product can have multiple tags, also the tags can have multiple tags

  2. there is Many - many relationship between products and tags.

  3. this join stores pairs of productID and TagID to represent relation.

0개의 λŒ“κΈ€