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
I migrated to drizzle-orm from type-orm recently but now my app crashes when mysql database connection is idle for a while. I didn't encounter this with any other orm before. According to documentation, I tought drizzle manages opening and closing connections but it seems to be connecting only once. I came up with below approach but I'm not sure if this is a good practice. any ideas would be highly appreciated.
//* In-memory store for database connectionsconststore=newMap<string,mysql2.Connection>();//* Utility function for database connection handlingasyncfunctioncreateConnection(dbName: string){constexconn=store?.get(dbName);if(exconn&&(awaitping(exconn))){returnexconn;}constconn=awaitmysql2.createConnection({ ...masterDbOptions,database: dbName});store.set(dbName,conn);returnconn;}//* Function to establish shared database connectionexportconstsharedDb=async()=>{if(!zenv?.DATABASE_NAME)returnnull;constconnection=awaitcreateConnection(zenv.DATABASE_NAME);returndrizzle(connection,{ schema,mode: 'default'});};//* Function to establish tenant database connection with authorizationexportconsttenantDb=async({ request, permissions }: {request: Request;permissions?: UserPermissions[]})=>{const{ user, tenant }=awaitauthenticate(request,{ permissions });constconnection=awaitcreateConnection(`tenant_${tenant}`);constconn=drizzle(connection,{schema: tenancySchema,mode: 'default'})asMySql2Database<typeoftenancySchema>&{ctx: {user: string;tenant: string}};conn.ctx={user: user.username, tenant };returnconn;};//* ping function to check if server is aliveconstping=async(conn: mysql2.Connection): Promise<boolean>=>conn?.ping().then(()=>true).catch(()=>false);
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
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I migrated to drizzle-orm from type-orm recently but now my app crashes when mysql database connection is idle for a while. I didn't encounter this with any other orm before. According to documentation, I tought drizzle manages opening and closing connections but it seems to be connecting only once. I came up with below approach but I'm not sure if this is a good practice. any ideas would be highly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions