{"action":"closed","issue":{"url":"https://api.github.com/repos/vitaly-t/pg-promise/issues/217","repository_url":"https://api.github.com/repos/vitaly-t/pg-promise","labels_url":"https://api.github.com/repos/vitaly-t/pg-promise/issues/217/labels{/name}","comments_url":"https://api.github.com/repos/vitaly-t/pg-promise/issues/217/comments","events_url":"https://api.github.com/repos/vitaly-t/pg-promise/issues/217/events","html_url":"https://github.com/vitaly-t/pg-promise/issues/217","id":180424272,"number":217,"title":"Simple insert","user":{"login":"gkiely","id":1948935,"avatar_url":"https://avatars.githubusercontent.com/u/1948935?v=3","gravatar_id":"","url":"https://api.github.com/users/gkiely","html_url":"https://github.com/gkiely","followers_url":"https://api.github.com/users/gkiely/followers","following_url":"https://api.github.com/users/gkiely/following{/other_user}","gists_url":"https://api.github.com/users/gkiely/gists{/gist_id}","starred_url":"https://api.github.com/users/gkiely/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/gkiely/subscriptions","organizations_url":"https://api.github.com/users/gkiely/orgs","repos_url":"https://api.github.com/users/gkiely/repos","events_url":"https://api.github.com/users/gkiely/events{/privacy}","received_events_url":"https://api.github.com/users/gkiely/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/vitaly-t/pg-promise/labels/question","name":"question","color":"cc317c"}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":3,"created_at":"2016-09-30T23:51:35Z","updated_at":"2016-10-01T22:13:56Z","closed_at":"2016-10-01T22:13:56Z","body":"Correct me if there's already a way to do this.\r\n\r\nUse case: often times I find myself updating the insert query in three different places. It would be great if I could just pass an object and let it generate the query automatically.\r\n\r\nGiven a post object:\r\n`db.query('insert into posts (title, description, amount, date) values($1, $2, $3, $4)', [post.title, post.description, post.amount, post.date]);`\r\n\r\nWould become:\r\n`db.insert('posts', post);`\r\n\r\n\r\nCurrently I'm using the following function in this way:\r\n```js\r\nlet generateInsert = function(str, obj){\r\n  let count = 0;\r\n  let vals = \"values (\";\r\n  let arr = [];\r\n\r\n  str = 'insert into ' + str + ' (';\r\n  for(let i in obj){\r\n    count++;\r\n    str += i + ',';\r\n    vals += '$' + count + ',';\r\n    arr.push(obj[i]);\r\n  }\r\n  str = str.replace(/,$/, ')');\r\n  vals = vals.replace(/,$/, ')');\r\n\r\n  return db.query(str +  vals, arr);\r\n};\r\n\r\nlet query = generateInsert('posts', obj);\r\n```"}}
{"actor":{"display_login":"gkiely"}}