Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Prisma in the Air

Prisma in the Air

Slides for a talk about how to let's start with Prisma

Here the Github Repo

Luca Del Puppo

April 29, 2022
Tweet

More Decks by Luca Del Puppo

Other Decks in Programming

Transcript

  1. 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?
  2. • 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?
  3. • Create project (prisma/schema.prisma) • Generate Types • Generate Migrations

    • Apply Migrations • Create Db N.B. available using `npm install prisma` CLI
  4. • PostgreSQL • MySQL • SQLite • Microsoft SQL Server

    • MongoDB (April 2022) • CockroachDB (Preview) Providers
  5. • 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
  6. • Field Types • Primary Keys • Foreign Keys •

    Constraints • Table name N.B. more detail about providers in the officials docs Schema
  7. Convert schema to ts types npx prisma generate By default

    types are generated in the folder `node_modules/.prisma/client` Generate
  8. 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
  9. Prisma Client is an auto-generated and type-safe query builder that's

    tailored to your data. npm install @prisma/client Client
  10. Operations • create/createMany • update/updateMany • delete/deleteMany • findUnique/findMany/findFirst •

    aggregate • count • groupBy • upsert (create or update) One for each model Client
  11. 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
  12. 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
  13. • MongoDb is not supported (use `prisma db push`) •

    Switch DB Provider is not supported Migration - Limitations
  14. 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
  15. 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
  16. Cons • Newby • Don’t manage multiple provider concurrency •

    Splitting schema not implemented out-of-the-box (prisma-aurora) Conclusion
  17. Pros • Wonderful developer experience • Type safe queries •

    Migrations • Custom queries • Active Community • Awesome documentation • Core team active in Github and Slack Conclusion