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

    View Slide

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

    View Slide

  3. www.delpuppo.net
    [email protected]
    @puppo92
    Luca Del Puppo
    Full-Stack developer @Flowing
    Ego Slide

    View 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?

    View Slide

  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?

    View Slide

  6. Where it is positioned

    View Slide

  7. View Slide

  8. • Create project (prisma/schema.prisma)
    • Generate Types
    • Generate Migrations
    • Apply Migrations
    • Create Db
    N.B. available using `npm install prisma`
    CLI

    View Slide

  9. npx prisma init --datasource-provider PostgreSQL
    Let’s start

    View Slide

  10. • PostgreSQL
    • MySQL
    • SQLite
    • Microsoft SQL Server
    • MongoDB (April 2022)
    • CockroachDB (Preview)
    Providers

    View Slide

  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

    View Slide

  12. • Field Types
    • Primary Keys
    • Foreign Keys
    • Constraints
    • Table name
    N.B. more detail about providers in the
    officials docs
    Schema

    View Slide

  13. Convert schema to ts types
    npx prisma generate
    By default types are generated in the folder `node_modules/.prisma/client`
    Generate

    View Slide

  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

    View Slide

  15. Prisma Client is an auto-generated and type-safe query builder that's tailored to
    your data.
    npm install @prisma/client
    Client

    View Slide

  16. Client

    View Slide

  17. Operations
    • create/createMany
    • update/updateMany
    • delete/deleteMany
    • findUnique/findMany/findFirst
    • aggregate
    • count
    • groupBy
    • upsert (create or update)
    One for each model
    Client

    View Slide

  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

    View Slide

  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

    View Slide

  20. • MongoDb is not supported (use `prisma db push`)
    • Switch DB Provider is not supported
    Migration - Limitations

    View Slide

  21. Introspection

    View Slide

  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

    View Slide

  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

    View Slide

  24. DEMO
    Demo

    View Slide

  25. Cons
    • Newby
    • Don’t manage multiple provider concurrency
    • Splitting schema not implemented out-of-the-box (prisma-aurora)
    Conclusion

    View Slide

  26. Pros
    • Wonderful developer experience
    • Type safe queries
    • Migrations
    • Custom queries
    • Active Community
    • Awesome documentation
    • Core team active in Github and Slack
    Conclusion

    View Slide

  27. My Series about Prisma
    https://dev.to/this-is-learning/series/15827

    View Slide

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

    View Slide

  29. www.delpuppo.net
    [email protected]
    @puppo92
    Luca Del Puppo
    Full-Stack developer @Flowing
    Contatti

    View Slide