@@ -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
@@ -20,9 +26,23 @@ interface PgClientThis {
2026
2127interface PgOptions {
2228 usePgNative ?: boolean ;
29+ /**
30+ * Supply your postgres module directly, instead of having Sentry attempt automatic resolution.
31+ * Use this if you (a) use a module that's not `pg`, or (b) use a bundler that breaks resolution (e.g. esbuild).
32+ *
33+ * Usage:
34+ * ```
35+ * import pg from 'pg';
36+ *
37+ * Sentry.init({
38+ * integrations: [new Sentry.Integrations.Postgres({ module: pg })],
39+ * });
40+ * ```
41+ */
42+ module ?: PGModule ;
2343}
2444
25- type PGModule = { Client : PgClient ; native : { Client : PgClient } } ;
45+ type PGModule = { Client : PgClient ; native : { Client : PgClient } | null } ;
2646
2747/** Tracing integration for node-postgres package */
2848export class Postgres implements LazyLoadedIntegration < PGModule > {
@@ -43,6 +63,7 @@ export class Postgres implements LazyLoadedIntegration<PGModule> {
4363 public constructor ( options : PgOptions = { } ) {
4464 this . name = Postgres . id ;
4565 this . _usePgNative = ! ! options . usePgNative ;
66+ this . _module = options . module ;
4667 }
4768
4869 /** @inheritdoc */
@@ -66,21 +87,21 @@ export class Postgres implements LazyLoadedIntegration<PGModule> {
6687 return ;
6788 }
6889
69- if ( this . _usePgNative && ! pkg . native ?. Client ) {
90+ const Client = this . _usePgNative ? pkg . native ?. Client : pkg . Client ;
91+
92+ if ( ! Client ) {
7093 __DEBUG_BUILD__ && logger . error ( "Postgres Integration was unable to access 'pg-native' bindings." ) ;
7194 return ;
7295 }
7396
74- const { Client } = this . _usePgNative ? pkg . native : pkg ;
75-
7697 /**
7798 * function (query, callback) => void
7899 * function (query, params, callback) => void
79100 * function (query) => Promise
80101 * function (query, params) => Promise
81102 * function (pg.Cursor) => pg.Cursor
82103 */
83- fill ( Client . prototype , 'query' , function ( orig : ( ) => void | Promise < unknown > ) {
104+ fill ( Client . prototype , 'query' , function ( orig : PgClientQuery ) {
84105 return function ( this : PgClientThis , config : unknown , values : unknown , callback : unknown ) {
85106 const scope = getCurrentHub ( ) . getScope ( ) ;
86107 const parentSpan = scope . getSpan ( ) ;
0 commit comments