brianc / node-postgres

PostgreSQL client for node.js.
https://node-postgres.com
MIT License
12.22k stars 1.22k forks source link

Feature Request: handle an object in query string parameter array #1526

Closed jgsestrich closed 6 years ago

jgsestrich commented 6 years ago

Having just transitioned from mysql to postgres, I am missing a feature that seems rather handy to me: If an element in the query string parameter array happens to be an object, it gets transformed into key = 'val' pairs.

From node-mysql docs-- (https://github.com/mysqljs/mysql#performing-queries)

Objects are turned into key = 'val' pairs for each enumerable property on the object. If the property's value is a function, it is skipped; if the property's value is an object, toString() is called on it and the returned value is used.

This escaping allows you to do neat things like this:

var post = {id: 1, title: 'Hello MySQL'}; var query = connection.query('INSERT INTO posts SET ?', post, function (error, results, fields) { if (error) throw error; // Neat! }); console.log(query.sql); // INSERT INTO posts SET id = 1, title = 'Hello MySQL'

charmander commented 6 years ago

This library operates at a lower level than node-mysql and doesn’t modify your queries. (PostgreSQL doesn’t support INSERT INTO … SET either.) You can use a query builder (e.g. Knex.js) instead.