summaryrefslogtreecommitdiff
path: root/node_modules/mongoose/examples
diff options
context:
space:
mode:
authorJules Laplace <jules@okfoc.us>2012-09-24 16:22:07 -0400
committerJules Laplace <jules@okfoc.us>2012-09-24 16:22:07 -0400
commit686106d544ecc3b6ffd4db2b665d3bc879a58d8c (patch)
treea5b5e50237cef70e12f0745371896e96f5f6d578 /node_modules/mongoose/examples
ok
Diffstat (limited to 'node_modules/mongoose/examples')
-rw-r--r--node_modules/mongoose/examples/schema.js102
1 files changed, 102 insertions, 0 deletions
diff --git a/node_modules/mongoose/examples/schema.js b/node_modules/mongoose/examples/schema.js
new file mode 100644
index 0000000..d108d05
--- /dev/null
+++ b/node_modules/mongoose/examples/schema.js
@@ -0,0 +1,102 @@
+
+/**
+ * Module dependencies.
+ */
+
+var mongoose = require('mongoose')
+ , Schema = mongoose.Schema;
+
+/**
+ * Schema definition
+ */
+
+// recursive embedded-document schema
+
+var Comment = new Schema();
+
+Comment.add({
+ title : { type: String, index: true }
+ , date : Date
+ , body : String
+ , comments : [Comment]
+});
+
+var BlogPost = new Schema({
+ title : { type: String, index: true }
+ , slug : { type: String, lowercase: true, trim: true }
+ , date : Date
+ , buf : Buffer
+ , comments : [Comment]
+ , creator : Schema.ObjectId
+});
+
+var Person = new Schema({
+ name: {
+ first: String
+ , last : String
+ }
+ , email: { type: String, required: true, index: { unique: true, sparse: true } }
+ , alive: Boolean
+});
+
+/**
+ * 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();
+});
+
+/**
+ * Methods
+ */
+
+BlogPost.methods.findCreator = function (callback) {
+ return this.db.model('Person').findById(this.creator, callback);
+}
+
+BlogPost.statics.findByTitle = function (title, callback) {
+ return this.find({ title: title }, callback);
+}
+
+BlogPost.methods.expressiveQuery = function (creator, date, callback) {
+ return this.find('creator', creator).where('date').gte(date).run(callback);
+}
+
+/**
+ * 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.
+ */
+
+mongoose.model('BlogPost', BlogPost);
+mongoose.model('Person', Person);