porsager / postgres

Postgres.js - The Fastest full featured PostgreSQL client for Node.js, Deno, Bun and CloudFlare
The Unlicense
7.41k stars 270 forks source link

Query hangs when used as express middleware #941

Open ext4cats opened 1 month ago

ext4cats commented 1 month ago

I've been trying to use this library as an express middleware, for dependency injection convenience.

But I ran into a roadblock; queries get stuck seemingly forever when I do this. I am not sure why this happens? I've tested the code over and over and the issue persists. I moved off my production project to try reproducing it and was able to do it.

Here's some sample code, running on Node LTS with Postgres.js 3.4.4 and Express 4.20.0.

import express from 'express';
import postgres from 'postgres';

function createExpressApp() {
  const app = express();
  app.use(databaseMiddleware());
  app.get('/', testHandler);
  return app;
}

const databaseMiddleware = () => {
  const sql = postgres(
    'postgresql://postgres:postgres@localhost:8080/postgres',
  );
  return (req, res, next) => {
    req.sql = sql;
    next();
  };
};

const testHandler = async (req, res, next) => {
  try {
    const response = await req.sql`SELECT version()`;
    res.status(200).json(response);
  } catch (err) {
    next(err);
  }
};

createExpressApp().listen(8080, () => console.log("listenin'"));

I hope this gets fixed if it really is a bug; feels like a shame to not be able to use such a nice library because of such an obnoxious issue.

ext4cats commented 1 month ago

The closest related issue I could find is #747, which was posted November last year and is still open. Gotta say, it doesn't give me a lot of hope...