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. xedotnet.org Luca Del Puppo @puppo92 Prisma in the air

  2. Agenda •What is it? •Data Modeling • Client •Migration •Deploy

    Intro
  3. www.delpuppo.net luca@delpuppo.net @puppo92 Luca Del Puppo Full-Stack developer @Flowing Ego

    Slide
  4. 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?
  5. • 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?
  6. Where it is positioned

  7. None
  8. • Create project (prisma/schema.prisma) • Generate Types • Generate Migrations

    • Apply Migrations • Create Db N.B. available using `npm install prisma` CLI
  9. npx prisma init --datasource-provider PostgreSQL Let’s start

  10. • PostgreSQL • MySQL • SQLite • Microsoft SQL Server

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

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

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

    tailored to your data. npm install @prisma/client Client
  16. Client

  17. Operations • create/createMany • update/updateMany • delete/deleteMany • findUnique/findMany/findFirst •

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

    Switch DB Provider is not supported Migration - Limitations
  21. Introspection

  22. 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
  23. 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
  24. DEMO Demo

  25. Cons • Newby • Don’t manage multiple provider concurrency •

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

    Migrations • Custom queries • Active Community • Awesome documentation • Core team active in Github and Slack Conclusion
  27. My Series about Prisma https://dev.to/this-is-learning/series/15827

  28. That’s all folks Grazie per l’ascolto Codice: https://github.com/puppo/prisma-in-the-air

  29. www.delpuppo.net luca@delpuppo.net @puppo92 Luca Del Puppo Full-Stack developer @Flowing Contatti