2014-05-13 16:46:56 -04:00
|
|
|
mongoose = require('mongoose')
|
2014-05-19 17:20:50 -04:00
|
|
|
plugins = require('../plugins/plugins')
|
2014-05-13 16:46:56 -04:00
|
|
|
jsonschema = require('../../app/schemas/models/achievement')
|
2014-05-21 13:47:17 -04:00
|
|
|
log = require 'winston'
|
2014-05-13 16:46:56 -04:00
|
|
|
|
|
|
|
# `pre` and `post` are not called for update operations executed directly on the database,
|
|
|
|
# including `Model.update`,`.findByIdAndUpdate`,`.findOneAndUpdate`, `.findOneAndRemove`,and `.findByIdAndRemove`.order
|
|
|
|
# to utilize `pre` or `post` middleware, you should `find()` the document, and call the `init`, `validate`, `save`,
|
|
|
|
# or `remove` functions on the document. See [explanation](http://github.com/LearnBoost/mongoose/issues/964).
|
|
|
|
|
|
|
|
AchievementSchema = new mongoose.Schema({
|
2014-05-21 13:47:17 -04:00
|
|
|
userField: String
|
2014-05-13 16:46:56 -04:00
|
|
|
}, {strict: false})
|
|
|
|
|
2014-05-21 13:47:17 -04:00
|
|
|
AchievementSchema.methods.objectifyQuery = () ->
|
|
|
|
try
|
|
|
|
@set('query', JSON.parse(@get('query'))) if typeof @get('query') == "string"
|
|
|
|
catch error
|
2014-05-24 14:45:53 -04:00
|
|
|
log.error "Couldn't convert query string to object because of #{error}"
|
2014-05-21 13:47:17 -04:00
|
|
|
@set('query', {})
|
|
|
|
|
|
|
|
AchievementSchema.methods.stringifyQuery = () ->
|
|
|
|
@set('query', JSON.stringify(@get('query'))) if typeof @get('query') != "string"
|
|
|
|
|
|
|
|
AchievementSchema.post('init', (doc) -> doc.objectifyQuery())
|
|
|
|
|
|
|
|
AchievementSchema.pre('save', (next) ->
|
|
|
|
@stringifyQuery()
|
|
|
|
next()
|
|
|
|
)
|
|
|
|
|
2014-05-19 17:20:50 -04:00
|
|
|
AchievementSchema.plugin(plugins.SearchablePlugin, {searchable: ['name']})
|
|
|
|
AchievementSchema.plugin(plugins.NamedPlugin)
|
|
|
|
|
2014-05-18 10:04:50 -04:00
|
|
|
module.exports = Achievement = mongoose.model('Achievement', AchievementSchema)
|