sidorares / sidorares/node-mysql2

Add stream query interface for promise based pool

Open
#2,252 0 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
4.4k
Forks
680
Avg merge
9h 7m
Merged PRs (30d)
59

Description

I'd like to propose adding a stream based query interface when using the promise based interface, that would act similarly as the stream method that exists on the Query interface.

For example, given the following code:

import mysql, { PoolConnection } from 'mysql2';

const pool = mysql.createPool({
  database: 'db',
  host: '127.0.0.1',
  multipleStatements: true,
  password: 'password',
  user: 'root',
});

(async () => {
  const connection = await new Promise<PoolConnection>((resolve, reject) => {
    pool.getConnection((err, conn) => {
      if (err) {
        reject(err);
      } else {
        resolve(conn);
      }
    });
  });
  const stream = connection
    .query({
      sql: 'SELECT 1',
    })
    .stream();

  stream.on('data', (data) => {
    console.log({ data });
  });

  await new Promise<void>((resolve, reject) => {
    stream.on('end', () => {
      resolve();
    });
    stream.on('error', (err) => {
      reject(err);
    });
  });
  connection.release();
})()
  .then(() => {
    console.log('done');
  })
  .catch((err) => {
    console.error(err);
  })
  .finally(() => {
    pool.end();
    process.exit(0);
  });

As far as I'm aware, there's no way then to rewrite this to use mysql2/promise instead as .query always returns a Promise. Given that I don't think that interface can change, I think it'd be good to see an additional queryStream method that works the same as query, but instead returns a Readable object, similar to query().stream().

Contributor guide

Open the contributing guide

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 promise-based pool query interface and its TypeScript definitions, then compare them with the Query interface and its existing stream method linked in the issue. Trace how promise queries are implemented and identify the tests covering pool queries. Done means the promise pool exposes a queryStream method returning a Readable with equivalent streaming behavior and appropriate type coverage.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, node.js, typescript
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.