diff --git a/db/index.js b/db/index.js new file mode 100644 index 0000000..31bb5a4 --- /dev/null +++ b/db/index.js @@ -0,0 +1,12 @@ +"use strict"; + +var path = require("path"); +var mongoose = require("mongoose"); +var config = require(path.join(__dirname, "../config/config.js")); + +mongoose.connect(config.mongo.uri); + +// Load models +require(path.join(__dirname, "/models/message")); + +module.exports = mongoose; \ No newline at end of file diff --git a/db/models/message.js b/db/models/message.js new file mode 100644 index 0000000..17aae02 --- /dev/null +++ b/db/models/message.js @@ -0,0 +1,26 @@ +"use strict"; + +var mongoose = require("mongoose"); +var Schema = mongoose.Schema; + +var messageSchema = new Schema({ + author: { + username: { type: String, required: true }, + avatar: String + }, + content: { type: String, required: true }, + createdAt: { type: Date, default: Date.now, required: true } +}); + +messageSchema.index({ createdAt: -1 }); + +messageSchema.methods.toJson = function () { + return { + id: this.id, + author: this.author, + content: this.content, + createdAt: this.createdAt + }; +}; + +module.exports = mongoose.model('message', messageSchema); diff --git a/package.json b/package.json index aee3701..49fb8c8 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "gulp-watch": "~4.3.4", "javascript-state-machine": "~2.3.5", "lodash": "~3.10.0", - "mongoose": "^4.1.1", + "mongoose": "~4.1.1", "morgan": "~1.6.1", "react-tools": "~0.13.3", "request": "~2.60.0", diff --git a/spec/helpers/index.js b/spec/helpers/index.js index ccba524..a7aa811 100644 --- a/spec/helpers/index.js +++ b/spec/helpers/index.js @@ -16,6 +16,20 @@ var Gather = helpers.Gather = require(path.join(__dirname, "../../lib/gather/gat var Gatherer = helpers.Gatherer = require(path.join(__dirname, "../../lib/gather/gatherer")); +// Mongo & Associated Models +var db = require(path.join(__dirname, "../../db/index")); +var mongoose = require("mongoose"); +var Message = helpers.Message = mongoose.model('message'); + +var async = require("async"); +helpers.clearDb = function (callback) { + async.series([ + function (cb) { + Message.remove({}, cb) + } + ], callback); +} + // Create User Method // Each user will have unique ID, username and steam attributes var createUser = helpers.createUser = (function () { diff --git a/spec/message.js b/spec/message.js new file mode 100644 index 0000000..ed6e57a --- /dev/null +++ b/spec/message.js @@ -0,0 +1,41 @@ +"use strict"; + +var helper = require("./helpers/index.js"); +var Message = helper.Message; +var assert = require("chai").assert; + +describe("Message Model", function () { + var user; + + before(function (done) { + helper.clearDb(done); + }); + + after(function (done) { + helper.clearDb(done); + }); + + beforeEach(function () { + user = helper.createUser(); + }); + + describe(".create", function () { + it ("creates a new message with an author", function (done) { + var content = "Foo"; + Message.create({ + author: { + username: user.username, + avatar: user.avatar + }, + content: content + }, function (error, result) { + if (error) return done(error); + assert.equal(result.author.username, user.username); + assert.equal(result.author.avatar, user.avatar); + assert.equal(result.content, content); + assert.isDefined(result.createdAt); + done(); + }); + }); + }); +}); \ No newline at end of file