Fireship · @fireship

Prisma in 100 Seconds

Uploaded January 26, 2022 · Length 2:34

Prisma in 100 Seconds

Summary

prisma explained as an orm for modern app development with sql and nodejs

  • prisma
  • orm
  • sql
  • nodejs
  • database
  • relational database

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    prisma an open source tool that makes it

  2. 0:02

    fun and safe to work with your database

  3. 0:04

    the relational database is ancient

  4. 0:06

    technology that has stood the test of

  5. 0:08

    time but writing sql code is not an

  6. 0:10

    ideal abstraction for modern app

  7. 0:12

    development it provides too much low

  8. 0:14

    level control and you'll probably blow

  9. 0:15

    your foot off writing it to address this

  10. 0:17

    concern orms like sqlize implement

  11. 0:20

    object relational mapping to express

  12. 0:22

    data with object oriented code like

  13. 0:24

    javascript classes prisma is also an orm

  14. 0:26

    that addresses similar problems but

  15. 0:28

    instead it has its own declarative

  16. 0:30

    schema definition language you write a

  17. 0:32

    schema to express your data and

  18. 0:34

    relationships in a human readable way or

  19. 0:36

    the schema can be automatically inferred

  20. 0:38

    from any existing database prisma then

  21. 0:40

    converts the schema into type

  22. 0:42

    definitions allowing you to perform

  23. 0:43

    typesafe crud operations with your own

  24. 0:45

    database models which means you get ide

  25. 0:47

    autocompletion tailored to your own

  26. 0:49

    custom schema in addition it handles

  27. 0:51

    migrations as your data structure

  28. 0:53

    evolves and if you want to visualize

  29. 0:54

    your data prisma studio allows you to

  30. 0:56

    view and manage all of your tables and

  31. 0:58

    rows in the browser to get started

  32. 1:00

    you'll need a relational database like

  33. 1:02

    mysql or postgres although mongodb is

  34. 1:04

    supported as well from a nodejs project

  35. 1:07

    run npx prisma init this creates a env

  36. 1:10

    file where you can add your database url

  37. 1:12

    and a prisma directory to define the

  38. 1:14

    schema if the existing database already

  39. 1:16

    has data we can run prismadb pool to

  40. 1:19

    introspect it and automatically generate

  41. 1:21

    the schema from it notice how each table

  42. 1:23

    is represented with the model keyword

  43. 1:24

    then inside the braces we have column

  44. 1:26

    names and their data types as well as

  45. 1:28

    optional constraints like for example we

  46. 1:31

    might use relation to create a foreign

  47. 1:33

    key constraint between a weapon and a

  48. 1:35

    human and we can also index the weapon

  49. 1:37

    id while we're at it the beauty of this

  50. 1:38

    is that the code is significantly more

  51. 1:40

    concise than what you would write

  52. 1:41

    otherwise in raw sql now if we decide to

  53. 1:44

    modify our schema like add an extra

  54. 1:46

    timestamp column we'll also need to

  55. 1:47

    modify the database structure which can

  56. 1:49

    be handled automatically with the prisma

  57. 1:51

    migrate command now to interact with the

  58. 1:53

    database on the server we tell prisma to

  59. 1:55

    generate the client library import the

  60. 1:57

    client into a javascript file and notice

  61. 1:59

    how it auto-completes the models in the