Express & Prisma【5Select Fields】

4858 ワード

YouTube: https://youtu.be/oQYbrKlm63I

prisma crud: https://www.prisma.io/docs/concepts/components/prisma-client/crud#select-a-subset-of-fields

prismaClient.ts
import { PrismaClient } from "@prisma/client";

export const prisma = new PrismaClient()
user.ts
import { Router } from "express";
import { prisma } from '../utils/prismaClient'

const router = Router()

router.get('/', async (_req, res) => {
  try {
    const users = await prisma.user.findMany({
      select: {
        id: true,
        email: true,
        name: true,
      }}
    )
    res.status(200).json(users)
  } catch (error) {
    res.status(500).json({"error": error})
  }
})

export default router