mongoosejs: setters schema

According to mongoose.js doc, it is able to set setters to a field on schema.
However, the doc is not quite detailed and obselete.
Some example:

1
2
3
4
5
6
7
8
9
10
11
var schema=new Schema({
password:{
type:String,
required:true,
set:hash
}
})
function hash(plainPwd){
return require("crypto").createHash("sha1").update(plainPwd).update(secret).digest("hex");
}

The hash will be called mainly on following scenarios:

  • When a new doc being created.

    1
    model.create({password:"12345"}) //password will be hashed
  • When set a value to a doc.

    1
    doc.password="22222" // 22222 will be hashed

However, this will not work for update query:

1
2
3
model.update({_id:<id>},{$set:{password:"12345"}}) // password will not be hashed
model.findOneAndUpdate
model.findAndUpdate

For password, it is able to write beforeUpdateHook

1
2
3
4
5
6
7
8
schema.methods.beforeUpdateHook=function(data){
if (!data || this.password === data.password){
return ;
}
if (data.password){
data.password=hash(data.password);
}
}

wordpress reverse proxy headers

When put wordpress blog behind a reverse proxy (e.g. nginx), some headers have to be setup:

1
2
3
4
5
6
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header HTTP_X_FORWARDED_PROTO $scheme;

textarea quick hack

Resize

Switch on / off resize of a textarea on horizontal / vertical / both.

1
<textarea style="resize:horizontal"></textarea>

The resize property can be either:

  • vertical: only resize vertically
  • horizontal: only resize horizontally
  • none: disable resize (also remove the resize area on right bottom.)

Outline

Remove or change style of outline when textarea is focused on safari / chrome etc.

1
<textarea class="customize"></textarea>

1
2
3
.customize:focus{
outline-color:red;
}

Move to Hexo

Today, I moved my blog from Wordpress to Hexo. I use github.io to host the blog.

The reason I choose Hexo not Jekyll mainly is I am more familiar with Node.js than Ruby so I can write plugins easily.

Hexo is a very powerful static content generator which converts MarkDown posts to Html.

more >>