@@ -5,9 +5,15 @@ import { fill, isThenable, loadModule, logger } from '@sentry/utils';
55import type { LazyLoadedIntegration } from './lazy' ;
66import { shouldDisableAutoInstrumentation } from './utils/node-utils' ;
77
8+ type PgClientQuery = (
9+ config : unknown ,
10+ values ?: unknown ,
11+ callback ?: ( err : unknown , result : unknown ) => void ,
12+ ) => void | Promise < unknown > ;
13+
814interface PgClient {
915 prototype : {
10- query : ( ) => void | Promise < unknown > ;
16+ query : PgClientQuery ;
1117 } ;
1218}
1319
@@ -18,12 +24,17 @@ interface PgClientThis {
1824 user ?: string ;
1925}
2026
27+ type PGModule = { Client : PgClient ; native : { Client : PgClient } | null } ;
28+
2129interface PgOptions {
2230 usePgNative ?: boolean ;
31+ /**
32+ * Supply your postgres module directly, instead of having Sentry attempt automatic resolution.
33+ * Use this if you (a) use a module that's not `pg`, or (b) use a bundler that breaks resolution (e.g. esbuild).
34+ */
35+ module ?: PGModule ;
2336}
2437
25- type PGModule = { Client : PgClient ; native : { Client : PgClient } } ;
26-
2738/** Tracing integration for node-postgres package */
2839export class Postgres implements LazyLoadedIntegration < PGModule > {
2940 /**
@@ -43,6 +54,7 @@ export class Postgres implements LazyLoadedIntegration<PGModule> {
4354 public constructor ( options : PgOptions = { } ) {
4455 this . name = Postgres . id ;
4556 this . _usePgNative = ! ! options . usePgNative ;
57+ this . _module = options . module ;
4658 }
4759
4860 /** @inheritdoc */
@@ -66,21 +78,21 @@ export class Postgres implements LazyLoadedIntegration<PGModule> {
6678 return ;
6779 }
6880
69- if ( this . _usePgNative && ! pkg . native ?. Client ) {
81+ const Client = this . _usePgNative ? pkg . native ?. Client : pkg . Client ;
82+
83+ if ( ! Client ) {
7084 __DEBUG_BUILD__ && logger . error ( "Postgres Integration was unable to access 'pg-native' bindings." ) ;
7185 return ;
7286 }
7387
74- const { Client } = this . _usePgNative ? pkg . native : pkg ;
75-
7688 /**
7789 * function (query, callback) => void
7890 * function (query, params, callback) => void
7991 * function (query) => Promise
8092 * function (query, params) => Promise
8193 * function (pg.Cursor) => pg.Cursor
8294 */
83- fill ( Client . prototype , 'query' , function ( orig : ( ) => void | Promise < unknown > ) {
95+ fill ( Client . prototype , 'query' , function ( orig : PgClientQuery ) {
8496 return function ( this : PgClientThis , config : unknown , values : unknown , callback : unknown ) {
8597 const scope = getCurrentHub ( ) . getScope ( ) ;
8698 const parentSpan = scope . getSpan ( ) ;
0 commit comments