Browse Source
Merge pull request #1508 from Budibase/postgres-connection-pool
postgres connection pooling
gh-pages
Martin McKeaveney
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with
19 additions and
9 deletions
-
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 }] |
|
|
|
} |
|
|
|
} |
|
|
|
|