Getting Started with Prisma

Here’s a short tutorial on how to get started with Prisma:

Step 1: Installation

To use Prisma in your Node.js project, you need to install it first. You can do this by running the following command in your terminal:

cssCopy codenpm install prisma --save

Step 2: Database Setup

Next, you need to create a database and set up your database schema. Prisma supports a variety of databases, including MySQL, PostgreSQL, SQLite, and MongoDB. You can set up your database and schema using the Prisma CLI.

To create a new Prisma project, run the following command:

csharpCopy codenpx prisma init

This will create a new directory called prisma in your project and generate a schema.prisma file.

In the schema.prisma file, you can define your database schema using Prisma’s schema language. Here’s an example schema for a simple blog application:

prismaCopy codedatasource db {
  provider = "postgresql"
  url      = "postgresql://user:password@localhost:5432/myblog"
}

generator client {
  provider = "prisma-client-js"
}

model Post {
  id        Int     @id @default(autoincrement())
  title     String
  content   String
  createdAt DateTime @default(now())
  author    User    @relation(fields: [authorId], references: [id])
  authorId  Int
}

model User {
  id        Int     @id @default(autoincrement())
  name      String
  email     String  @unique
  password  String
  posts     Post[]
}

In this schema, we define two models: Post and User. The Post model has an id, title, content, createdAt, and authorId. The User model has an id, name, email, password, and a relation to the Post model.

Once you’ve defined your schema, you can use the Prisma CLI to migrate the database and generate a Prisma client.

Step 3: Generate Prisma Client

To generate a Prisma client, run the following command in your terminal:

Copy codenpx prisma generate

This will generate a Prisma client based on your schema, which you can use to interact with your database.

Step 4: Using the Prisma Client

Now that you have a Prisma client, you can use it to interact with your database. Here’s an example of how to create a new post using the Prisma client:

javascriptCopy codeconst { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()

async function main() {
  const newPost = await prisma.post.create({
    data: {
      title: 'My First Post',
      content: 'This is my first post using Prisma',
      author: {
        connect: { id: 1 }
      }
    }
  })
  console.log('Created new post:', newPost)
}

main()
  .catch(e => {
    console.error(e)
    process.exit(1)
  })
  .finally(async () => {
    await prisma.$disconnect()
  })

In this code, we create a new post using the prisma.post.create method, passing in the post data as an object. We also connect the post to the author by specifying the authorId.

Conclusion

Prisma is a powerful and flexible ORM tool that simplifies database access and management for Node.js and TypeScript applications.

Tags: No tags

Add a Comment

Your email address will not be published. Required fields are marked *