N
Nuxt8mo ago
Medim

Use prisma globally in api routes

I have a question, is it possible to use the prisma client globally without having to import it everytime? I'm using Prisma + Turso so there's a bit more than just two lines of code to use the prisma client so adding it to every api file is a bit tiring, is there a way to make it available as a util/helper globally? what is the best way to do it?
import { PrismaClient } from "@prisma/client"
const libsql = createClient({
url: `${tursoDatabaseUrl}`,
authToken: `${tursoAuthToken}`,
})

const adapter = new PrismaLibSQL(libsql)
const prisma = new PrismaClient({ adapter })
import { PrismaClient } from "@prisma/client"
const libsql = createClient({
url: `${tursoDatabaseUrl}`,
authToken: `${tursoAuthToken}`,
})

const adapter = new PrismaLibSQL(libsql)
const prisma = new PrismaClient({ adapter })
3 Replies
AGill
AGill8mo ago
exports in server/utils/ will be auto-imported. otherwise use the imports field in nuxt.config.ts to specify your own paths https://nuxt.com/docs/api/nuxt-config#imports
Nuxt
Nuxt Configuration
Discover all the options you can use in your nuxt.config.ts file.
Medim
MedimOP8mo ago
Nuxt
server/ · Nuxt Directory Structure
The server/ directory is used to register API and server handlers to your application.
Medim
MedimOP8mo ago
update: im using it as a server/utils, but instead of exporting a handler, i'm just exporting prisma itself :salute:
Want results from more Discord servers?
Add your server