Skip to content

Commit e7a33ac

Browse files
authored
Merge pull request #7 from LOKE/feature/upgrade-knex-v1
[ORD-526]upgrade knex version to v1
2 parents 4e6e6d8 + 1a80154 commit e7a33ac

File tree

5 files changed

+234
-1545
lines changed

5 files changed

+234
-1545
lines changed

Diff for: .github/workflows/npm-publish.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
on:
2+
push:
3+
branches:
4+
- main
5+
jobs:
6+
publish:
7+
runs-on: ubuntu-latest
8+
9+
steps:
10+
- uses: actions/checkout@v3
11+
- uses: actions/setup-node@v3
12+
with:
13+
node-version: 14.x
14+
- run: npm install
15+
- uses: JS-DevTools/npm-publish@v1
16+
with:
17+
token: ${{ secrets.NPM_TOKEN }}

Diff for: knex/index.ts

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import camelcaseKeys from "camelcase-keys";
22
import decamelize from "decamelize";
33
import { EventEmitter } from "events";
4-
import { Config, Migrator, Sql } from "knex";
4+
import { Knex } from "knex";
5+
56
import path from "path";
67
import { Gauge, Histogram, Registry } from "prom-client";
78
import { format as formatUrl } from "url";
@@ -10,7 +11,7 @@ import redactor from "url-auth-redactor";
1011
export interface KnexClient extends EventEmitter {
1112
// tslint:disable-next-line: no-any
1213
client: { pool: any };
13-
migrate: Migrator;
14+
migrate: Knex.Migrator;
1415
}
1516

1617
export type Connection =
@@ -54,8 +55,8 @@ export interface KnexConfig {
5455
directory: string;
5556
stub: string;
5657
};
57-
postProcessResponse?: Config["postProcessResponse"];
58-
wrapIdentifier?: Config["wrapIdentifier"];
58+
postProcessResponse?: Knex.Config["postProcessResponse"];
59+
wrapIdentifier?: Knex.Config["wrapIdentifier"];
5960
}
6061

6162
interface Logger {
@@ -69,35 +70,35 @@ type StringTransform = (str: string) => string;
6970
const poolUsedGauge = new Gauge({
7071
name: "knex_pool_used",
7172
help: "Number of non-free resources.",
72-
registers: []
73+
registers: [],
7374
});
7475
const poolFreeGauge = new Gauge({
7576
name: "knex_pool_free",
7677
help: "Number of free resources.",
77-
registers: []
78+
registers: [],
7879
});
7980
const poolPendingAcquiresGauge = new Gauge({
8081
name: "knex_pool_pending_acquires",
8182
help: "How many acquires are waiting for a resource to be released.",
82-
registers: []
83+
registers: [],
8384
});
8485
const poolPendingCreatesGauge = new Gauge({
8586
name: "knex_pool_pending_creates",
8687
help: "How many asynchronous create calls are running.",
87-
registers: []
88+
registers: [],
8889
});
8990
const queryDuration = new Histogram({
9091
name: "knex_query_duration_seconds",
9192
help: "Knex sql query durations in seconds",
9293
labelNames: ["method"],
93-
registers: []
94+
registers: [],
9495
});
9596

9697
export function createConfig(opts: ConfigOptions): KnexConfig {
9798
const {
9899
connection,
99100
client = "pg",
100-
migrationsDirectory = "./lib/postgres/migrations"
101+
migrationsDirectory = "./lib/postgres/migrations",
101102
} = opts;
102103

103104
if (!connection) {
@@ -109,13 +110,15 @@ export function createConfig(opts: ConfigOptions): KnexConfig {
109110
connection,
110111
pool: {
111112
min: 2,
112-
max: 10
113+
max: 10,
113114
},
114115
migrations: {
115116
directory: migrationsDirectory,
116-
stub: path.join(__dirname, "./_migration-template.js")
117+
stub: path.join(__dirname, "./_migration-template.js"),
117118
},
118-
postProcessResponse: (result: unknown /*, queryContext*/) => {
119+
postProcessResponse: (
120+
result?: { [key: string]: unknown } | null /*, queryContext*/
121+
) => {
119122
if (typeof result !== "object" || result === null) {
120123
return result;
121124
}
@@ -124,7 +127,7 @@ export function createConfig(opts: ConfigOptions): KnexConfig {
124127
wrapIdentifier: (
125128
value: string,
126129
origImpl: StringTransform /*, queryContext*/
127-
) => origImpl(decamelize(value))
130+
) => origImpl(decamelize(value)),
128131
};
129132
}
130133

@@ -147,12 +150,12 @@ export async function setup(
147150

148151
// Request time logging
149152
dbClient
150-
.on("query", (query: Sql) => {
153+
.on("query", (query: Knex.Sql) => {
151154
if (query.bindings) {
152155
queryStartTimes.set(query.bindings, process.hrtime());
153156
}
154157
})
155-
.on("query-response", (result: unknown, query: Sql) => {
158+
.on("query-response", (result: unknown, query: Knex.Sql) => {
156159
const responseTime = process.hrtime(queryStartTimes.get(query.bindings));
157160

158161
const ms = toMilliseconds(responseTime);
@@ -229,7 +232,7 @@ export function registerMetrics(registry: Registry) {
229232
}
230233

231234
const delay = (timeout: number) =>
232-
new Promise(resolve => setTimeout(resolve, timeout));
235+
new Promise((resolve) => setTimeout(resolve, timeout));
233236

234237
export function formatConnection(connection: Connection) {
235238
if (typeof connection === "string") {
@@ -248,7 +251,7 @@ export function formatConnection(connection: Connection) {
248251
auth,
249252
host: connection.host || "127.0.0.1",
250253
port: connection.port,
251-
pathname: connection.database
254+
pathname: connection.database,
252255
});
253256
}
254257
}

0 commit comments

Comments
 (0)