diff --git a/packages/server/__mocks__/pg.ts b/packages/server/__mocks__/pg.ts index 5f8a133134..44aeabcb38 100644 --- a/packages/server/__mocks__/pg.ts +++ b/packages/server/__mocks__/pg.ts @@ -14,6 +14,7 @@ module PgMock { function Client() {} Client.prototype.query = query + Client.prototype.end = jest.fn() Client.prototype.connect = jest.fn() Client.prototype.release = jest.fn() diff --git a/packages/server/src/integrations/postgres.ts b/packages/server/src/integrations/postgres.ts index 1dc6fd9d2d..d38b657566 100644 --- a/packages/server/src/integrations/postgres.ts +++ b/packages/server/src/integrations/postgres.ts @@ -16,7 +16,7 @@ import { import { DatasourcePlus } from "./base/datasourcePlus" module PostgresModule { - const { Pool } = require("pg") + const { Client } = require("pg") const Sql = require("./base/sql") const { escapeDangerousCharacters } = require("../utilities") @@ -104,7 +104,6 @@ module PostgresModule { } class PostgresIntegration extends Sql implements DatasourcePlus { - static pool: any private readonly client: any private readonly config: PostgresConfig private index: number = 1 @@ -136,11 +135,7 @@ module PostgresModule { } : undefined, } - if (!this.pool) { - this.pool = new Pool(newConfig) - } - - this.client = this.pool + this.client = new Client(newConfig) this.setSchema() } @@ -171,16 +166,17 @@ module PostgresModule { } catch (err) { // @ts-ignore throw new Error(err) + } finally { + await this.client.end() } } - setSchema() { + async setSchema() { + await this.client.connect() if (!this.config.schema) { this.config.schema = "public" } - this.client.on("connect", (client: any) => { - client.query(`SET search_path TO ${this.config.schema}`) - }) + this.client.query(`SET search_path TO ${this.config.schema}`) this.COLUMNS_SQL = `select * from information_schema.columns where table_schema = '${this.config.schema}'` } diff --git a/packages/server/src/integrations/tests/postgres.spec.js b/packages/server/src/integrations/tests/postgres.spec.js index 5c0d086ce0..4ce5f12e96 100644 --- a/packages/server/src/integrations/tests/postgres.spec.js +++ b/packages/server/src/integrations/tests/postgres.spec.js @@ -15,10 +15,6 @@ describe("Postgres Integration", () => { config = new TestConfiguration() }) - it("calls the connection callback", async () => { - expect(pg.on).toHaveBeenCalledWith('connect', expect.anything()) - }) - it("calls the create method with the correct params", async () => { const sql = "insert into users (name, age) values ('Joe', 123);" await config.integration.create({