From 6cb13807703f8c359ae7c43f30529cd7b92fbe7e Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Wed, 19 May 2021 20:58:59 +0100 Subject: [PATCH] postgres connection pooling --- packages/bbui/src/Typography/Body.svelte | 2 +- packages/bbui/src/Typography/Heading.svelte | 2 +- .../src/components/login/ForgotForm.svelte | 8 ++++-- .../src/components/login/ResetForm.svelte | 5 ++-- packages/server/src/integrations/postgres.js | 28 +++++++++++++------ 5 files changed, 29 insertions(+), 16 deletions(-) diff --git a/packages/bbui/src/Typography/Body.svelte b/packages/bbui/src/Typography/Body.svelte index ec32e54140..13e83434e4 100644 --- a/packages/bbui/src/Typography/Body.svelte +++ b/packages/bbui/src/Typography/Body.svelte @@ -8,7 +8,7 @@

diff --git a/packages/builder/src/components/login/ForgotForm.svelte b/packages/builder/src/components/login/ForgotForm.svelte index afadf9f4cf..25d72228ef 100644 --- a/packages/builder/src/components/login/ForgotForm.svelte +++ b/packages/builder/src/components/login/ForgotForm.svelte @@ -30,12 +30,14 @@ Forgotten your password? - No problem! Just enter your account's email address and we'll send - you a link to reset it. + No problem! Just enter your account's email address and we'll send you + a link to reset it. - + diff --git a/packages/builder/src/components/login/ResetForm.svelte b/packages/builder/src/components/login/ResetForm.svelte index 4ae19b1ab1..83e9ab7a6e 100644 --- a/packages/builder/src/components/login/ResetForm.svelte +++ b/packages/builder/src/components/login/ResetForm.svelte @@ -16,7 +16,6 @@ } catch (err) { notifications.error("Unable to reset password") } - } @@ -33,7 +32,9 @@ - + diff --git a/packages/server/src/integrations/postgres.js b/packages/server/src/integrations/postgres.js index c6a24577b3..d567db7ba0 100644 --- a/packages/server/src/integrations/postgres.js +++ b/packages/server/src/integrations/postgres.js @@ -1,4 +1,6 @@ -const { Client } = require("pg") +const { Pool } = require("pg") + +let pool const SCHEMA = { docs: "https://node-postgres.com", @@ -51,31 +53,39 @@ const SCHEMA = { class PostgresIntegration { constructor(config) { this.config = config - this.client = new Client(config) - this.connect() + if (!pool) { + pool = new Pool(this.config) + } } - async connect() { - return this.client.connect() + async query(sql) { + try { + this.client = await pool.connect() + return await this.client.query(sql) + } catch (err) { + throw new Error(err) + } finally { + this.client.release() + } } async create({ sql }) { - const response = await this.client.query(sql) + const response = await this.query(sql) return response.rows.length ? response.rows : [{ created: true }] } async read({ sql }) { - const response = await this.client.query(sql) + const response = await this.query(sql) return response.rows } async update({ sql }) { - const response = await this.client.query(sql) + const response = await this.query(sql) return response.rows.length ? response.rows : [{ updated: true }] } async delete({ sql }) { - const response = await this.client.query(sql) + const response = await this.query(sql) return response.rows.length ? response.rows : [{ deleted: true }] } }