Prisma: Quickstart guide - eodeluga/dev-notes GitHub Wiki

Getting started - Quickstart guide

Prerequisites

You need Node.js v14.17.0 or higher for this guide (learn more about system requirements) and postgreSQL

1. Create TypeScript project and set up Prisma

As a first step, create a project directory and navigate into it:

mkdir hello-prisma 
cd hello-prisma 

Next, initialize a TypeScript project using npm:

npm init -y 
npm install typescript ts-node @types/node --save-dev

This creates a package.json with an initial setup for your TypeScript app.

Now, create a tsconfig.json file in the root of your project:

touch tsconfig.json

With the tsconfig.json file in place, add the following configuration to it:

tsconfig.json
{
  "compilerOptions": {
    "sourceMap": true,
    "outDir": "dist",
    "strict": true,
    "lib": ["esnext"],
    "esModuleInterop": true
  }
}

Then, install the Prisma CLI as a development dependency in the project:

npm install prisma --save-dev

Finally, set up Prisma with the init command of the Prisma CLI:

npx prisma init --datasource-provider postgresql

This creates a new prisma directory with your Prisma schema file and configures postgresql as your database provider. It also creates a .env file containing the DATABASE_URL environment variable to store the database connection string.

2. Configure database access

There are two main ways to access the data in a database, and with that the next steps differ depending on whether you have an existing database or need to create one from scratch.

No existing database

  • Create a postgreSQL database

  • Edit the .env file as appropriate

    DATABASE_URL="postgresql://<username>:<password>@localhost:5432/<database_name>"

  • Model your data in the Prisma schema

    The Prisma schema provides an intuitive way to model data.

    Edit the prisma/prisma.schema file by adding a data model to it such as the user model as shown

    // This is your Prisma schema file,
    // learn more about it in the docs: https://pris.ly/d/prisma-schema
    
    generator client {
      provider = "prisma-client-js"
    }
    
    datasource db {
      provider = "postgresql"
      url      = env("DATABASE_URL")
    }
    
    model User {
      id Int @id @default(autoincrement())
      name String
    }
    

    Models in the Prisma schema have two main purposes:

    • Represent the tables in the underlying database
    • Serve as foundation for the generated Prisma Client API
  • Build database tables by running a schema migration

    At this point, you have a Prisma schema but an empty database.

    Run the following command to create database tables represented by the schema model.

    npx prisma migrate dev --name init

    This command did two things:

    • It creates a new SQL migration file for this migration in the prisma/migrations directory.
    • It runs the SQL migration file against the database.

    Because the Prisma database file didn't exist before, the command also created it inside the prisma directory with the name dev.db as defined via the environment variable in the .env file.

Database already exists

To create a prisma schema for an existing database, do the following.

  • Edit the .env file as appropriate

    DATABASE_URL="postgresql://<username>:<password>@localhost:5432/<database_name>"

  • Introspect the existing database to create a prisma data model of it (schema)

    npx prisma db pull

    This commands reads the DATABASE_URL environment variable that's defined in .env and connects to your database. Once the connection is established, it introspects the database (i.e. it reads the database schema). It then translates the database schema into a Prisma data model.

    After the introspection is complete, your Prisma schema file was updated:

3. Generate the prisma client

To interact with the database using code, a prisma client must now be installed and configured

npx prisma generate

This does three things:

  • Searches the current directory and parent directories to find the applicable npm project. It will create a package.json file in the current directory if it cannot find one.

  • Installs the @prisma/client into the npm project if it is not already present.

  • Inspects the current directory to find a Prisma schema file to process. It will then generate a customized Prisma Client for your project.

⚠️ **GitHub.com Fallback** ⚠️