Guest User

Untitled

a guest
Jul 8th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. import { NextResponse } from 'next/server';
  2. import pool from '../../../../lib/db';
  3. import mysql, { RowDataPacket } from 'mysql2/promise';
  4.  
  5. export async function GET() {
  6. let connection: mysql.PoolConnection | undefined;
  7. try {
  8. connection = await pool.getConnection();
  9. const [rows]: [RowDataPacket[], any] = await connection.query('SELECT COUNT(*) as count FROM guild');
  10. const count = rows[0].count;
  11. await connection.release();
  12. return NextResponse.json({ count }, { status: 200 });
  13. } catch (error) {
  14. console.error('Error fetching data:', error); // Log the error for debugging
  15. if (connection) {
  16. connection.release();
  17. return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
  18. }
  19. } finally {
  20. if (connection) {
  21. try {
  22. await connection.release(); // Release connection back to the pool
  23. console.log("Released")
  24. } catch (releaseError) {
  25. console.error('Error releasing connection:', releaseError); // Log release error
  26. }
  27. }
  28. }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment