Skip to content

Instantly share code, notes, and snippets.

@nihaux
Last active August 29, 2015 14:13
Show Gist options
  • Save nihaux/395c600879c5d68c5011 to your computer and use it in GitHub Desktop.
Save nihaux/395c600879c5d68c5011 to your computer and use it in GitHub Desktop.
mongoose schemas
'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);
'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