What is ORM

Object–relational mapping (ORM) in computer science is a programming technique for converting data between a relational database and the heap of an object-oriented programming language. This creates, in effect, a virtual object database that can be used from within the programming language.

What is Prisma ORM

When using Prisma ORM, Everything start with a Prisma schema. The Prisma schema allows developers to define their application models in an intuitive data modeling language.

Tools:

  • 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.

Prisma schema

Example:

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

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

model Post {
  id        Int     @id @default(autoincrement())
  title     String
  content   String?
  published Boolean @default(false)
  author    User?   @relation(fields: [authorId], references: [id])
  authorId  Int?
}

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
  posts Post[]
}

The Prisma schema data mode

A model has two major functions:

  • Represent a table in relational databases or a collection in MongoDB
  • Provide the foundation for the queries in the Prisma Client API

There are two major workflows for “getting” a data model into your Prisma schema:

  • Manually writing the data model and mapping it to the database with Prisma Migrate
  • Generating the data model by introspecting a database

Prisma Client

You are Accessing your database with Prisma Client.

Third part lib

npm install @prisma/client

Using Prisma Client to send queries to your database

For example, Create a new User and a new Post record in the same query:

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()

// Run inside `async` function
const user = await prisma.user.create({
  data: {
    name: 'Alice',
    email: 'alice@prisma.io',
    posts: {
      create: { title: 'Join us for Prisma Day 2020' },
    },
  },
})

Prisma Migrate

Prisma Migrate is important tool.

Prisma Migrate enables you to:

  • Keep your database schema in sync with your Prisma schema as it evolves and
  • Maintain existing data in your database

Prisma Migrate generates a history of .sql migration files, and plays a role in both development and production.

Prisma Migrate can be considered a hybrid database schema migration tool, meaning it has both of declarative and imperative elements:

  • Declarative: The data model is described in a declarative way in the Prisma schema. Prisma Migrate generates SQL migration files from that data model.
  • Imperative: All generated SQL migration files are fully customizable. Prisma Migrate hence provides the flexibility of an imperative migration tool by enabling you to modify what and how migrations are executed (and allows you to run custom SQL to e.g. make use of native database feature, perform data migrations, …).