From f37f1f66de657e2d57778310bfad025ea3fe33ae Mon Sep 17 00:00:00 2001 From: lmarkus Date: Wed, 4 Dec 2013 18:03:10 -0800 Subject: [PATCH] Created a Product model --- models/index.js | 0 models/productModel.js | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) delete mode 100644 models/index.js create mode 100644 models/productModel.js diff --git a/models/index.js b/models/index.js deleted file mode 100644 index e69de29..0000000 diff --git a/models/productModel.js b/models/productModel.js new file mode 100644 index 0000000..4762036 --- /dev/null +++ b/models/productModel.js @@ -0,0 +1,34 @@ +'use strict'; + +var mongoose = require('mongoose'); + +var productModel = function () { + + //Define a super simple schema for our products. + var productSchema = mongoose.Schema({ + name: String, + price: Number + }); + + /** + * Verbose toString method + */ + productSchema.methods.whatAmI = function () { + var greeting = this.name ? + 'Hello, I\'m a ' + this.name + ' and I\'m worth $' + this.price + : 'I don\'t have a name :('; + console.log(greeting); + }; + + /** + * Format the price of the product to show a dollar sign, and two decimal places + */ + productSchema.methods.prettyPrice = function () { + return '$' + this.price.toFixed(2); + }; + + return mongoose.model('Product', productSchema); + +}; + +module.exports = new productModel(); \ No newline at end of file