{"action":"created","issue":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/6761","repository_url":"https://api.github.com/repos/Automattic/mongoose","labels_url":"https://api.github.com/repos/Automattic/mongoose/issues/6761/labels{/name}","comments_url":"https://api.github.com/repos/Automattic/mongoose/issues/6761/comments","events_url":"https://api.github.com/repos/Automattic/mongoose/issues/6761/events","html_url":"https://github.com/Automattic/mongoose/issues/6761","id":343800083,"node_id":"MDU6SXNzdWUzNDM4MDAwODM=","number":6761,"title":"Model.create in a transaction","user":{"login":"sebastian-nowak","id":5895458,"node_id":"MDQ6VXNlcjU4OTU0NTg=","avatar_url":"https://avatars3.githubusercontent.com/u/5895458?v=4","gravatar_id":"","url":"https://api.github.com/users/sebastian-nowak","html_url":"https://github.com/sebastian-nowak","followers_url":"https://api.github.com/users/sebastian-nowak/followers","following_url":"https://api.github.com/users/sebastian-nowak/following{/other_user}","gists_url":"https://api.github.com/users/sebastian-nowak/gists{/gist_id}","starred_url":"https://api.github.com/users/sebastian-nowak/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sebastian-nowak/subscriptions","organizations_url":"https://api.github.com/users/sebastian-nowak/orgs","repos_url":"https://api.github.com/users/sebastian-nowak/repos","events_url":"https://api.github.com/users/sebastian-nowak/events{/privacy}","received_events_url":"https://api.github.com/users/sebastian-nowak/received_events","type":"User","site_admin":false},"labels":[{"id":180910479,"node_id":"MDU6TGFiZWwxODA5MTA0Nzk=","url":"https://api.github.com/repos/Automattic/mongoose/labels/discussion","name":"discussion","color":"d4c5f9","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-07-23T21:20:24Z","updated_at":"2018-11-09T20:59:00Z","closed_at":null,"author_association":"NONE","body":"With the recent introduction of transactions, various methods now accept an additional `session` option. Unfortunately it made things a bit tricky for `Model.create`.\r\n\r\n<img width=\"839\" alt=\"screen shot 2018-07-23 at 23 16 04\" src=\"https://user-images.githubusercontent.com/5895458/43103465-6a8064be-8ece-11e8-935d-06a4d98ca05d.png\">\r\n\r\n`Model.create` accepts options only if given an array. This means that a simple statement like this:\r\n\r\n```js\r\nconst instance = await SomeModel.create({\r\n  prop1: true,\r\n  prop2: 123\r\n});\r\n```\r\n\r\nnow becomes:\r\n\r\n```js\r\nconst instance = (await SomeModel.create([{\r\n  prop1: true,\r\n  prop2: 123\r\n}], {session: ...}))[0];\r\n```\r\n\r\nIt's becoming hard to read, so it would be awesome if there would be a way to create a single document without having to pass an array."},"comment":{"url":"https://api.github.com/repos/Automattic/mongoose/issues/comments/437493275","html_url":"https://github.com/Automattic/mongoose/issues/6761#issuecomment-437493275","issue_url":"https://api.github.com/repos/Automattic/mongoose/issues/6761","id":437493275,"node_id":"MDEyOklzc3VlQ29tbWVudDQzNzQ5MzI3NQ==","user":{"login":"c-eliasson","id":1837520,"node_id":"MDQ6VXNlcjE4Mzc1MjA=","avatar_url":"https://avatars3.githubusercontent.com/u/1837520?v=4","gravatar_id":"","url":"https://api.github.com/users/c-eliasson","html_url":"https://github.com/c-eliasson","followers_url":"https://api.github.com/users/c-eliasson/followers","following_url":"https://api.github.com/users/c-eliasson/following{/other_user}","gists_url":"https://api.github.com/users/c-eliasson/gists{/gist_id}","starred_url":"https://api.github.com/users/c-eliasson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/c-eliasson/subscriptions","organizations_url":"https://api.github.com/users/c-eliasson/orgs","repos_url":"https://api.github.com/users/c-eliasson/repos","events_url":"https://api.github.com/users/c-eliasson/events{/privacy}","received_events_url":"https://api.github.com/users/c-eliasson/received_events","type":"User","site_admin":false},"created_at":"2018-11-09T20:59:00Z","updated_at":"2018-11-09T20:59:00Z","author_association":"NONE","body":"@vkarpov15 Should `SomeModel(obj).save(session)` really work?\r\n\r\nWhen i first tried it, with my previous comment, all I verified was that the document was actually created. However, I just noticed that if i abort the transaction with `session.abortTransaction()`, the document created with `SomeModel(obj).save(session)` is NOT rolled back. The same goes for when you provoke a database error, like trying to create a dupe key on a unique index. In that case, the transaction aborts, but the document created with `SomeModel(obj).save(session)` is NOT rolled back.\r\n\r\nI also failed to find any support for it int [the docs](https://mongoosejs.com/docs/api.html#model_Model-save)\r\n\r\nUsing your example with `await SomeModel.create([obj], { session })` works perfectly though, both when manually aborting the transaction, and when transaction aborts due to a database error."}}
{"actor":{"display_login":"c-eliasson"}}