Created
February 21, 2011 01:39
-
-
Save mcantelon/836544 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Module dependencies. | |
*/ | |
var mongoose = require('mongoose') | |
, Schema = mongoose.Schema | |
mongoose.connect('mongodb://localhost/BB') | |
/** | |
* Schema definition | |
*/ | |
/* | |
var Comments = new Schema() | |
Comments.add({ | |
title : { type: String, index: true } | |
, date : Date | |
, body : String | |
, comments : [Comments] | |
}) | |
*/ | |
var BlogPost = new Schema({ | |
title : String | |
, slug : String | |
, date : Date | |
// , comments : [Comments] | |
}) | |
/** | |
* Accessing a specific schema type by key | |
*/ | |
BlogPost.path('date') | |
.default(function(){ | |
return new Date() | |
}) | |
.set(function(v){ | |
return v == 'now' ? new Date() : v; | |
}); | |
/** | |
* Pre hook. | |
*/ | |
BlogPost.pre('save', function(next, done){ | |
//emailAuthor(done); // some async function | |
next(); | |
}); | |
/** | |
* Plugins | |
*/ | |
function slugGenerator (options){ | |
options = options || {}; | |
var key = options.key || 'title'; | |
return function slugGenerator(schema){ | |
schema.path(key).set(function(v){ | |
this.slug = v.toLowerCase().replace(/[^a-z0-9]/g, '').replace(/-+/g, ''); | |
return v; | |
}); | |
}; | |
}; | |
BlogPost.plugin(slugGenerator()); | |
/** | |
* Define model. | |
*/ | |
// save as model to MongoDB? | |
mongoose.model('BlogPost', BlogPost); | |
var Post = mongoose.model('BlogPost') | |
var post = new Post() | |
post.title = 'Fuck it' | |
//post.comments.push({"title": "Yeah"}) | |
post.save(function(err) { | |
var result = (err) ? err : "Saved" | |
console.log(result) | |
}) | |
Post.find({}, function(err, docs) { | |
console.log(err) | |
console.log(docs.forEach) | |
mongoose.disconnect() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment