node-postgres: Add query name as query hash

18 Views Asked by At

From docs

PostgreSQL has the concept of a prepared statement. node-postgres supports this by supplying a name parameter to the query config object. If you supply a name parameter the query execution plan will be cached on the PostgreSQL server on a per connection basis.

Has some drawbacks set the query name as sql MD5 hash? eg.:

import { Pool, QueryConfig, QueryResult } from 'pg';
import crypto from 'crypto';

const pool = new Pool({ ... });

const query = <R, I extends any[] = any[]>(SQL: string, params?: I): Promise<Array<R> | null> => {
    const query: QueryConfig<I> = {
       text: SQL,
       values: params,
       name: crypto.createHash('md5').update(SQL, 'utf8').digest('hex')
    }
    const queryResult: QueryResult<R> = await pool.query<R, I>(query);
    return queryResult.rows;
}
0

There are 0 best solutions below