Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Prisma the ORM that node was waiting for

Prisma the ORM that node was waiting for

Luca Del Puppo

November 21, 2022
Tweet

More Decks by Luca Del Puppo

Other Decks in Programming

Transcript

  1. 1. What is it? 2. CLI 3. Data Modeling 4.

    Generation Agenda Prisma the ORM that node was waiting for 5. Client 6. Migration 7. Deploy 8. Conclusion
  2. Who I am Luca Del Puppo 
 (aka Puppo)
 Full-Stack

    Developer in Flowing a Claranet Italian Company @puppo92
 https://www.linkedin.com/in/lucadelpuppo/
 [email protected]
  3. Where it is positioned Productivity 0 10 Control 0 12

    ORM Prisma SQL Query Build Plain SQL Min - Healty Constraint - Max
  4. ➔ PostgreSQL ➔ MySQL ➔ SQLite ➔ Microsoft SQL Server

    ➔ MongoDB ➔ CockroachDB Providers
  5. generator client { provider = "prisma-client-js" } datasource db {

    provider = "postgresql" url = env("DATABASE_URL") } Prisma File
  6. model Ingredient { id Int @id @default(autoincrement()) name String createdAt

    DateTime @default(now()) updatedAt DateTime @updatedAt pizzaIngredients PizzaIngredient[] @@map("INGREDIENTS") } export type Ingredient = { id: number name: string createdAt: Date updatedAt: Date } Generate Types & Client
  7. ➔ One file (schema.prisma) ➔ Data sources ➔ Generators ➔

    Data model definition ➔ Syntax - PSL (Prisma Schema Language) N.B. Multiple Files is not supported yet Schema
  8. generator client { provider = "prisma-client-js" } datasource db {

    provider = "postgresql" url = env("DATABASE_URL") } Generator & DataSource
  9. model Ingredient { id Int @id @default(autoincrement()) name String createdAt

    DateTime @default(now()) updatedAt DateTime @updatedAt pizzaIngredients PizzaIngredient[] @@map("INGREDIENTS") } First look to the model
  10. model PizzaIngredient { ingredientId Int pizzaId Int pizza Pizza @relation(

    fi elds: [pizzaId], references: [id]) ingredient Ingredient @relation( fi elds: [ingredientId], references: [id]) @@id([pizzaId, ingredientId]) @@map("PIZZA_INGREDIENTS") } First look to the relations
  11. ➔ Models ๏ Convert Models to Typescript Types ➔ Client

    ➔ Query Models (type safe) ๏ Constraint to build where clause ๏ Constraint to build select ๏ Constraint to build insert/update/delete with relation What does it generate?
  12. const prisma = new PrismaClient(); await prisma.$connect(); const pizza =

    await prisma.pizza.findUnique({ where: { id: id, }, include: { pizzaIngredients: { include: { ingredient: true, }, }, }, }); How to use the client?
  13. Crud SQL\Prisma Single Multiple Insert create createMany Update update updateMany

    Delete delete deleteMany Select findUnique/ findFirst findMany Insert/Update upsert -
  14. Migration Developer Prisma Schema Prisma CLI Database 1. update 2.

    prisma migrate dev 3. read schema 4. database schema 5. generate migration 6. update database
  15. Introspection Developer Prisma Schema Prisma CLI Database 1. Change database

    schema (SQL) 2. prisma db pull 3. Reads database schema 4. Database schema 5. Updates
  16. ➔ MongoDb is not supported (use `prisma db push`) ➔

    Switch DB Provider is not supported Limitations
  17. Prisma exposes us a command for seeding the db •

    It runs on • prisma migrate dev • prisma migrate reset • manually • It can be written in typescript, go, sql… • ‘--skip-seed’ allows skipping Seeding
  18. • Newby • Don’t manage multiple provider concurrency • Splitting

    schema not implemented out-of-the-box (prisma-aurora) Cons
  19. • Wonderful developer experience • Type safe queries • Migrations

    • Custom queries • Active Community • Awesome documentation • Core team active in Github and Slack Pros