Last active
August 29, 2015 14:13
-
-
Save nihaux/395c600879c5d68c5011 to your computer and use it in GitHub Desktop.
mongoose schemas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* Module dependencies. | |
*/ | |
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema, | |
_ = require('lodash'), | |
async = require('async'); | |
var AnswerSchema = new Schema({ | |
text: { | |
type: String | |
}, | |
right: { | |
type: Boolean | |
}, | |
assets: [{ | |
type: String | |
}] | |
}); | |
var Answer = mongoose.model('Answer', AnswerSchema); | |
/** | |
* Question Schema | |
*/ | |
var QuestionSchema = new Schema({ | |
created: { | |
type: Date, | |
default: Date.now | |
}, | |
text: { | |
type: String | |
}, | |
answers: [AnswerSchema], | |
theme: [{ | |
type: Schema.Types.ObjectId, | |
ref:'Theme' | |
}], | |
assets: [{ | |
type: String | |
}], | |
tags: [{ | |
type: String, | |
trim: true | |
}] | |
}); | |
mongoose.model('Question', QuestionSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
/** | |
* Module dependencies. | |
*/ | |
var mongoose = require('mongoose'), | |
Schema = mongoose.Schema; | |
/** | |
* Theme Schema | |
*/ | |
var ThemeSchema = new Schema({ | |
children: [ThemeSchema], | |
name: { | |
type: String, | |
default: '', | |
required: 'Please fill Theme name', | |
trim: true | |
}, | |
created: { | |
type: Date, | |
default: Date.now | |
}, | |
description: { | |
type: String, | |
default: '' | |
}, | |
iconUrl: { | |
type: String | |
}, | |
backgroundUrl: { | |
type: String | |
}, | |
cardUrl: { | |
type: String | |
}, | |
available: { | |
type: Boolean | |
} | |
}); | |
ThemeSchema.statics.isValidForGame = function isValidForGame (themeId, callback) { | |
mongoose.model('Theme').find({'_id': themeId, 'available': true}).exec(function(err, theme) { | |
if (err) { | |
callback(err, false); | |
return; | |
} | |
if (theme.length === 0) { | |
callback(null, false); | |
return; | |
} | |
mongoose.model('Question').find({'theme': themeId}).exec(function(err, questions) { | |
if (err) { | |
callback(err, false); | |
return; | |
} | |
if (questions.length === 0) { | |
callback(null, false); | |
return; | |
} | |
callback(null,theme[0]); | |
}); | |
}); | |
}; | |
mongoose.model('Theme', ThemeSchema); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment