# Setting Up Prisma — Prisma / TypeORM / Sequelize

Source: https://www.skillbyai.com/en/orms/or-prisma-setup

> Initialise a Prisma project, write a minimal schema, generate the client and run a first query against it.

## Schema, generator, client

A Prisma project has three moving parts. The **schema** (`prisma/schema.prisma`) contains a `datasource` block that names the database provider, a `generator` block that says which client to produce, and one `model` block per table. The **Prisma CLI** (`prisma` package, a dev dependency) reads the schema to create migrations and to **generate** the client. The **Prisma Client** (`@prisma/client`) is the generated, typed query API your code imports. The key idea is that the client is *generated code*: when you change the schema you must regenerate, or your types and queries will not match the database. `npx prisma init` scaffolds the folder and a `.env` file for the connection string. Note that recent major versions of Prisma moved some configuration, such as the connection URL, into a `prisma.config.ts` file and changed the default generator, so always follow the setup page for the version you install.

## A minimal schema

Two models with a one-to-many relation. `@id`, `@default` and `@unique` become a primary key, a default and a unique constraint in SQL.

```text
// prisma/schema.prisma (shown with the classic generator)
datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

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

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

model Post {
  id       Int    @id @default(autoincrement())
  title    String
  authorId Int
  author   User   @relation(fields: [authorId], references: [id])
}
```

## Generate, migrate, query

Create the tables with a migration, generate the client, then use it from TypeScript.

```ts
// terminal: npx prisma migrate dev --name init && npx prisma generate
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  const user = await prisma.user.create({
    data: { email: 'asha@example.com', name: 'Asha' },
  });
  console.log(user.id, user.email);
}

main().finally(() => prisma.$disconnect());
```

## Red squiggles after a schema change

If TypeScript says a field does not exist right after you added it to the schema, the client is stale. Run `npx prisma generate` (some Prisma versions also run it as part of `migrate dev`) and restart your editor's TypeScript server if needed.

**Quiz:** You added a phone field to the User model. What must happen before prisma.user.create can accept phone with correct types?

- [ ] Nothing; Prisma reads the schema at runtime on every query
- [x] The client must be regenerated with prisma generate
- [ ] You must add a @Column decorator to the User class
- [ ] You must call sequelize.sync()

*Answer:* The client must be regenerated with prisma generate. Prisma Client is generated code. Schema changes reach your types only after regeneration; the database table changes only after a migration is applied.
