Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undestroy method for paranoid models #2540

Merged
merged 9 commits into from Nov 13, 2014

Conversation

seth-admittedly
Copy link
Contributor

Undestroy allows for restoring destroyed paranoid models. Mirrors the syntax of destroy, with both model (bulk) and instance options.

Implementing this without resorting to manual SQL required tweaking the query generator to allow for omitNull to be passed as an option with an individual query.

var identifier;

if (!self.Model._timestampAttributes.deletedAt) {
return Promise.reject(new Error("Model is not paranoid"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should fail early if deletedAt is not define (before hooks)

@janmeier
Copy link
Member

janmeier commented Nov 6, 2014

On of the tests is failing under pg.

Other than that, just some small comments, please ask if you have problems with promise style in tests

@janmeier janmeier self-assigned this Nov 6, 2014
@mickhansen
Copy link
Contributor

Might restore be a better name for undestroy?

@seth-admittedly
Copy link
Contributor Author

@janmeier Do those promise tests look right? Don't have much experience with them.

@seth-admittedly
Copy link
Contributor Author

@mickhansen Agreed

@seth-admittedly
Copy link
Contributor Author

@janmeier Is it better to return a Promise.reject or throw a error synchronously?

@cusspvz
Copy link

cusspvz commented Nov 6, 2014

@seth-admittedly usually they prefer an error throw :)

+1, thanks for the PR!

* @return {Promise<undefined>}
*/
Instance.prototype.restore = function(options) {
if (!this.Model._timestampAttributes.deletedAt) return Promise.reject(new Error("Model is not paranoid"));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually they prefer an error throw since you should always call you methods right.

An error should be triggered into the promise if isn't at your handling scope. :)

@janmeier
Copy link
Member

janmeier commented Nov 7, 2014

@seth-admittedly I'd prefer promise tests that are not so nested. So rather this:

something.then(function () {
  return somethingelse()
}).then(function () {
  return anotherPromise();
})

Than

something.then(function () {
  return somethingelse().then(function () {
    return anotherPromise();
  })
})

But it's fine for now :)

@janmeier
Copy link
Member

janmeier commented Nov 7, 2014

As @cusspvz we usually just throw the error synchronously, since the error is not because of a DB error or something other async thing, but because of a programming error from the user.

You can also change the assertion to

expect(self.User.restore({where : {username : "Peter"}})).to.throw(Error, 'Model is not paranoid')

To avoid the awkvard expect false to be true in the succes handler

@seth-admittedly
Copy link
Contributor Author

The only way I could get the throw to test correct was with the kludgey looking try/catch. Otherwise the throw seemed to interfere with the promise chain being returned correctly. I couldn't find an example of a promisey throw test, suggestions on how to make it better are very welcome.

return this.User.create({username : "Peter", secretValue : "42"})
.then(function(user){
try{
expect(self.User.destroy({where : {secretValue : "42"}})).to.throw(Error, "Model is not paranoid");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be restore?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason you have to wrap it in a function - still not pretty, but better

return this.User.create({username : "Peter", secretValue : "42"})
      .then(function(user){
          expect(function () {
            self.User.restore({where : {secretValue : "42"}})
          }).to.throw(Error, "Model is not paranoid");
      })

@seth-admittedly
Copy link
Contributor Author

Thanks for the typo catch and the function wrapper suggestion, which makes sense once you see it.

janmeier added a commit that referenced this pull request Nov 13, 2014
@janmeier janmeier merged commit 9d13c1b into sequelize:master Nov 13, 2014
@janmeier
Copy link
Member

Good work as always @seth-admittedly !

janmeier added a commit that referenced this pull request Nov 13, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants