alexandrainst / node-red-contrib-postgresql

Node-RED node for PostgreSQL, supporting parameters, split, back-pressure
https://flows.nodered.org/node/node-red-contrib-postgresql
Apache License 2.0
32 stars 13 forks source link

possible to query directly in node-red function #38

Closed kolaval closed 1 year ago

kolaval commented 1 year ago

Is it possible to query postgres directly in a node-red function? I found an article that explains the process for mysql and I'm hoping that there's something similar. `// Initialize the MySQL node var mysql = context.get('mysql');

// Define the SQL query to be executed var sql = 'SELECT * FROM users WHERE id = 1';

// Execute the query and retrieve the results var result = await mysql.query(sql);

// Use the results in your JavaScript code msg.payload = result;`

https://community.home-assistant.io/t/access-sql-database-from-within-a-node-red-function-node/501344

Alkarex commented 1 year ago

Hello, This is an interesting approach, which I have not investigated. It would require some minor refactoring to allow that elegantly. PR welcome!

Alkarex commented 1 year ago

At a quick glance, the node-red-node-mysql node referred by the article does not seem to allow such a usage https://github.com/node-red/node-red-nodes/blob/master/storage/mysql/68-mysql.js which makes me doubt whether that article ever worked.

Alternatively, you can use the underlying pg library from a node function: https://github.com/brianc/node-postgres