You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello! I have been connecting to multiple databases using plugins, with each connection pool attached to the Nitro context. Here’s a sample of my setup:
exportdefaultdefineNitroPlugin(async(nitroApp)=>{construntimeConfig=useRuntimeConfig();// MAIN Database Poolconstpool=newPG.Pool({user: runtimeConfig.db.user,host: runtimeConfig.db.host.main,database: 'postgres',password: runtimeConfig.db.password,port: 5432,max: 250,ssl: true,statement_timeout: 3*60*1000,maxUses: 10000,});// ... other pools// Test connectionstry{awaitpool.query('SELECT NOW()');awaitpool_meta.query('SELECT NOW()');}catch(error){console.error('🚨 Failed to connect to the database!');process.exit(1);}// Attach pools and Redis to request contextnitroApp.hooks.hook('request',(event)=>{event.context.pools={main: pool,meta: pool_meta,// ... other pools};event.context.redis=redis;});// Handle graceful shutdownnitroApp.hooks.hook('close',async()=>{console.log('-> Shutting down PG pools.');awaitpool.end();awaitpool_meta.end();console.log('-> Shutting down Redis.');awaitredis.quit();});});
This setup mostly works, but I occasionally encounter errors like "Cannot read properties of undefined (reading 'main')". I suspect it could be due to how the context is initialized or the way I’m attaching pools to requests.
Is there a better way to handle multi-database connections in Nitro, or a way to prevent this error? Any insights would be appreciated! Thanks!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hello! I have been connecting to multiple databases using plugins, with each connection pool attached to the Nitro context. Here’s a sample of my setup:
This setup mostly works, but I occasionally encounter errors like
"Cannot read properties of undefined (reading 'main')"
. I suspect it could be due to how the context is initialized or the way I’m attaching pools to requests.Is there a better way to handle multi-database connections in Nitro, or a way to prevent this error? Any insights would be appreciated! Thanks!
Beta Was this translation helpful? Give feedback.
All reactions