Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Feb 1, 2011
1 parent c3efb72 commit 0f1beb1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
Expand Up @@ -178,19 +178,19 @@ you define (ie: the parameters your function accepts):
- Serial
Serial middleware are defined like:

.pre(method, function (next) {
.pre(method, function (next) {

})
})

They're executed one after the other, when each middleware calls `next`.

- Parallel
Parallel middleware offer more fine-grained flow control, and are defined
like

.pre(method, function (next, done) {
.pre(method, function (next, done) {

})
})

Parallel middleware can `next()` immediately, but the final argument will be
called when all the parallel middleware have called `done()`.
Expand Down
11 changes: 11 additions & 0 deletions docs/getters-setters.md
Expand Up @@ -4,38 +4,49 @@ Getters and Setters
Getters and setters help you change how you get and set the attributes defined by the keys and values in the underlying raw document.

## Setters

Setters allow you to transform the mongoose document's data before it gets to the raw mongodb document and is set as a value on an actual key.

Suppose you are implementing user registration for a website. User provide an email and password, which gets saved to mongodb. The email is a string that you will want to normalize to lower case, in order to avoid one email having more than one account -- e.g., otherwise, avenue@q.com can be registered for 2 accounts via avenue@q.com and AvEnUe@Q.CoM.

You can set up email lower case normalization easily via a Mongoose setter. Note in the following snippet that setters (and also getters) are defined in the `Schema`:

function toLower (v) {
return v.toLowerCase();
}

var UserSchema = new Schema({
email: { type: String, set: toLower }
});

var User = mongoose.model('User', UserSchema);
var user = new User({email: 'AVENUE@Q.COM'});

console.log(user.email); // 'avenue@q.com'


As you can see above, setters allow you to transform the data before it gets to the raw mongodb document and is set as a value on an actual key.

## Getters

Getters allow you to transform the representation of the data as it travels from the raw mongodb document to the value that you see.

Suppose you are storing credit card numbers and you want to hide everything except the last 4 digits to the mongoose user. You can do so by defining a getter in the following way (again, notice that getters are defined in the `Schema`):

function obfuscate (cc) {
return '****-****-****-' + cc.slice(cc.length-4, cc.length);
}

var AccountSchema = new Schema({
creditCardNumber: { type: String, get: obfuscate }
});

var Account = mongoose.model('Account', AccountSchema);

Account.findById( someId, function (err, found) {
console.log(found.creditCardNumber); // '****-****-****-1234'
});

## Summary

Setters are intended to modify the underlying raw data. Getters are intended to transform (but not modify at the raw data level) the underlying raw data into something that the user expects to see. They are both defined in the `Schema` definition.
8 changes: 4 additions & 4 deletions docs/middleware.md
Expand Up @@ -12,19 +12,19 @@ you define (ie: the parameters your function accepts):
- Serial
Serial middleware are defined like:

schema.pre(methodName, function (next) {
schema.pre(methodName, function (next) {

})
})

They're executed one after the other, when each middleware calls `next`.

- Parallel
Parallel middleware offer more fine-grained flow control, and are defined
like

schema.pre(methodName, function (next, done) {
schema.pre(methodName, function (next, done) {

})
})

Parallel middleware can `next()` immediately, but the final argument will be
called when all the parallel middleware have called `done()`.
Expand Down
12 changes: 12 additions & 0 deletions docs/virtuals.md
Expand Up @@ -8,6 +8,7 @@ An example is helpful.

## Example
Take the following schema:

var PersonSchema = new Schema({
name: {
first: String
Expand All @@ -23,26 +24,34 @@ Suppose you want to write `theSituation`'s full name. You could do so via:
console.log( theSituation.name.first + ' ' + theSituation.name.last );

It is more convenient to define a virtual attribute, `name.full`, so you can instead write:

console.log( theSituation.name.full );

To do so, you can declare a virtual attribute on the Schema, `Person`:

PersonSchema.virtual('name.full')
.get( function () {
return this.name.first + this.name.last;
});

So when you get `name.full`, via

theSituation.name.full;

the implementation ends up invoking the getter function

function () {
return this.name.first + this.name.last;
}

and returning it.

It would also be nice to be able to set `this.name.first` and `this.name.last` by setting `this.name.full`. For example, it would be nice if we wanted to change theSituation's `name.first` and `name.last` to 'The' and 'Situation' respectively just by invoking:

theSituation.set('name.full', 'The Situation');

Mongoose allows you to do this, too, via virtual attribute setters. You can define a virtual attribute setter thusly:

PersonSchema.virtual('name.full')
.get( function () {
return this.name.first + this.name.last;
Expand All @@ -55,8 +64,11 @@ Mongoose allows you to do this, too, via virtual attribute setters. You can defi
});

Then, when you invoke:

theSituation.set('name.full', 'The Situation');

and you save the document, then `name.first` and `name.last` will be changed in monbodb, but the mongodb document will not have persisted a `name.full` key or value to the database:

theSituation.save( function (err) {
Person.findById(theSituation._id, function (err, found) {
console.log(found.name.first); // 'The'
Expand Down

0 comments on commit 0f1beb1

Please sign in to comment.