Added simple mongoose example to the boilerplate #21

Open
wants to merge 2 commits into from
View
1 initproject.sh
@@ -22,6 +22,7 @@ cp ./templates/views/index.jade ./views/index.jade
cp ./templates/views/layout.jade ./views/layout.jade
cp ./templates/js/script.js ./static/js/script.js
# TODO copy over the models
+cp ./templates/models/post.js ./models/post.js
echo "Setting up the dependancies from NPM..."
npm install
View
3 templates/app/package.json
@@ -8,7 +8,8 @@
"express": "2.5.2",
"jade": "0.20.0",
"mongoose": "2.4.8",
- "socket.io": "0.8.7"
+ "socket.io": "0.8.7",
+ "walk" : "2.0.2"
},
"devDependencies": {
"vows": "0.5.x",
View
6 templates/app/server.js
@@ -2,6 +2,7 @@
var connect = require('connect')
, express = require('express')
, io = require('socket.io')
+ , mongoose = require('mongoose')
, port = (process.env.PORT || 8081);
//Setup Express
@@ -50,6 +51,11 @@ io.sockets.on('connection', function(socket){
});
});
+//Setup Mongoose
+var Schema = mongoose.Schema;
+var ObjectId = Schema.ObjectId;
+require('./models/post.js').make(Schema, mongoose);
+var db = mongoose.connect('mongodb://localhost/boilerplate');
///////////////////////////////////////////
// Routes //
View
22 templates/models/post.js
@@ -0,0 +1,22 @@
+
+// Schema
+function make(Schema, mongoose) {
+
+ CommentSchema = new Schema({
+ body : { type: String }
+ , created : { type: Date, default: Date.now }
+ });
+ mongoose.model('Comment', CommentSchema);
+
+ PostSchema = new Schema({
+ title : { type: String, default: 'New Post' }
+ , body : { type: String }
+ , created : { type: Date, default: Date.now }
+ , updated : { type: Date, default: Date.now }
+ , comments: [CommentSchema]
+ });
+ mongoose.model('Post', PostSchema);
+}
+
+module.exports.make = make;
+