1
0
Fork 0
mirror of synced 2024-07-16 03:35:56 +12:00

Merge pull request #1508 from Budibase/postgres-connection-pool

postgres connection pooling
This commit is contained in:
Martin McKeaveney 2021-05-21 12:28:51 +01:00 committed by GitHub
commit 6233de1fc1

View file

@ -1,4 +1,6 @@
const { Client } = require("pg") const { Pool } = require("pg")
let pool
const SCHEMA = { const SCHEMA = {
docs: "https://node-postgres.com", docs: "https://node-postgres.com",
@ -51,31 +53,39 @@ const SCHEMA = {
class PostgresIntegration { class PostgresIntegration {
constructor(config) { constructor(config) {
this.config = config this.config = config
this.client = new Client(config) if (!pool) {
this.connect() pool = new Pool(this.config)
}
} }
async connect() { async query(sql) {
return this.client.connect() 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 }) { async create({ sql }) {
const response = await this.client.query(sql) const response = await this.query(sql)
return response.rows.length ? response.rows : [{ created: true }] return response.rows.length ? response.rows : [{ created: true }]
} }
async read({ sql }) { async read({ sql }) {
const response = await this.client.query(sql) const response = await this.query(sql)
return response.rows return response.rows
} }
async update({ sql }) { async update({ sql }) {
const response = await this.client.query(sql) const response = await this.query(sql)
return response.rows.length ? response.rows : [{ updated: true }] return response.rows.length ? response.rows : [{ updated: true }]
} }
async delete({ sql }) { async delete({ sql }) {
const response = await this.client.query(sql) const response = await this.query(sql)
return response.rows.length ? response.rows : [{ deleted: true }] return response.rows.length ? response.rows : [{ deleted: true }]
} }
} }