From 5d48fe08c77755ed18b3f7a9ea834268e317e7ef Mon Sep 17 00:00:00 2001 From: Fabio Di Stasio Date: Wed, 24 Nov 2021 14:24:52 +0100 Subject: [PATCH] feat(PostgreSQL): read-only mode --- src/common/customizations/postgresql.js | 3 ++- src/main/libs/clients/PostgreSQLClient.js | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/common/customizations/postgresql.js b/src/common/customizations/postgresql.js index 078ce79e..ff67b892 100644 --- a/src/common/customizations/postgresql.js +++ b/src/common/customizations/postgresql.js @@ -54,5 +54,6 @@ module.exports = { triggerTableInName: true, triggerOnlyRename: false, triggerEnableDisable: true, - languages: ['sql', 'plpgsql', 'c', 'internal'] + languages: ['sql', 'plpgsql', 'c', 'internal'], + readOnlyMode: true }; diff --git a/src/main/libs/clients/PostgreSQLClient.js b/src/main/libs/clients/PostgreSQLClient.js index 6a68948f..7c2e65fc 100644 --- a/src/main/libs/clients/PostgreSQLClient.js +++ b/src/main/libs/clients/PostgreSQLClient.js @@ -105,10 +105,19 @@ export class PostgreSQLClient extends AntaresCore { const client = new Client(dbConfig); await client.connect(); this._connection = client; + + if (this._params.readonly) + await this.raw('SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY'); } else { const pool = new Pool({ ...dbConfig, max: this._poolSize }); this._connection = pool; + + if (this._params.readonly) { + this._connection.on('connect', connection => { + connection.query('SET SESSION CHARACTERISTICS AS TRANSACTION READ ONLY'); + }); + } } }