Skip to content

Commit

Permalink
Initial commit for hello-mongoose: MongoLab MongoDB Mongoose Node.js …
Browse files Browse the repository at this point in the history
…Demo on Heroku
  • Loading branch information
benzenwen committed Nov 17, 2012
0 parents commit 483e1d4
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
lib-cov
*.seed
*.log
*.csv
*.dat
*.out
*.pid
*.gz

.DS_Stor

pids
logs
results

node_modules
npm-debug.log
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: node app.js
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
* hello-mongoose: Mongoose.js: elegant MongoDB object models for Node.js.
=========================

MongoDB and Node.js are often used together because of their shared use of Javascript and its Object Notation (JSON). Mongoose is a popular helper library that provides a more rigorous modeling environment for your data, enforcing a little bit more structure as needed, while still maintaining flexibility that makes MongoDB powerful. In this article, you make a connection to a hosted MongoDB instance at add-on provider MongoLab with Mongoose and model a simple object.

** License

MIT Licensed
133 changes: 133 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
//
// hello-mongoose: MongoDB with Mongoose on Node.js example on Heroku.
// Mongoose is a object/data mapping utility for the MongoDB database.
//

// by Ben Wen with thanks to Aaron Heckmann

//
// Copyright 2012 ObjectLabs Corp.
// ObjectLabs operates MongoLab.com a MongoDb-as-a-Service offering
//
// MIT Licensed
//

// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation files
// (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:

// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

//
// Preamble
var http = require ('http'); // For serving a basic web page.
var mongoose = require ("mongoose"); // The reason for this demo.

// Here we find an appropriate database to connect to, defaulting to
// localhost if we don't find one.
var uristring =
process.env.MONGODB_URI ||
process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/HelloMongoose';

// The http server will listen to an appropriate port, or default to
// port 2000.
var theport = process.env.PORT || 5000;

// MongoDB's default is for speed over safety on writes. Most
// applications should prefer safety over speed, so that's what
// we use below.
var mongoOptions = { db: { safe: true }};

// Makes connection asynchronously. Mongoose will queue up database
// operations and release them when the connection is complete.
mongoose.connect(uristring, mongoOptions, function (err, res) {
if (err) {
console.log ('ERROR connecting to: ' + uristring + '. ' + err);
} else {
console.log ('Succeeded connected to: ' + uristring);
}
});

// This is the schema. Note the types, validation and trim
// statements. They enforce useful constraints on the data.
var userSchema = new mongoose.Schema({
name: {
last: String,
first: { type: String, trim: true }
},
age: { type: Number, min: 0}
});

// Instantiating an instance of the model, opening (or creating, if
// nonexistent) the 'PowerUsers' collection in the MongoDB database
var PUser = mongoose.model('PowerUsers', userSchema);

// Creating one user.
var johndoe = new PUser ({
name: { last: 'John', first: ' Doe ' },
age: Math.floor(Math.random()*91+1)
});

// Saving it to the database. If you restart and run this code
// multiple times, you will see multiple documents with random ages.
johndoe.save();

// In case the browser connects before the database is connected, the
// user will see this message.
var found = ['DB Connection not yet established. Try again later. Check the console output for error messages if this persists.'];

// Create a rudimentary http server. (Note, a real web application
// would use a complete web framework and router like express.js).
// This is effectively the main interaction loop for the application.
// As new http requests arrive, the callback function gets invoked.
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
// Lets find all the documents
PUser.find({}).exec(function(err, result) {
if (!err) {
res.end(html1 + result.length + html2 + JSON.stringify(result, undefined, 2) + html3);
} else {
res.end('Error in query. ' + err)
};
});
}).listen(theport);

// Tell the console we're getting ready.
// The listener in http.createServer should still be active after these messages are emitted.
console.log('http server will be listening on port %d', theport);
console.log('CTRL+C to exit');

// DONE.

//
// House keeping.

//
// The rudimentary HTML content in three pieces.
var html1 = '<title> hello-mongoose: MongoLab MongoDB Mongoose Node.js Demo on Heroku </title> \
<head> \
<style> body {color: #394a5f; font-family: sans-serif} </style> \
</head> \
<h1> hello-mongoose: MongoLab MongoDB Mongoose Node.js Demo on Heroku </h1> \
<br\> \
There are: ';
var html2 = ' documents. <br\> <br\> <h2> Documents in MonogoDB database </h2> <pre><code>';
var html3 = '</code></pre> <br\> <br\> <center><i> Demo code available at <a href="http://github.com/mongolab/hello-mongoose">github.com</a> </i></center>';


17 changes: 17 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "hello-mongoose",
"version": "0.0.0",
"description": "MongoDB with Mongoose on Node.js example",
"main": "app.js",
"engines" : {
"node" : "0.8",
"npm" : ">=1.1"
},
"repository": "",
"author": "Ben Wen",
"license": "MIT",
"dependencies": {
"mongoose": "3.3.1"
}
}

0 comments on commit 483e1d4

Please sign in to comment.