```error: sorry, too many clients already``` can i...
# orm-help
s
Copy code
error: sorry, too many clients already
can i expect this in production or its a dev only bug? i know it's as a result of nextjs'
fast-refresh
which only occurs in the dev environment ... but i'm just sking to be sure.
r
@Samson Ssali 👋 Yes this will only be in dev and you can check out the workaround for that here 🙂
s
Incase the database connection is already exhausted (as my current situation is), what's the solution? Tried deleting the database and I am being it's being used by 99 other sessions
r
Creating a new database should work fine. Also try removing the connections or running processes currently and start the app again.
s
I did. Also restarted my computer. However I get this error on every query to the database:
Copy code
Cannot read property 'findUnique' of undefined:
r
Could you run
prisma generate
and check again?
s
after
yarn prisma generate
same error
r
Is
prisma
available in the context? Could you log that and check?
s
That's the bug... it's an empty object
Copy code
// prisma/index.ts
import { PrismaClient } from '@prisma/client'
declare global {
  var prisma: PrismaClient
}
let prisma: PrismaClient
// check to use this workaround only in development and not in production
if (process.env.NODE_ENV === 'production') {
  prisma = new PrismaClient()
} else {
  if (!global.prisma) {
    global.prisma = new PrismaClient()
  }
  prisma = global.prisma
}
export default prisma
then
Copy code
// nexus/context.ts

require('dotenv').config()

// import { PubSub } from 'apollo-server'

import { PostgresPubSub } from "graphql-postgres-subscriptions";
import { Client } from "pg";
import { NextApiRequest, NextApiResponse } from 'next'
import jwt from 'jsonwebtoken'
import cookie from 'cookie'
import { PrismaClient } from "@prisma/client";
import prisma from 'prisma'



console.log(prisma)




const jwtRefreshToken = process.env.JWT_REFRESH_TOKEN
const jwtAuthToken = process.env.JWT_AUTH_TOKEN


export interface Context{
  prisma: PrismaClient;
  pubsub: PostgresPubSub;
  req: NextApiRequest;
  res: NextApiResponse;
  phone?: string;
}



// const pubsub = new PubSub()

 const connectionString = process.env.DATABASE_URL;

 const client = new Client({
  connectionString: connectionString
});
client.connect();

const pubsub = new PostgresPubSub({ client }); // const pubsub = new PubSub()


export const context = async({ req, res }): Promise<Context> => {
 
  const {accessToken,refreshToken} =req.cookies
  
  // refresh jwt
  jwt.verify(refreshToken, jwtRefreshToken, (err, decoded) => {
 
    if (!err&&decoded) {
      const { data } = (decoded)
      const accessToken = jwt.sign({data},jwtAuthToken,{expiresIn:'5m'})
      res.setHeader('Set-Cookie', cookie.serialize('accessToken', accessToken,{
        path:"/",
        maxAge: 5 * 60,
        sameSite: 'strict',
        httpOnly: true,
        secure:process.env.NODE_ENV!=='development'
      }))
     
    } else {
      res.setHeader('Set-Cookie', cookie.serialize('accessToken', '',{
        path:"/",
        expires: new Date(0),
        sameSite: 'strict',
        httpOnly: true,
        secure:process.env.NODE_ENV!=='development'
      }))
    }
    
  })
  
  // authenticate
 let phone=''
  jwt.verify(accessToken, jwtAuthToken, async (err, decoded) => {
    if (!err&&decoded) {
      const { data } = (decoded)
      phone=data
     }
  })

  
  return{ prisma, pubsub, req, res,phone }
}
not sure why it's an empty object yet i replicated the code in the work around almost exactly
r
Could you check if the context is being passed correctly? If
prisma
is defined where you are returning the context? We also have an example that does the same.
s
I had to do this to solve it:
Copy code
declare global {
  var prisma: PrismaClient
}



if (process.env.NODE_ENV === 'production') {
  prisma = new PrismaClient()
} else {
  if (!global.prisma) {
    global.prisma = new PrismaClient()
  }
  prisma = global.prisma
}
i think the issue was the
prisma
scope. i was declaring it in multiple scopes ... and at some point it assumed a value of
{}
so here i only declare it in the global scope and then initialize it accordingly