title: Mongoose ODM, change variables before saving
body: <p>I want to create a model layer with Mongoose for my user documents, which does:</p>
  
  <ol>
  <li>validation (unique, length)</li>
  <li>canonicalisation (username and email are converted to lowercase to check uniqueness)</li>
  <li>salt generation</li>
  <li>password hashing</li>
  <li>(logging)</li>
  </ol>
  
  <p>All of these actions are required to be executed before persisting to the db. Fortunately mongoose supports validation, plugins and middleware.</p>
  
  <p>The bad thing is that I cannot find any good material on the subject.
  The official docs on mongoosejs.com are too short...</p>
  
  <p>Does anyone have an example about pre actions with Mongoose (or a complete plugin which does all, if it exists)?</p>
  
  <p>Regards</p>