1 minute read

Prisma Client Methods Overview

🔹 Basic CRUD Methods

Prisma Client automatically generates CRUD methods for each model (e.g., User, Post).

1. Read (Retrieve Data)

  • findUnique(): Retrieve a single record by a unique field (usually id).

    const user = await prisma.user.findUnique({
      where: { id: 1 },
    });
    
  • findFirst(): Retrieve the first record that matches the condition.

    const user = await prisma.user.findFirst({
      where: { email: "test@example.com" },
    });
    
  • findMany(): Retrieve multiple records.

    const users = await prisma.user.findMany({
      where: { role: "admin" },
      orderBy: { createdAt: "desc" },
      take: 10, // Retrieve the top 10 users
    });
    

2. Create (Insert Data)

  • create(): Insert a single record.

    const user = await prisma.user.create({
      data: {
        name: "John Doe",
        email: "john@example.com",
      },
    });
    
  • createMany(): Insert multiple records at once.

    await prisma.user.createMany({
      data: [
        { name: "Alice", email: "alice@example.com" },
        { name: "Bob", email: "bob@example.com" },
      ],
    });
    

3. Update (Modify Data)

  • update(): Update a specific record.

    const user = await prisma.user.update({
      where: { id: 1 },
      data: { name: "Updated Name" },
    });
    
  • updateMany(): Update multiple records that match a condition.

    await prisma.user.updateMany({
      where: { role: "user" },
      data: { role: "member" },
    });
    

4. Delete (Remove Data)

  • delete(): Delete a specific record.

    await prisma.user.delete({
      where: { id: 1 },
    });
    
  • deleteMany(): Delete multiple records based on a condition.

    await prisma.user.deleteMany({
      where: { role: "guest" },
    });
    

🔹 Advanced Methods

5. Upsert (Create or Update)

  • If a record exists, update it; otherwise, create a new one.

    const user = await prisma.user.upsert({
      where: { email: "john@example.com" },
      update: { name: "Updated John" },
      create: { name: "John", email: "john@example.com" },
    });
    

6. Aggregation (Counting, Summing, etc.)

  • count(): Count the number of records.

    const count = await prisma.user.count({
      where: { role: "admin" },
    });
    
  • sum() / avg() / min() / max(): Perform numerical aggregations.

    const stats = await prisma.user.aggregate({
      _sum: { age: true },
      _avg: { age: true },
    });
    

7. Grouping (Group By)

  • Group records based on a field.

    const result = await prisma.user.groupBy({
      by: ["role"],
      _count: { id: true },
      _avg: { age: true },
    });
    

8. Transactions

  • prisma.$transaction(): Execute multiple queries within a transaction.

    await prisma.$transaction([
      prisma.user.create({ data: { name: "Alice" } }),
      prisma.post.create({ data: { title: "Hello World", userId: 1 } }),
    ]);
    

9. Raw SQL Execution

  • prisma.$queryRaw() / prisma.$executeRaw()

    const result =
      await prisma.$queryRaw`SELECT * FROM User WHERE role = 'admin'`;