Merge pull request #1508 from Budibase/postgres-connection-pool
postgres connection pooling
This commit is contained in:
commit
6233de1fc1
1 changed files with 19 additions and 9 deletions
|
@ -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 }]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue