porsager / porsager/postgres

Query hangs when used as express middleware

Open
#941 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the provided Express reproduction, especially databaseMiddleware and the testHandler's awaited SELECT version() query. Investigate why this middleware setup leaves the query hanging, then verify the sample application returns the PostgreSQL version response instead of waiting indefinitely.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, javascript, node.js, postgresql
Domain
backend, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.