{"action":"created","issue":{"url":"https://api.github.com/repos/bookshelf/bookshelf/issues/1798","repository_url":"https://api.github.com/repos/bookshelf/bookshelf","labels_url":"https://api.github.com/repos/bookshelf/bookshelf/issues/1798/labels{/name}","comments_url":"https://api.github.com/repos/bookshelf/bookshelf/issues/1798/comments","events_url":"https://api.github.com/repos/bookshelf/bookshelf/issues/1798/events","html_url":"https://github.com/bookshelf/bookshelf/pull/1798","id":308359416,"number":1798,"title":"Clean up automatic timestamps feature","user":{"login":"ricardograca","id":1457035,"avatar_url":"https://avatars1.githubusercontent.com/u/1457035?v=4","gravatar_id":"","url":"https://api.github.com/users/ricardograca","html_url":"https://github.com/ricardograca","followers_url":"https://api.github.com/users/ricardograca/followers","following_url":"https://api.github.com/users/ricardograca/following{/other_user}","gists_url":"https://api.github.com/users/ricardograca/gists{/gist_id}","starred_url":"https://api.github.com/users/ricardograca/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ricardograca/subscriptions","organizations_url":"https://api.github.com/users/ricardograca/orgs","repos_url":"https://api.github.com/users/ricardograca/repos","events_url":"https://api.github.com/users/ricardograca/events{/privacy}","received_events_url":"https://api.github.com/users/ricardograca/received_events","type":"User","site_admin":false},"labels":[{"id":35193628,"url":"https://api.github.com/repos/bookshelf/bookshelf/labels/change","name":"change","color":"009800","default":false},{"id":234309108,"url":"https://api.github.com/repos/bookshelf/bookshelf/labels/timestamps","name":"timestamps","color":"fbca04","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":15,"created_at":"2018-03-25T15:37:16Z","updated_at":"2018-04-20T10:02:24Z","closed_at":"2018-03-25T22:40:54Z","author_association":"OWNER","pull_request":{"url":"https://api.github.com/repos/bookshelf/bookshelf/pulls/1798","html_url":"https://github.com/bookshelf/bookshelf/pull/1798","diff_url":"https://github.com/bookshelf/bookshelf/pull/1798.diff","patch_url":"https://github.com/bookshelf/bookshelf/pull/1798.patch"},"body":"* Previous PRs: #1710, #1607, #1592, #1583 \r\n\r\n## Introduction\r\n\r\nThis cleans up the previous implementations of the automatic timestamps logic.\r\n\r\nIt also changes the existing functionality so that saving a model that hasn't changed will not update its `updated_at` attribute as discussed in #1710.\r\n\r\nPassing `date` in the options to `save()` is also deprecated.\r\n\r\nAll the documentation regarding this feature is updated and new examples are added to further explain it.\r\n\r\nCloses #1710.\r\n\r\n## Motivation\r\n\r\nThe previous PRs introduced valuable functionality but were overly complicated in their implementations or introduced bugs in the way this feature was expected to work. The current solution behaves like the Rails ActiveRecord's counterpart.\r\n\r\nIn #1710 the general idea was good, but it introduced a bug that would lead to `created_at` always getting updated with a new value whenever a model was updated, and the bugs around `editCreatedAt` that were mentioned there are not valid anymore since those options are removed.\r\n\r\nWith PR #1607 it was possible to prevent a model's timestamp attributes from being updated by user values, but this can easily be achieved using other methods, like unsetting the attributes before saving. It also introduced (along with #1583) the ability to update the timestamps with user supplied values, which was good, but the implementation was a bit spread out throughout the code base and too complicated, and in the case of #1583 it introduced a bug that prevented the `updated_at` attribute from being automatically updated in some cases.\r\n\r\nThe ability to pass a `date` in the options to `save()` that was introduced in #1592 is redundant since the same thing can easily be achieved with existing methods, so it was deprecated.\r\n\r\n## Proposed solution\r\n\r\nThis will prioritize user defined values for the `created_at` and `updated_at` timestamps over any other considerations, so it's always possible to update these values directly.\r\n\r\nIf no user defined values are provided for these attributes they will be automatically set. If the model is new both of them will be set to the current date. This also works if the model being saved is new but already includes the id attribute, because in this case the `{method: 'insert'}` option is passed to the `save()` call.\r\n\r\nIf the model is not new only the `updated_at` attribute will be automatically updated.\r\n\r\nThe `options.date` functionality of `.save()` to set both timestamp attributes with the same value is deprecated and will be removed in the next version. It's already possible to achieve the same effect by setting the timestamp attributes with the wanted values and saving.\r\n\r\n## Current PR Issues\r\n\r\nBreaks backwards compatibility if users are abusing this feature to only update the `updated_at` attribute of a model without doing any further changes, but the same effect can be easily achieved with:\r\n\r\n```js\r\n// Before\r\nmyModel.save() // updated_at got updated to the current date\r\n\r\n// Now\r\nmyModel.save({updated_at: new Date()})\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/bookshelf/bookshelf/issues/comments/383049229","html_url":"https://github.com/bookshelf/bookshelf/pull/1798#issuecomment-383049229","issue_url":"https://api.github.com/repos/bookshelf/bookshelf/issues/1798","id":383049229,"user":{"login":"mborst","id":19157468,"avatar_url":"https://avatars1.githubusercontent.com/u/19157468?v=4","gravatar_id":"","url":"https://api.github.com/users/mborst","html_url":"https://github.com/mborst","followers_url":"https://api.github.com/users/mborst/followers","following_url":"https://api.github.com/users/mborst/following{/other_user}","gists_url":"https://api.github.com/users/mborst/gists{/gist_id}","starred_url":"https://api.github.com/users/mborst/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mborst/subscriptions","organizations_url":"https://api.github.com/users/mborst/orgs","repos_url":"https://api.github.com/users/mborst/repos","events_url":"https://api.github.com/users/mborst/events{/privacy}","received_events_url":"https://api.github.com/users/mborst/received_events","type":"User","site_admin":false},"created_at":"2018-04-20T10:02:24Z","updated_at":"2018-04-20T10:02:24Z","author_association":"CONTRIBUTOR","body":"I am using this in practice as stated: I pass in `myIdeaOfWhatTimeItIs` everywhere as `date`, and assume that the library takes care of handling the rest.\r\nIn my opinion, I shouldn't have to care about what exactly the library will do in my application code. My brain is already full with the application logic. I know the library will set the _right_ fields, because the developer (in this case I) spent time thinking about it once and took care that in any case, the right timestamps are filled with the right values. That's what I mean by \"stupid\": I don't need to care, because it has been solved once."}}
{"actor":{"display_login":"mborst"}}