Added message data model

This commit is contained in:
Chris Blanchard 2015-08-05 01:41:43 +01:00
parent 4dc825ef05
commit cf29e50842
5 changed files with 94 additions and 1 deletions

12
db/index.js Normal file
View file

@ -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;

26
db/models/message.js Normal file
View file

@ -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);

View file

@ -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",

View file

@ -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 () {

41
spec/message.js Normal file
View file

@ -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();
});
});
});
});