Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var rows = [{
- name: 'Brian',
- age: 31
- }, {
- name: 'Aaron',
- age: 29
- }]
- var buildStatement = function(rows) {
- var params = []
- var chunks = []
- for(var i = 0; i < rows.lenght; i++) {
- var row = rows[i]
- var valuesClause = []
- params.push(row.name)
- valueClause.push('$' + params.length)
- params.push(row.age)
- valueClause.push('$' + params.length)
- chunks.push('(' + valueClause.join(', ') + ')')
- }
- return {
- text: 'INSERT INTO foo(name, age) VALUES ' + chunks.join(', '),
- values: params
- }
- }
- client.query(buildStatement(rows))
Add Comment
Please, Sign In to add comment