Created
March 22, 2012 18:33
-
-
Save kapouer/2161388 to your computer and use it in GitHub Desktop.
strict bug
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
var mongoose = require('mongoose') | |
, Schema = mongoose.Schema | |
var user = new Schema({ | |
domain: String, | |
token: String | |
}, { strict: true }); | |
user.statics.populate = function(obj, cb) { | |
var self = this; | |
self.update({}, {token:null}, {multi: true}, function() { | |
var keys = Object.keys(obj); | |
var tonum = keys.length; | |
keys.forEach(function(k) { | |
self.findOne({domain:k}, function(err, row) { | |
if (!row) row = new User({domain:k}); | |
row.token = obj[k]; | |
row.save(function() { | |
tonum--; | |
if (tonum == 0) self.find({token:null}).remove(); | |
}); | |
}); | |
}); | |
}); | |
}; | |
var User = mongoose.model('User', user); | |
exports.User = User; |
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
var mongoose = require('mongoose') | |
, Schema = mongoose.Schema | |
var message = new Schema({ | |
_creator : { type: Schema.ObjectId, ref: 'Page', index: true }, | |
date : { type: Date, default: Date.now }, | |
title : String, | |
style : String, | |
text : String | |
}, { strict: true }); | |
var Message = mongoose.model('Message', message); | |
exports.Message = Message; | |
var page = new Schema({ | |
domain : {type: String, index: true}, | |
key : {type: String, index: true}, | |
title : String, | |
start : Date, | |
stop : Date, | |
last : Date, | |
messages: [{ type: Schema.ObjectId, ref: 'Message' }] | |
}, { strict: true }); | |
page.virtual('when').get(function() { | |
var now = (new Date()).getTime(); | |
if (!this.start || this.start.getTime() > now) return -1; // before | |
if (!this.stop || this.stop.getTime() > now) return 0; // during | |
return 1; // after | |
}); | |
page.statics.have = function(domain, key, cb) { | |
var obj = { | |
key: key, | |
domain: domain | |
}; | |
return this.findOne(obj, function(err, row) { | |
if (!row) { | |
row = new Page(obj); | |
row.save(); | |
} | |
cb(err, row); | |
}); | |
}; | |
var Page = mongoose.model('Page', page); | |
exports.Page = Page; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment