Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why isn't the pool connection closed for every return path? #108

Open
KoTTi97 opened this issue Oct 13, 2024 · 0 comments
Open

Why isn't the pool connection closed for every return path? #108

KoTTi97 opened this issue Oct 13, 2024 · 0 comments

Comments

@KoTTi97
Copy link

KoTTi97 commented Oct 13, 2024

I just noticed that in the provided example for Vercel Edge functions, pool connections are not closed for every return path:

import { Pool } from '@neondatabase/serverless';

export default async (req: Request, ctx: any) => {
  const pool = new Pool({ connectionString: process.env.DATABASE_URL });
  const postId = parseInt(new URL(req.url).searchParams.get('postId'), 10);

  if (isNaN(postId)) {
    // --> pool connection is not closed here
    return new Response('Bad request', { status: 400 });
  }

  const [post] = await pool.query('SELECT * FROM posts WHERE id = $1', [postId]);

  if (!post) {
    // --> connection is not closed here either
    return new Response('Not found', { status: 404 });
  }

  ctx.waitUntil(pool.end());

  return new Response(JSON.stringify(post), {
    headers: { 'content-type': 'application/json' }
  });
}

export const config = {
  runtime: 'edge',
  regions: ['iad1'],  // specify the region nearest your Neon DB
};

I was wondering if there is a reason for this or if this may be a mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant