Luca Del Puppo - Senior Software Engineer

Prisma the ORM that node was waiting for

Luca Del Puppo

  • Senior Software Engineer
  • Javascript enthusiast
  • Typescript lover
  • “Youtuber”
  • “Writer”

Love sport: running, hiking

Love animals

Agenda

  1. What is it?
  2. CLI
  3. Data Modeling
  4. Generation
  1. Client
  2. Migration
  3. Deploy
  4. Conclusion

What is it?

Prisma is an open source next-generation ORM. (Node eco-system)

Prisma Client

Auto-generated and type-safe query builder for Node.js & TypeScript

Prisma Migrate

Migration system

Prisma Studio

GUI to view and edit data in your database

Control

Productivity

Orm

Plain SQL

SQL Builder

Prisma

Healty Constraint

Providers

  • PostgreSQL
  • MySQL
  • SQLite
  • Microsoft SQL Server
  • MongoDB
  • CockroachDB

Hey Bro!

Could you talk about interesting stuff?

CLI

$ npm install prisma

Install CLI

npx prisma init --datasource-provider PostgreSQL

Create Project

Prisma File

generator client {
  provider = "prisma-client-js"
}

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

CLI commands

  • init
  • generate
  • migrate

Data Modelling

Schema

  • One file (schema.prisma)
    • Data sources
    • Generators
    • Data model definition
  • Syntax - PSL (Prisma Schema Language)

 

N.B. Multiple Files is not supported yet

Generator & DataSource

generator client {
  provider = "prisma-client-js"
}

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

Models

model Ingredient {
  id Int @id @default(autoincrement())
  name String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  pizzaIngredients PizzaIngredient[]

  @@map("INGREDIENTS")
}

Relations

model PizzaIngredient {
  ingredientId Int
  pizzaId Int
  ingredient Ingredient @relation(fields: [ingredientId], references: [id])
  pizza Pizza @relation(fields: [pizzaId], references: [id])

  @@id([pizzaId, ingredientId])

  @@map("PIZZA_INGREDIENTS")
}

Generation

$ npx prisma generate

Generation

What does it generate?

  • 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

Generated Type

export type Ingredient = {
  id: number
  name: string
  createdAt: Date
  updatedAt: Date
}

Client

$ npm install @prisma/client

Installation

What does it generate?

const prisma = new PrismaClient();
await prisma.$connect();

const pizza = await prisma.pizza.findUnique({
 where: {
   id: id,
 },
 include: {
      pizzaIngredients: {
      include: {
        ingredient: true,
      },
    },
  },
});

Crud

SQL\Prisma Single Multiple
Insert create createMany
Update update updateMany
Delete delete deleteMany
Select findUnique/
findFirst
findMany
Insert/
Update
upsert

Aggregation

  • Aggregate
  • Count
  • GroupBy

One for each model

Migration

Prisma Schema

Prisma CLI

Database

1. update

2. prisma migrate dev

3. read schema

4. database schema

5. generate migration

6. update database

Migration flow

Prisma Schema

Prisma CLI

Database

2. prisma db pull

Introspection flow

1. Change database schema (SQL)

3. read schema

4. database schema

5. Update

Limitations

  • MongoDb is not supported (use prisma db push)
  • Switch DB Provider is not supported

Seeding

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

Deploy

$ npx prisma migrate deploy

N.B. Use a CI/CD flow for your deployment

Demo

Conclusion

Cons

  • Newby
  • Don’t manage multiple provider concurrency
  • Splitting schema not implemented out-of-the-box (prisma-aurora)

Pros

  • Wonderful developer experience
  • Type safe queries
  • Migrations
  • Custom queries
  • Active Community
  • Awesome documentation
  • Core team active in Github and Slack

Slides

Prisma Series

We are hiring

Luca Del Puppo

@puppo92

Luca Del Puppo

Puppo_92

@puppo

Prisma the ORM that node was waiting for

By Luca Del Puppo

Prisma the ORM that node was waiting for

  • 519