diff --git a/src/server/db.js b/src/server/db.js index 785ede2..9b3b737 100644 --- a/src/server/db.js +++ b/src/server/db.js @@ -1,14 +1,21 @@ const mysql = require("mysql2/promise"); +let globalDb; + async function connectToDb() { - const db = await mysql.createConnection({ + if (globalDb) { + return globalDb; + } + + globalDb = await mysql.createPool({ + connectionLimit: 5, host: "impress.openneo.net", user: process.env["IMPRESS_MYSQL_USER"], password: process.env["IMPRESS_MYSQL_PASSWORD"], database: "openneo_impress", }); - return db; + return globalDb; } module.exports = connectToDb; diff --git a/src/server/index.test.js b/src/server/index.test.js index 269235c..ee1abf3 100644 --- a/src/server/index.test.js +++ b/src/server/index.test.js @@ -12,15 +12,18 @@ const { query } = createTestClient(new ApolloServer(config)); // keep an eye on perf - watch for tests with way too many queries! jest.mock("./db"); let queryFn; +let db; beforeEach(() => { connectToDb.mockImplementation(async (...args) => { - const db = await actualConnectToDb(...args); + db = await actualConnectToDb(...args); queryFn = jest.spyOn(db, "execute"); return db; }); }); afterEach(() => { jest.resetAllMocks(); + db.end(); + db = null; }); it("can load items", async () => {