Created
August 17, 2016 14:32
-
-
Save rek/3a83dd40717d95c454657ed4731cef26 to your computer and use it in GitHub Desktop.
PostgreSQL Insert Helper - Javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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