@@ -5,9 +5,15 @@ import { fill, isThenable, loadModule, logger } from '@sentry/utils';
5
5
import type { LazyLoadedIntegration } from './lazy' ;
6
6
import { shouldDisableAutoInstrumentation } from './utils/node-utils' ;
7
7
8
+ type PgClientQuery = (
9
+ config : unknown ,
10
+ values ?: unknown ,
11
+ callback ?: ( err : unknown , result : unknown ) => void ,
12
+ ) => void | Promise < unknown > ;
13
+
8
14
interface PgClient {
9
15
prototype : {
10
- query : ( ) => void | Promise < unknown > ;
16
+ query : PgClientQuery ;
11
17
} ;
12
18
}
13
19
@@ -18,12 +24,17 @@ interface PgClientThis {
18
24
user ?: string ;
19
25
}
20
26
27
+ type PGModule = { Client : PgClient ; native : { Client : PgClient } | null } ;
28
+
21
29
interface PgOptions {
22
30
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 ;
23
36
}
24
37
25
- type PGModule = { Client : PgClient ; native : { Client : PgClient } } ;
26
-
27
38
/** Tracing integration for node-postgres package */
28
39
export class Postgres implements LazyLoadedIntegration < PGModule > {
29
40
/**
@@ -43,6 +54,7 @@ export class Postgres implements LazyLoadedIntegration<PGModule> {
43
54
public constructor ( options : PgOptions = { } ) {
44
55
this . name = Postgres . id ;
45
56
this . _usePgNative = ! ! options . usePgNative ;
57
+ this . _module = options . module ;
46
58
}
47
59
48
60
/** @inheritdoc */
@@ -66,21 +78,21 @@ export class Postgres implements LazyLoadedIntegration<PGModule> {
66
78
return ;
67
79
}
68
80
69
- if ( this . _usePgNative && ! pkg . native ?. Client ) {
81
+ const Client = this . _usePgNative ? pkg . native ?. Client : pkg . Client ;
82
+
83
+ if ( ! Client ) {
70
84
__DEBUG_BUILD__ && logger . error ( "Postgres Integration was unable to access 'pg-native' bindings." ) ;
71
85
return ;
72
86
}
73
87
74
- const { Client } = this . _usePgNative ? pkg . native : pkg ;
75
-
76
88
/**
77
89
* function (query, callback) => void
78
90
* function (query, params, callback) => void
79
91
* function (query) => Promise
80
92
* function (query, params) => Promise
81
93
* function (pg.Cursor) => pg.Cursor
82
94
*/
83
- fill ( Client . prototype , 'query' , function ( orig : ( ) => void | Promise < unknown > ) {
95
+ fill ( Client . prototype , 'query' , function ( orig : PgClientQuery ) {
84
96
return function ( this : PgClientThis , config : unknown , values : unknown , callback : unknown ) {
85
97
const scope = getCurrentHub ( ) . getScope ( ) ;
86
98
const parentSpan = scope . getSpan ( ) ;
0 commit comments