Skip to content

Instantly share code, notes, and snippets.

@rek
Created August 17, 2016 14:32
Show Gist options
  • Save rek/3a83dd40717d95c454657ed4731cef26 to your computer and use it in GitHub Desktop.
Save rek/3a83dd40717d95c454657ed4731cef26 to your computer and use it in GitHub Desktop.
PostgreSQL Insert Helper - Javascript
import _ from 'lodash'
let inserter = (table, items) => {
let validKeys = _.keys(items).join(', '),
countKeys = _.reduce(items, (results) => {
results.push('$' + (results.length + 1))
return results
}, []).join(', '),
data = _.values(items)
return {
data,
query: `INSERT INTO ${table} (${validKeys}) VALUES (${countKeys})`,
}
}
export {inserter}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment