Prisma: Next-generation Node.js and TypeScript ORM

A presentation by Marvin Witt

๐Ÿค” What is Prisma?

  • Prisma is a database toolkit for Node.js and TypeScript
  • It is a next-generation ORM (Object-Relational Mapper)
  • It simplifies database workflows and allows for easy data management
  • It supports various database systems such as MySQL, PostgreSQL, and MongoDB
  • It features a powerful query engine and flexible data modeling

๐Ÿค” Why Prisma instead of raw SQL?

  • Prisma is a modern ORM designed for Node.js and TypeScript
  • Offers powerful query engine and flexible data modeling
  • Integrates seamlessly with GraphQL
  • Strong developer community and ecosystem
  • Popular choice among developers

๐Ÿง Prisma vs. Raw SQL

Feature Prisma Raw SQL
Model definition Yes N/A
Type-safe queries Yes No
Query building Yes No
Relationship mapping Yes No
Database migration Yes No
Transaction support Yes Yes
Connection pooling Yes Yes
Performance High High
Ease of use High Low
Integration with serverless APIs Good Good

๐Ÿง Prisma vs. Query Builders

Feature Prisma Query Builders
Model definition Yes Yes
Type-safe queries Yes No
Query building Yes Yes
Relationship mapping Yes No
Database migration Yes No
Transaction support Yes Yes
Connection pooling Yes Yes
Performance High High
Ease of use High Medium
Integration with serverless APIs Good Good

๐Ÿ“ The Prisma Schema

  • The Prisma schema is the single source of truth for your database schema
  • It is used to generate the Prisma Client API
  • It is written in the Prisma schema language (.prisma)

๐Ÿ“ The Prisma Schema: ๐Ÿ’พ Datasource

Datasource

๐Ÿ“ The Prisma Schema: ๐Ÿ“ฆ Generator

Generator

๐Ÿ“ The Prisma Schema: ๐Ÿงต Models & Relations

Model & Relations

๐Ÿ“ฆ The Prisma Client API

  • The Prisma Client API is generated from the Prisma schema
  • It is a type-safe database client
  • It is used to perform database operations
  • It is used to perform CRUD operations

๐Ÿ“ฆ The Prisma Client API: ๐Ÿ” Find Many

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

const prisma = new PrismaClient();

const allUsers = await prisma.user.findMany();
console.log(allUsers);

๐Ÿ“ฆ The Prisma Client API: ๐Ÿ” Find Many

Output:

[
  {
    "id": 1,
    "name": "Alice",
    "email": "alice@prisma.io",
    "posts": [
      {
        "id": 1,
        "title": "Join the Prisma Slack",
        "content": "https://slack.prisma.io",
        "published": true,
        "authorId": 1
      }
    ]
  }
]

๐Ÿ“ฆ The Prisma Client API: ๐Ÿชก Find One

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

const prisma = new PrismaClient();

const user = await prisma.user.findUnique({
  where: {
    email: "alice@prisma.io",
  },
});

console.log(user);

๐Ÿ“ฆ The Prisma Client API: ๐Ÿชก Find One

Output:

{
  "id": 1,
  "name": "Alice",
  "email": "alice@prisma.io",
  "posts": [
    {
      "id": 1,
      "title": "Join the Prisma Slack",
      "content": "https://slack.prisma.io",
      "published": true,
      "authorId": 1
    }
  ]
}

๐Ÿ“ฆ The Prisma Client API: โž• Create

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

const prisma = new PrismaClient();

const newUser = await prisma.user.create({
  data: {
    name: "Bob",
    email: "bob@prisma.io",
  },
});

console.log(newUser);

๐Ÿ“ฆ The Prisma Client API: โž• Create

Output:

{
  "id": 2,
  "name": "Bob",
  "email": "bob@prisma.io"
}

๐Ÿ“ฆ The Prisma Client API: ๐Ÿ” Update

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

const prisma = new PrismaClient();

const updatedPost = await prisma.post.update({
  where: {
    id: 1,
  },
  data: {
    published: false,
  },
});

console.log(updatedPost);

๐Ÿ“ฆ The Prisma Client API: ๐Ÿ” Update

Output:

{
  "id": 1,
  "title": "Join the Prisma Slack",
  "content": "https://slack.prisma.io",
  "published": false,
  "authorId": 1
}

๐Ÿ“ฆ The Prisma Client API: ๐Ÿšฎ Delete

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

const prisma = new PrismaClient();

const deletedUser = await prisma.user.delete({
  where: {
    id: 1,
  },
});

console.log(deletedUser);

๐Ÿ“ฆ The Prisma Client API: ๐Ÿšฎ Delete

Output:

{
  "id": 1,
  "name": "Alice",
  "email": "alice@prisma.io"
}

๐Ÿ“ฆ The Prisma Client API: ๐Ÿชน Nested Writes

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

const prisma = new PrismaClient();

const newUser = await prisma.user.create({
  data: {
    name: "Bob",
    email: "bob@prisma.io",
    posts: {
      create: [
        {
          title: "Follow Prisma on Twitter",
          content: "https://twitter.com/prisma",
          published: true,
        },
        {
          title: "Follow Prisma on GitHub",
          content: "https://github.com/prisma",
          published: true,
        },
      ],
    },
  },
});

console.log(newUser);

๐Ÿ“ฆ The Prisma Client API: ๐Ÿชน Nested Writes

Output:

{
  "id": 2,
  "name": "Bob",
  "email": "bob@prisma.io",
  "posts": [
    {
      "id": 2,
      "title": "Follow Prisma on Twitter",
      "content": "https://twitter.com/prisma",
      "published": true,
      "authorId": 2
    },
    {
      "id": 3,
      "title": "Follow Prisma on GitHub",
      "content": "https://github.com/prisma",
      "published": true
    }
  ]
}

๐Ÿ“ฆ The Prisma Client API: ๐Ÿชน Nested Writes

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

const prisma = new PrismaClient();

const updatedUser = await prisma.user.update({
  where: {
    id: 2,
  },
  data: {
    posts: {
      update: [
        {
          where: {
            id: 2,
          },
          data: {
            published: false,
          },
        },
      ],
      delete: [
        {
          id: 3,
        },
      ],
    },
  },
});

console.log(updatedUser);

๐Ÿ“ฆ The Prisma Client API: ๐Ÿชน Nested Writes

Output:

{
  "id": 2,
  "name": "Bob",
  "email": "bob@prisma.io",
  "posts": [
    {
      "id": 2,
      "title": "Follow Prisma on Twitter",
      "content": "https://twitter.com/prisma",
      "published": false,
      "authorId": 2
    }
  ]
}

Thank you for your attention! ๐Ÿ™

Got any questions? ๐Ÿค” Feel free to ask away! ๐Ÿ™Œ

You can also find the slides of this presentation at prisma.nurmarv.in!