{"action":"created","issue":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/9066","repository_url":"https://api.github.com/repos/sequelize/sequelize","labels_url":"https://api.github.com/repos/sequelize/sequelize/issues/9066/labels{/name}","comments_url":"https://api.github.com/repos/sequelize/sequelize/issues/9066/comments","events_url":"https://api.github.com/repos/sequelize/sequelize/issues/9066/events","html_url":"https://github.com/sequelize/sequelize/issues/9066","id":298035766,"number":9066,"title":"Granular rejectOnEmpty configuration","user":{"login":"pzanitti","id":1615955,"avatar_url":"https://avatars3.githubusercontent.com/u/1615955?v=4","gravatar_id":"","url":"https://api.github.com/users/pzanitti","html_url":"https://github.com/pzanitti","followers_url":"https://api.github.com/users/pzanitti/followers","following_url":"https://api.github.com/users/pzanitti/following{/other_user}","gists_url":"https://api.github.com/users/pzanitti/gists{/gist_id}","starred_url":"https://api.github.com/users/pzanitti/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/pzanitti/subscriptions","organizations_url":"https://api.github.com/users/pzanitti/orgs","repos_url":"https://api.github.com/users/pzanitti/repos","events_url":"https://api.github.com/users/pzanitti/events{/privacy}","received_events_url":"https://api.github.com/users/pzanitti/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-02-17T20:57:51Z","updated_at":"2018-02-25T09:16:40Z","closed_at":null,"author_association":"NONE","body":"<!--\r\n\r\nPlease note this is an issue tracker, not a support forum.\r\nFor general questions, please use StackOverflow or Slack.\r\n\r\nFor bugs, please fill out the template below.\r\n\r\n-->\r\n\r\n## What are you doing?\r\nI want findOne and findById to reject on `null` result, and findAll to resolve with [] on empty result. It's so coding on Express and axios is easier. Axios resolves on 2xx and rejects on anything else, so I want the status to be something nicer than the defaults.\r\n\r\n```\r\nmodel.someModel.[findAll/findById/findOne](params)\r\n    .then(responseHandler)\r\n    .catch(error => {\r\n      if (error instanceof Sequelize.EmptyResultError) {\r\n        return res.status(404).send()\r\n      } else {\r\n        return res.status(500)\r\n      }\r\n    })\r\n```\r\n\r\nTesting with:\r\n\r\n```js\r\ndb.someModel.findOne({ where: { id: 3 } }).then((res) => console.log('findOne resolves', res))\r\n  .catch((err) => console.log('findOne rejects', err))\r\n\r\ndb.someModel.findById(3).then((res) => console.log('findById resolves', res))\r\n  .catch((err) => console.log('findById rejects', err))\r\n\r\ndb.someModel.findAll().then((res) => console.log('findAll resolves', res))\r\n  .catch((err) => console.log('findAll rejects', err))\r\n```\r\n\r\n## What do you expect to happen?\r\n```\r\nfindById rejects SequelizeEmptyResultError\r\nfindAll resolves []\r\nfindOne rejects SequelizeEmptyResultError\r\n```\r\n\r\nI went ahead and overrode the prototypes like this guy did https://github.com/sequelize/sequelize/issues/8892\r\n\r\n```\r\nconst originalFindById = Sequelize.Model.findById\r\n\r\nSequelize.Model.findById = async function (...args) {\r\n  const obj = await originalFindById.call(this, ...args)\r\n  if (obj === null) {\r\n    throw new Sequelize.EmptyResultError()\r\n  }\r\n\r\n  return obj\r\n}\r\n\r\nconst originalFindOne = Sequelize.Model.findOne\r\n\r\nSequelize.Model.findOne = async function (...args) {\r\n  const obj = await originalFindOne.call(this, ...args)\r\n  if (obj === null) {\r\n    throw new Sequelize.EmptyResultError()\r\n  }\r\n\r\n  return obj\r\n}\r\n```\r\nNo problems yet but I wonder if I'll run into any. For example, findByOne internally calls findAll. If I overrode findAll most likely findByOne would break?\r\n\r\n## What is actually happening?\r\n```\r\nfindById resolves null\r\nfindAll resolves []\r\nfindOne resolves null\r\n```\r\n\r\nAnd if I turn on `rejectOnEmpty`: \r\n\r\n```\r\nfindById rejects SequelizeEmptyResultError\r\nfindAll rejects SequelizeEmptyResultError\r\nfindOne rejects SequelizeEmptyResultError\r\n```\r\n\r\nI wish I could set a middle ground using individual flags.\r\n\r\n__Dialect:__ any\r\n__Dialect version:__ -\r\n__Database version:__ -\r\n__Sequelize version:__ 4.33.4\r\n__Tested with latest release:__ Yes\r\n\r\n\r\n**Note :** _Your issue may be ignored OR closed by maintainers if it's not tested against latest version OR does not follow issue template._\r\n"},"comment":{"url":"https://api.github.com/repos/sequelize/sequelize/issues/comments/368294678","html_url":"https://github.com/sequelize/sequelize/issues/9066#issuecomment-368294678","issue_url":"https://api.github.com/repos/sequelize/sequelize/issues/9066","id":368294678,"user":{"login":"sushantdhiman","id":9989487,"avatar_url":"https://avatars1.githubusercontent.com/u/9989487?v=4","gravatar_id":"","url":"https://api.github.com/users/sushantdhiman","html_url":"https://github.com/sushantdhiman","followers_url":"https://api.github.com/users/sushantdhiman/followers","following_url":"https://api.github.com/users/sushantdhiman/following{/other_user}","gists_url":"https://api.github.com/users/sushantdhiman/gists{/gist_id}","starred_url":"https://api.github.com/users/sushantdhiman/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sushantdhiman/subscriptions","organizations_url":"https://api.github.com/users/sushantdhiman/orgs","repos_url":"https://api.github.com/users/sushantdhiman/repos","events_url":"https://api.github.com/users/sushantdhiman/events{/privacy}","received_events_url":"https://api.github.com/users/sushantdhiman/received_events","type":"User","site_admin":false},"created_at":"2018-02-25T09:16:40Z","updated_at":"2018-02-25T09:16:40Z","author_association":"OWNER","body":"Why dont you set `rejectOnEmpty` when calling `findOne / findById`.\r\n\r\nThere is no configuration to make this a default option, override is a good option. There is a chance that Sequelize may call your overloaded method internally.\r\n\r\nPR https://github.com/sequelize/sequelize/pull/8831 which will prevent overloaded methods from being used, but needs some work"}}
{"actor":{"display_login":"sushantdhiman"}}