Full-stack Web Technologies

CHAPTER 1
What is Prisma

Prisma is an ORM, i.e. an Object Relational Mapper. Prisma makes working with databases much easier because:

  • We can define a schema in a high-level language that works for multiple databases (Postgres, MySQL, SQLServer, SQLite, CockroachDB, MongoDB).
  • It generates type-safe client code in Typescript so that quering the database is easy and convenient.
  • It synchronizes schema changes to the database, and also to generates migration files.
  • It provides a user-interface for your database on top of the generated client so that querying and editing data is easier.

Project setup

The steps to prepare a new project for working with prisma are:

  1. Create a Bun project in a directory:
    mkdir prisma-test
    cd prisma-test
    bun init .
    
  2. Install Prisma:
    bun install -D prisma
    
  3. Set up Prisma with the init command:
    npx prisma init --datasource-provider sqlite
    

The Prisma directory

The Prisma setup creates a subdirectory in your package called prisma which will contain:

  • Your schema (prisma.schema).
  • Migrations (empty at the beginning).
  • The SQLite3 databases (if you use a relative path).

You should check this directory into Git.

Prisma in VSCode

The official Prisma extension for Visual Studio Code makes it much easier to work with .prisma files providing:

  • syntax highlighting,
  • formatting,
  • linting and autocompletion,
  • suggestions, and
  • jump-to-definition.