Skip to content

[Overview] Prisma

Anh Tu Do edited this page Sep 9, 2023 · 2 revisions

Main link

https://www.prisma.io/

What is Prisma?

Prisma is an ORM

  • Object-relational mapping (ORM) is a way to align programming code with database structures

With Prisma, you can write schemas and models easily without touching SQL (unless you want to customize your database migrations)

Prisma auto-generates SQL migrations from your defined schemas for you. Migrations are simply SQL files that create/alter tables to look like your schemas. These migration files are stored in /prisma/migrations and act as the source of truth for all coders in your project

Prisma also generates types (as in Typescript) and CRUD functions on the models for you to use directly in the Javascript/Typescript code

What is the flow to use Prisma?

There are 3 simple steps you must always do:

1. Update data schemas/models

Go to /server/prisma/schema.prisma

Make necessary changes to the models

You can reference this link to learn more about the syntax

2. Generate a migration for your new changes and push to database

You can accomplish this simply by running npm run pdev -- --name your_migration_name

You can go to /server/package.json to see what command runs underneath npm run pdev

3. Generate in-code types and CRUD operations

You can simply run npm run pgen

Extremely important notes

For step 2 (generate a migration and push to database) to run, you need to have Docker running first.

To get Docker running, please reference to [Overview] Docker doc.