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 What is it?
• Thinking in objects instead of mapping relational data • Queries not classes to avoid complex model objects • Single source of truth for database and application models • Healthy constraints that prevent common pitfalls and antipatterns • An abstraction that makes the right thing easy ("pit of success") • Type-safe database queries that can be validated at compile time • Less boilerplate so developers can focus on the important parts of their app • Auto-completion in code editors instead of needing to look up documentation Why?
● One file (schema.prisma) ○ Data sources: Specify the details of the data sources Prisma should connect to (e.g. a PostgreSQL database) ○ Generators: Specifies what clients should be generated based on the data model (e.g. Prisma Client) ○ Data model definition: Specifies your application models (the shape of the data per data source) and their relations ● Syntax - PSL (Prisma Schema Language) Schema
What does it generate? ● Models ○ Convert Table to Typescript Types ● Query Models (type safe) ○ Constraint to build where clause ○ Constraint to build select ○ Constraint to build insert/update/delete with relation Generate
Operations • create/createMany • update/updateMany • delete/deleteMany • findUnique/findMany/findFirst • aggregate • count • groupBy • upsert (create or update) One for each model Client
Prisma Migrate is an imperative database schema migration tool that enables you to: • Keep your database schema in sync with your Prisma schema • Maintain existing data in your database Migration
1. Update your prisma schema 2. Run migration command `npx prisma migrate dev` a. It checks your db and your schema b. It creates a sql file to apply the changes 3. Happy code Migration - Workflow Development
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 Seeding
The deploy exists only if you are using migrations `npx prisma migrate deploy` That command applies migrations in you database. N.B. Use a CI/CD flow for your deployment Deploy
Pros • Wonderful developer experience • Type safe queries • Migrations • Custom queries • Active Community • Awesome documentation • Core team active in Github and Slack Conclusion