Skip to content

Commit

Permalink
acl pool
Browse files Browse the repository at this point in the history
  • Loading branch information
dbauszus-glx committed Nov 23, 2023
1 parent 7784803 commit 70f6b56
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions mod/user/acl.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
const { Pool } = require('pg');

let pool = null

module.exports = () => {

const connection = process.env.PRIVATE && process.env.PRIVATE.split('|') || process.env.PUBLIC && process.env.PUBLIC.split('|')
const connection = process.env.PRIVATE && process.env.PRIVATE.split('|')
|| process.env.PUBLIC && process.env.PUBLIC.split('|')

if(!connection || !connection[1]) return

Expand All @@ -11,24 +14,26 @@ module.exports = () => {
const acl_schema = connection[1].split('.')[0] === acl_table ? 'public' : connection[1].split('.')[0]

// Create PostgreSQL connection pool for ACL table.
const pool = new Pool({
connectionString: connection[0],
statement_timeout: 1000
pool ??= new Pool({
connectionString: connection[0]
})

// Method to query ACL. arr must be empty array by default.
return async (q, arr) => {

try {

const { rows } = await pool.query(q.replace(/acl_table/g, acl_table).replace(/acl_schema/g, acl_schema), arr)
const client = await pool.connect()

const { rows } = await client.query(q.replace(/acl_table/g, acl_table).replace(/acl_schema/g, acl_schema), arr)

client.release()

return rows

} catch (err) {
console.error(err)
return err
}

}

}

0 comments on commit 70f6b56

Please sign in to comment.