From 7542b715adb45900a98e604970beaef2e23509ba Mon Sep 17 00:00:00 2001 From: Pl217 Date: Mon, 26 Aug 2024 14:58:20 +0200 Subject: [PATCH] Upgrade Knex to `v3.1.0` The migration guide can be found here https://github.com/knex/knex/blob/176151d8048b2a7feeb89a3d649a5580786d4f4e/UPGRADING.md In section "Upgrading to version 0.95.0+", the only change that affects us is the import style change. Other sections, regarding upgrades to higher major versions of Knex, don't have anything that affects us. --- package.json | 2 +- src/data-providers/postgres/index.ts | 8 ++--- src/server.ts | 5 ++- tests/testContext.ts | 2 +- tests/utils/connection.ts | 8 ++--- tests/utils/server.ts | 2 +- yarn.lock | 51 ++++++++++++++++++++++++++-- 7 files changed, 62 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index ecb5e9d1..16e72dfc 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "bunyan": "^1.8.15", "class-validator": "^0.14.0", "graphql": "^15.7.2", - "knex": "0.21.1", + "knex": "3.1.0", "pg": "^8.11.3", "pm2": "^5.3.0", "reflect-metadata": "^0.1.13", diff --git a/src/data-providers/postgres/index.ts b/src/data-providers/postgres/index.ts index 66eefcc3..587fcaba 100644 --- a/src/data-providers/postgres/index.ts +++ b/src/data-providers/postgres/index.ts @@ -1,11 +1,11 @@ -import Knex from 'knex'; +import { knex } from 'knex'; import { CONFIG } from '../../../config'; /** * Initialize a new Postgres provider */ export async function createDbConnection() { - const knex = Knex({ + const knexInstance = knex({ client: 'pg', connection: CONFIG.db.connection, pool: { @@ -18,9 +18,9 @@ export async function createDbConnection() { // Verify the connection before proceeding try { - await knex.raw('SELECT now()'); + await knexInstance.raw('SELECT now()'); - return knex; + return knexInstance; } catch { throw new Error( 'Unable to connect to Postgres via Knex. Ensure a valid connection.' diff --git a/src/server.ts b/src/server.ts index f8259132..0cc4a9fb 100644 --- a/src/server.ts +++ b/src/server.ts @@ -1,16 +1,15 @@ import * as Hapi from '@hapi/hapi'; +import v4Models from '@unocha/hpc-api-core/src/db'; import { ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core'; import { ApolloServer, ApolloServerPluginStopHapiServer, } from 'apollo-server-hapi'; +import { type Knex } from 'knex'; import { join } from 'node:path'; import 'reflect-metadata'; import { buildSchema } from 'type-graphql'; import { Container } from 'typedi'; -import Knex = require('knex'); - -import v4Models from '@unocha/hpc-api-core/src/db'; import { CONFIG } from '../config'; import { getTokenFromRequest } from './common-libs/auth'; import { initializeLogging } from './common-libs/logging'; diff --git a/tests/testContext.ts b/tests/testContext.ts index 30d530ee..eeba324e 100644 --- a/tests/testContext.ts +++ b/tests/testContext.ts @@ -1,6 +1,6 @@ import v4Models, { type Database } from '@unocha/hpc-api-core/src/db'; import { type ApolloServer } from 'apollo-server-hapi'; -import type Knex from 'knex'; +import type { Knex } from 'knex'; import { createDbConnection } from './utils/connection'; import createApolloTestServer from './utils/server'; diff --git a/tests/utils/connection.ts b/tests/utils/connection.ts index ff67e7ca..82a0cdd3 100644 --- a/tests/utils/connection.ts +++ b/tests/utils/connection.ts @@ -1,5 +1,5 @@ import * as t from 'io-ts'; -import Knex from 'knex'; +import { knex } from 'knex'; const CONFIG = t.type({ host: t.string, @@ -12,7 +12,7 @@ const CONFIG = t.type({ * Initialize a new Postgres provider */ export async function createDbConnection(connection: t.TypeOf) { - const knex = Knex({ + const knexInstance = knex({ client: 'pg', connection, pool: { min: 0, max: 10, idleTimeoutMillis: 500 }, @@ -21,9 +21,9 @@ export async function createDbConnection(connection: t.TypeOf) { // Verify the connection before proceeding try { - await knex.raw('SELECT now()'); + await knexInstance.raw('SELECT now()'); - return knex; + return knexInstance; } catch (error) { console.log(error); throw new Error( diff --git a/tests/utils/server.ts b/tests/utils/server.ts index b18161bd..d8f3f7d3 100644 --- a/tests/utils/server.ts +++ b/tests/utils/server.ts @@ -5,7 +5,7 @@ import { ApolloServer, ApolloServerPluginStopHapiServer, } from 'apollo-server-hapi'; -import type Knex from 'knex'; +import type { Knex } from 'knex'; import PlatformPath from 'node:path'; import 'reflect-metadata'; import { buildSchema } from 'type-graphql'; diff --git a/yarn.lock b/yarn.lock index 8ba5f62e..d07af7cd 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2361,6 +2361,11 @@ colorette@1.1.0: resolved "https://registry.yarnpkg.com/colorette/-/colorette-1.1.0.tgz#1f943e5a357fac10b4e0f5aaef3b14cdc1af6ec7" integrity sha512-6S062WDQUXi6hOfkO/sBPVwE5ASXY4G2+b4atvhJfSsuUUhIaUKlkjLe9692Ipyt5/a+IPF5aVTu3V5gvXq5cg== +colorette@2.0.19: + version "2.0.19" + resolved "https://registry.yarnpkg.com/colorette/-/colorette-2.0.19.tgz#cdf044f47ad41a0f4b56b3a0d5b4e6e1a2d5a798" + integrity sha512-3tlv/dIP7FWvj3BsbHrGLJ6l/oKh1O3TcgBqMn+yyCagOxc23fyzDS6HypQbgxWbkpDnf52p1LuR4eWDQ/K9WQ== + colorette@^2.0.20: version "2.0.20" resolved "https://registry.yarnpkg.com/colorette/-/colorette-2.0.20.tgz#9eb793e6833067f7235902fcd3b09917a000a95a" @@ -2383,6 +2388,11 @@ commander@2.15.1: resolved "https://registry.yarnpkg.com/commander/-/commander-2.15.1.tgz#df46e867d0fc2aec66a34662b406a9ccafff5b0f" integrity sha512-VlfT9F3V0v+jr4yxPc5gg9s62/fIVWsd2Bk2iD435um1NlGMYdVCq+MjcXnhYq2icNOizHr1kK+5TI6H0Hy0ag== +commander@^10.0.0: + version "10.0.1" + resolved "https://registry.yarnpkg.com/commander/-/commander-10.0.1.tgz#881ee46b4f77d1c1dccc5823433aa39b022cbe06" + integrity sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug== + commander@^2.20.3: version "2.20.3" resolved "https://registry.yarnpkg.com/commander/-/commander-2.20.3.tgz#fd485e84c03eb4881c20722ba48035e8531aeb33" @@ -3248,6 +3258,11 @@ getopts@2.2.5: resolved "https://registry.yarnpkg.com/getopts/-/getopts-2.2.5.tgz#67a0fe471cacb9c687d817cab6450b96dde8313b" integrity sha512-9jb7AW5p3in+IiJWhQiZmmwkpLaR/ccTWdWQCtZM66HJcHHLegowh4q4tSD7gouUyeNvFWRavfK9GXosQHDpFA== +getopts@2.3.0: + version "2.3.0" + resolved "https://registry.yarnpkg.com/getopts/-/getopts-2.3.0.tgz#71e5593284807e03e2427449d4f6712a268666f4" + integrity sha512-5eDf9fuSXwxBL6q5HX+dhDj+dslFGWzU5thZ9kNKUkcPtaPdatmUFKwHFrLb/uf/WpA4BHET+AX3Scl56cAjpA== + git-node-fs@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/git-node-fs/-/git-node-fs-1.0.0.tgz#49b215e242ebe43aa4c7561bbba499521752080f" @@ -3580,7 +3595,7 @@ ini@^1.3.4, ini@^1.3.5: resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.8.tgz#a29da425b48806f34767a4efce397269af28432c" integrity sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew== -interpret@^2.0.0: +interpret@^2.0.0, interpret@^2.2.0: version "2.2.0" resolved "https://registry.yarnpkg.com/interpret/-/interpret-2.2.0.tgz#1a78a0b5965c40a5416d007ad6f50ad27c417df9" integrity sha512-Ju0Bz/cEia55xDwUWEa8+olFpCiQoypjnQySseKtmjNrnps3P+xfpUmGr90T7yjlVJmOtybRvPXhKMbHr+fWnw== @@ -4367,6 +4382,26 @@ knex@0.21.1: uuid "^7.0.3" v8flags "^3.1.3" +knex@3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/knex/-/knex-3.1.0.tgz#b6ddd5b5ad26a6315234a5b09ec38dc4a370bd8c" + integrity sha512-GLoII6hR0c4ti243gMs5/1Rb3B+AjwMOfjYm97pu0FOQa7JH56hgBxYf5WK2525ceSbBY1cjeZ9yk99GPMB6Kw== + dependencies: + colorette "2.0.19" + commander "^10.0.0" + debug "4.3.4" + escalade "^3.1.1" + esm "^3.2.25" + get-package-type "^0.1.0" + getopts "2.3.0" + interpret "^2.2.0" + lodash "^4.17.21" + pg-connection-string "2.6.2" + rechoir "^0.8.0" + resolve-from "^5.0.0" + tarn "^3.0.2" + tildify "2.0.0" + lazy@~1.0.11: version "1.0.11" resolved "https://registry.yarnpkg.com/lazy/-/lazy-1.0.11.tgz#daa068206282542c088288e975c297c1ae77b690" @@ -5070,7 +5105,7 @@ pg-connection-string@2.2.0: resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-2.2.0.tgz#caab4d38a9de4fdc29c9317acceed752897de41c" integrity sha512-xB/+wxcpFipUZOQcSzcgkjcNOosGhEoPSjz06jC89lv1dj7mc9bZv6wLVy8M2fVjP0a/xN0N988YDq1L0FhK3A== -pg-connection-string@^2.6.2: +pg-connection-string@2.6.2, pg-connection-string@^2.6.2: version "2.6.2" resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-2.6.2.tgz#713d82053de4e2bd166fab70cd4f26ad36aab475" integrity sha512-ch6OwaeaPYcova4kKZ15sbJ2hKb/VP48ZD2gE7i1J+L4MspCtBMAx8nMgz7bksc7IojCIIWuEhHibSMFH8m8oA== @@ -5485,6 +5520,13 @@ rechoir@^0.6.2: dependencies: resolve "^1.1.6" +rechoir@^0.8.0: + version "0.8.0" + resolved "https://registry.yarnpkg.com/rechoir/-/rechoir-0.8.0.tgz#49f866e0d32146142da3ad8f0eff352b3215ff22" + integrity sha512-/vxpCXddiX8NGfGO/mTafwjq4aFa/71pvamip0++IQk3zG8cbCj0fifNPrjjF1XMXUne91jL9OoxmdykoEtifQ== + dependencies: + resolve "^1.20.0" + reflect-metadata@^0.1.13: version "0.1.13" resolved "https://registry.yarnpkg.com/reflect-metadata/-/reflect-metadata-0.1.13.tgz#67ae3ca57c972a2aa1642b10fe363fe32d49dc08" @@ -6085,6 +6127,11 @@ tarn@^3.0.0: resolved "https://registry.yarnpkg.com/tarn/-/tarn-3.0.1.tgz#ebac2c6dbc6977d34d4526e0a7814200386a8aec" integrity sha512-6usSlV9KyHsspvwu2duKH+FMUhqJnAh6J5J/4MITl8s94iSUQTLkJggdiewKv4RyARQccnigV48Z+khiuVZDJw== +tarn@^3.0.2: + version "3.0.2" + resolved "https://registry.yarnpkg.com/tarn/-/tarn-3.0.2.tgz#73b6140fbb881b71559c4f8bfde3d9a4b3d27693" + integrity sha512-51LAVKUSZSVfI05vjPESNc5vwqqZpbXCsU+/+wxlOrUjk2SnFTt97v9ZgQrD4YmxYW1Px6w2KjaDitCfkvgxMQ== + test-exclude@^6.0.0: version "6.0.0" resolved "https://registry.yarnpkg.com/test-exclude/-/test-exclude-6.0.0.tgz#04a8698661d805ea6fa293b6cb9e63ac044ef15e"