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

Allow transformation for insert & update [Discussion] #8

Closed
djensen47 opened this issue May 15, 2014 · 4 comments
Closed

Allow transformation for insert & update [Discussion] #8

djensen47 opened this issue May 15, 2014 · 4 comments

Comments

@djensen47
Copy link
Contributor

This is what I'm working on now. Actually, it's done, I just need to write the tests.

Resource.prototype.insert = function (transform) {
  var self = this;
  var emitInsert = emitEvent(self, 'insert');

  return function(req, res, next) {
    if (transform && typeof transform === 'function') {
      transform(req, res);  
    }
    self.Model.create(req.body, function (err, model) {
      if (err) {
        return onError(err, next);
      }

      res.header('Location', req.url + '/' + model._id);
      res.send(200, model);

      emitInsert(model, next);
    });
  };
};

The idea is that sometimes on an insert or update, you need to modify the data that will be saved. Specifically, an authenticated user POSTing a new note shouldn't have to pass in their own id if we already have it on every request. Thus the transform function.

I wanted to run this by you before I finished the tests and submitted a pull request.

@saintedlama
Copy link
Owner

I guess we should make the transform callback aware so that we not only

transform(req, res); 

but

transform(req, res, function(err, transformedObj) {
  //...
});

This would not require transform to manipulate the body and transform itself could use some async functions. This could be solved in code like other functionality that uses async to handle these functions.

Thoughts?

@djensen47
Copy link
Contributor Author

Oops, I though I responded. I think something like beforeSave would be more appropriate. Otherwise, yes, making it part of the async.waterfall makes a lot of sense.

djensen47 added a commit to djensen47/restify-mongoose that referenced this issue May 18, 2014
Per the discussion on [Issue
8](saintedlama#8) this is the
functionality to execute a custom function before the model is saved.
This allows for operations such as inserting a user id on a model before
is it saved.
@djensen47
Copy link
Contributor Author

See Pull Request #10

@djensen47
Copy link
Contributor Author

I'm going to re-open until the pull request is accepted.

@djensen47 djensen47 reopened this May 18, 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

No branches or pull requests

2 participants