Advertisement
shawon_majid

database connection prisma

Oct 26th, 2023
799
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express')
  2. const { PrismaClient } = require('@prisma/client')
  3. const app = express();
  4.  
  5. app.use(express.json());
  6.  
  7. const prisma = new PrismaClient();
  8.  
  9.  
  10.  
  11.  
  12. app.get('/', async (req, res) => {
  13.     const allUsers = await prisma.User.findMany();
  14.     res.json(allUsers)
  15. })
  16.  
  17. app.post('/', async (req, res) => {
  18.     // console.log('hit')
  19.     const newUser = await prisma.User.create({ data: req.body })
  20.     res.json(newUser)
  21. })
  22.  
  23. app.put('/:id', async (req, res) => {
  24.     // console.log('hit')
  25.  
  26.     const id = req.params.id
  27.     const newName = req.body.firstName;
  28.  
  29.     const updatedUser = await prisma.User.update({
  30.         where: { id: parseInt(id) },
  31.         data: { firstName: newName }
  32.     })
  33.  
  34.     res.json(updatedUser)
  35.  
  36.  
  37. })
  38.  
  39.  
  40.  
  41.  
  42. app.listen(4141, () => console.log('server is listening on port 4141..'));
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement