1
1
import camelcaseKeys from "camelcase-keys" ;
2
2
import decamelize from "decamelize" ;
3
3
import { EventEmitter } from "events" ;
4
- import { Config , Migrator , Sql } from "knex" ;
4
+ import { Knex } from "knex" ;
5
+
5
6
import path from "path" ;
6
7
import { Gauge , Histogram , Registry } from "prom-client" ;
7
8
import { format as formatUrl } from "url" ;
@@ -10,7 +11,7 @@ import redactor from "url-auth-redactor";
10
11
export interface KnexClient extends EventEmitter {
11
12
// tslint:disable-next-line: no-any
12
13
client : { pool : any } ;
13
- migrate : Migrator ;
14
+ migrate : Knex . Migrator ;
14
15
}
15
16
16
17
export type Connection =
@@ -54,8 +55,8 @@ export interface KnexConfig {
54
55
directory : string ;
55
56
stub : string ;
56
57
} ;
57
- postProcessResponse ?: Config [ "postProcessResponse" ] ;
58
- wrapIdentifier ?: Config [ "wrapIdentifier" ] ;
58
+ postProcessResponse ?: Knex . Config [ "postProcessResponse" ] ;
59
+ wrapIdentifier ?: Knex . Config [ "wrapIdentifier" ] ;
59
60
}
60
61
61
62
interface Logger {
@@ -69,35 +70,35 @@ type StringTransform = (str: string) => string;
69
70
const poolUsedGauge = new Gauge ( {
70
71
name : "knex_pool_used" ,
71
72
help : "Number of non-free resources." ,
72
- registers : [ ]
73
+ registers : [ ] ,
73
74
} ) ;
74
75
const poolFreeGauge = new Gauge ( {
75
76
name : "knex_pool_free" ,
76
77
help : "Number of free resources." ,
77
- registers : [ ]
78
+ registers : [ ] ,
78
79
} ) ;
79
80
const poolPendingAcquiresGauge = new Gauge ( {
80
81
name : "knex_pool_pending_acquires" ,
81
82
help : "How many acquires are waiting for a resource to be released." ,
82
- registers : [ ]
83
+ registers : [ ] ,
83
84
} ) ;
84
85
const poolPendingCreatesGauge = new Gauge ( {
85
86
name : "knex_pool_pending_creates" ,
86
87
help : "How many asynchronous create calls are running." ,
87
- registers : [ ]
88
+ registers : [ ] ,
88
89
} ) ;
89
90
const queryDuration = new Histogram ( {
90
91
name : "knex_query_duration_seconds" ,
91
92
help : "Knex sql query durations in seconds" ,
92
93
labelNames : [ "method" ] ,
93
- registers : [ ]
94
+ registers : [ ] ,
94
95
} ) ;
95
96
96
97
export function createConfig ( opts : ConfigOptions ) : KnexConfig {
97
98
const {
98
99
connection,
99
100
client = "pg" ,
100
- migrationsDirectory = "./lib/postgres/migrations"
101
+ migrationsDirectory = "./lib/postgres/migrations" ,
101
102
} = opts ;
102
103
103
104
if ( ! connection ) {
@@ -109,13 +110,15 @@ export function createConfig(opts: ConfigOptions): KnexConfig {
109
110
connection,
110
111
pool : {
111
112
min : 2 ,
112
- max : 10
113
+ max : 10 ,
113
114
} ,
114
115
migrations : {
115
116
directory : migrationsDirectory ,
116
- stub : path . join ( __dirname , "./_migration-template.js" )
117
+ stub : path . join ( __dirname , "./_migration-template.js" ) ,
117
118
} ,
118
- postProcessResponse : ( result : unknown /*, queryContext*/ ) => {
119
+ postProcessResponse : (
120
+ result ?: { [ key : string ] : unknown } | null /*, queryContext*/
121
+ ) => {
119
122
if ( typeof result !== "object" || result === null ) {
120
123
return result ;
121
124
}
@@ -124,7 +127,7 @@ export function createConfig(opts: ConfigOptions): KnexConfig {
124
127
wrapIdentifier : (
125
128
value : string ,
126
129
origImpl : StringTransform /*, queryContext*/
127
- ) => origImpl ( decamelize ( value ) )
130
+ ) => origImpl ( decamelize ( value ) ) ,
128
131
} ;
129
132
}
130
133
@@ -147,12 +150,12 @@ export async function setup(
147
150
148
151
// Request time logging
149
152
dbClient
150
- . on ( "query" , ( query : Sql ) => {
153
+ . on ( "query" , ( query : Knex . Sql ) => {
151
154
if ( query . bindings ) {
152
155
queryStartTimes . set ( query . bindings , process . hrtime ( ) ) ;
153
156
}
154
157
} )
155
- . on ( "query-response" , ( result : unknown , query : Sql ) => {
158
+ . on ( "query-response" , ( result : unknown , query : Knex . Sql ) => {
156
159
const responseTime = process . hrtime ( queryStartTimes . get ( query . bindings ) ) ;
157
160
158
161
const ms = toMilliseconds ( responseTime ) ;
@@ -229,7 +232,7 @@ export function registerMetrics(registry: Registry) {
229
232
}
230
233
231
234
const delay = ( timeout : number ) =>
232
- new Promise ( resolve => setTimeout ( resolve , timeout ) ) ;
235
+ new Promise ( ( resolve ) => setTimeout ( resolve , timeout ) ) ;
233
236
234
237
export function formatConnection ( connection : Connection ) {
235
238
if ( typeof connection === "string" ) {
@@ -248,7 +251,7 @@ export function formatConnection(connection: Connection) {
248
251
auth,
249
252
host : connection . host || "127.0.0.1" ,
250
253
port : connection . port ,
251
- pathname : connection . database
254
+ pathname : connection . database ,
252
255
} ) ;
253
256
}
254
257
}
0 commit comments