Merge pull request #143 from Fabio286/feat/read-only-mode-on-connections

feat: read-only mode
This commit is contained in:
Fabio Di Stasio 2021-11-24 14:41:31 +01:00 committed by GitHub
commit fe8435531e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 3 deletions

View File

@ -62,5 +62,6 @@ module.exports = {
functionDeterministic: true,
functionDataAccess: true,
functionSql: 'BEGIN\r\n\r\nEND',
parametersLength: true
parametersLength: true,
readOnlyMode: true
};

View File

@ -54,5 +54,6 @@ module.exports = {
triggerTableInName: true,
triggerOnlyRename: false,
triggerEnableDisable: true,
languages: ['sql', 'plpgsql', 'c', 'internal']
languages: ['sql', 'plpgsql', 'c', 'internal'],
readOnlyMode: true
};

View File

@ -133,8 +133,12 @@ export class MySQLClient extends AntaresCore {
}
}
if (!this._poolSize)
if (!this._poolSize) {
this._connection = await mysql.createConnection(dbConfig);
if (this._params.readonly)
await this.raw('SET SESSION TRANSACTION READ ONLY');
}
else {
this._connection = mysql.createPool({
...dbConfig,
@ -146,6 +150,12 @@ export class MySQLClient extends AntaresCore {
return next();
}
});
if (this._params.readonly) {
this._connection.on('connection', connection => {
connection.query('SET SESSION TRANSACTION READ ONLY');
});
}
}
}

View File

@ -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');
});
}
}
}