-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.ts
68 lines (58 loc) · 1.47 KB
/
models.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
///<reference path='node/node.d.ts' />
var mongoose: any = require('mongoose');
// Initialize the uri for MongoDB
var uri = process.env.MONGOLAB_URI || 'mongodb://localhost/almond-choco'
console.log("DB Connecting: " + uri)
// Connect MongoDB (mongodb:#[hostname]/[dbname])
var db = mongoose.createConnection(uri)
console.log("DB Connected: " + uri)
// Define schemas
var Schema = mongoose.Schema
var CommentSchema = new Schema({
text: String,
date: Date
});
var TestCaseSchema = new Schema({
input: String,
output: String
});
var SubmitSchema = new Schema({
code: String,
comments: [CommentSchema],
date: Date,
});
var ProblemSchema = new Schema({
title: String,
description: String,
testCases: [TestCaseSchema],
comments: [CommentSchema],
submits: [SubmitSchema],
date: Date,
});
var UserSchema = new Schema({
mailAddress: String,
password: String,
name: String,
salt: String,
problems: [ProblemSchema]
});
var schemas = [
ProblemSchema,
TestCaseSchema,
SubmitSchema,
UserSchema,
CommentSchema
]
// Set up a logger for mongoose
schemas.forEach(schema => {
schema.pre ('save', next => {
console.log('before save')
next()
});
});
// Initialize model accessors
export var Comment = db.model('Comment', CommentSchema);
export var TestCase = db.model('TestCase', TestCaseSchema);
export var Submit = db.model('Submit', SubmitSchema);
export var Problem = db.model('Problem', ProblemSchema);
export var User = db.model('User', UserSchema);