#Executing functions on Nuxt start and injecting properties to Nitro context

6 messages · Page 1 of 1 (latest)

lethal shoal
#

Does somebody knows how to do something like but in Nuxt?:

`
// index.js
import express from 'express'
import { PrismaClient } from '@prisma/client'

// How initialize a PrismaClient before Nuxt start?
// Or how initialize it just at the very firsts of the Nuxt loading
// I want to load DB and then do a function at the first running of the App
const prisma = new PrismaClient() // Initialize prisma and inject it to the Nitro context so can be used in Api routes
await refreshUsersOnlineStates(prisma); // Use Prisma and execute this function always on Nuxt start

// Nuxt/Nitro starts
const app = express()
app.use(express.json())
`

I tried server/plugins but I cannot later inject properties to the event.context

Thanks.

broken yoke
lethal shoal
#

Yes, I´ve seen that module, but I cannot find a way to persist the Prisma connection in that level without calling Prism again.

#

Can somebody help me confirm if this is safe to do please?:

`
// server/plugins/test.ts

import { getV } from '../V'

export default defineNitroPlugin((nitroApp) => {
console.log('Nitro plugin', getV())
})
`

`
// V.ts

let V = 0;
export const getV = () => {
if(!V){
console.log('def')
V= 123
}
return V
}
`

So in API routes I could use:
`
// api/test.get.post

import { getV } '../V.ts'

export default defineEventHandler(() => {
var V = getV();
....
});
`

So V (like a PrismaClient instance) is not re defined again, for now it's working ok but not sure if this is safe at all.

high glacier