I’m working on a Hono application using Prisma and the withAccelerate extension. I’m encountering a TypeScript error when trying to set the extended Prisma client in the context. Here is my code:
Environment :-
Hono: ^4.4.7
Prisma: ^5.15.1
Wrangler: ^3.57.2
Typescript: ^5.4.5
import {Hono} from 'hono'
import { PrismaClient, } from '@prisma/client/edge';
import { withAccelerate } from '@prisma/extension-accelerate';
import { sign } from 'hono/jwt';
type Variables = {
prisma: ?? // do not know what value it needs gave PrismaClient Initially
}
type User = {
username : string;
password : string;
name? : string;
}
const app = new Hono<{
Bindings : {
DATABASE_URL : string
JWT_SECRET : string
};
Variables : Variables
}>();
app.use('/*', async (c, next) =>{
const prisma : PrismaClient = new PrismaClient({
datasourceUrl : c.env.DATABASE_URL
}).$extends(withAccelerate());
console.log(`Return type of prisma is : ${typeof prisma}`);
await next();
})
app.post('/signup' ,async (c) => {
console.log(c.env.DATABASE_URL)
const prisma = new PrismaClient({
datasourceUrl : c.env.DATABASE_URL
}).$extends(withAccelerate());
try {
const body : User = await c.req.json();
const response = await prisma.user.create({
data : {
username : body.username,
password : body.password,
name : body.name
},
});
// console.log(`Response after singup is : ${JSON.stringify(response)}`);
// use jwt to sign uderid
const payload = {
userId : response.id,
}
const token : string = await sign(payload, c.env.JWT_SECRET);
// console.log(`Token generated is : ${token}`);
return c.json({
message : 'User created successfully',
data : response,
token : token
}, 200);
} catch (error) {
return c.json({
message : `error while signup!! User Already Exists`,
error : error
}, 409);
}
});
This is the error message I got
Error
I tried with this
type Variables = {
prisma: PrismaClient // since it is created by prismaclient
}
Also tried to log the prisma value, it gave me object and some prisma string
Also found this github issues with prisma, but according to my understanding it is still under investigation.
Issue #22050
I have different files for routing and want to create a new Prisma client for every global route, for example:
api/v1/user/*
api/v1/post/*
api/v1/blog/*
I thought that creating a middleware and setting the Prisma value there would help. Is there any other solution to pass Prisma globally, or am I doing something wrong here? Currently, I am passing Prisma in every route.
Rishikesh Solapure is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.