pgtyped icon indicating copy to clipboard operation
pgtyped copied to clipboard

Suggestion: include query name in `PreparedQuery` type

Open PhilipTrauner opened this issue 1 year ago • 4 comments

Hey @adelsz, would you be open to a PR that adds a queryName field to PreparedQuery?

This would be great for logging purposes, as the name of a query is really useful for debugging.

There would only be two required changes:

  • add the queryName field to PreparedQuery
  • substitute in an escape version of typeDec.query.name in generateDeclarationFile

PhilipTrauner avatar May 10 '23 14:05 PhilipTrauner

This is something I would definitely use

timvandam avatar May 27 '23 13:05 timvandam

Any update on this? Would be a great feature.

KieranKaelin avatar Aug 11 '23 13:08 KieranKaelin

Just created #546 for consideration to implement this.

As additional context, before 3c99d10f5cea4e12d889ea91717adc400116cfe9 (in v1.0.0), the query name was included in the (private) QueryAST member on PreparedQuery. It was hacky to access it, but it was handy for a logging wrapper. With the new code, something along these lines:

const runQuery = async <TParamType, TResultType>(
  query: PreparedQuery<TParamType, TResultType>,
  params: TParamType
): Promise<Array<TResultType>> => {
  const startTime = new Date();

  try {
    return await query.run(params, pool);
  } finally {
    const time = new Date().getTime() - startTime.getTime();
    logger.info(
      { query: { name: query.queryName, time, params } },
      'Database query completed'
    );
  }
};

It would be great to have this ability again :smile:

logan12358 avatar Nov 01 '23 03:11 logan12358

Was also looking for this feature 👍

jareware avatar Jul 01 '24 14:07 jareware