mariadb-corporation / mariadb-connector-nodejs

MariaDB Connector/Node.js is used to connect applications developed on Node.js to MariaDB and MySQL databases. MariaDB Connector/Node.js is LGPL licensed.
GNU Lesser General Public License v2.1
369 stars 91 forks source link

Read-only Connections (feature request) #269

Closed joematthews closed 8 months ago

joematthews commented 8 months ago

I could not find a previous issue/request on this topic.

Would it be convenient (and safer) to offer a --read-only argument for the mariadb CLI and a readOnly property for the config object for mariadb-connector-nodejs connections? This would make it so much easier, to use an environment variable like DB_READ_ONLY=true to easily disable writes.

The basic idea is that a read-only property could provide a more clear and succinct way to connect to any database 'in read-only mode' without using scary, SQL-in-args that flush privileges and without creating a universal, read-only user that everyone shares.

I feel like this feature would be especially helpful to prevent accidental writes when reproducing issues and debugging using live prod data (not best practice, obviously, but it's common).

My inspiration comes from DataGrip's and DBeaver's read-only option in the connection settings allowing the same read privileges as my user but prevents writes (with zero configuration) -- and personally, I keep read-only on by default for all my production connections in these editors.

I am not a DBA, and I suspect this may end up being a bad idea in some scenarios or environments...but on the surface it sounds very helpful?

rusher commented 8 months ago

There isn't an option for that, but this can easily be done using initSql: 'SET SESSION TRANSACTION READ ONLY', like in

 const conn = await mariadb.createConnection({
  host: 'mydb.com',
  user: 'myUser',
  password: 'myPwd',
  initSql: 'SET SESSION TRANSACTION READ ONLY'
 });

Is there a need for a specific option for that, i don't know, there isn't in all other mysql connectors (for any languages)

joematthews commented 8 months ago

Awesome. That's simple enough. I think it will fill the need. I'll go ahead and close this. Thank you.