rewrite & api: use mongoose populate to replace group.members array with
popualte member objects. this puts a load on the front-end, we'll need to optimize! also added some duplicates sanitization.
This commit is contained in:
+43
-18
@@ -1,18 +1,10 @@
|
||||
var GroupSchema, Schema, helpers, mongoose, _;
|
||||
var mongoose = require("mongoose");
|
||||
var Schema = mongoose.Schema;
|
||||
var helpers = require('habitrpg-shared/script/helpers');
|
||||
var _ = require('lodash');
|
||||
|
||||
mongoose = require("mongoose");
|
||||
|
||||
Schema = mongoose.Schema;
|
||||
|
||||
helpers = require('habitrpg-shared/script/helpers');
|
||||
|
||||
_ = require('lodash');
|
||||
|
||||
GroupSchema = new Schema({
|
||||
_id: {
|
||||
type: String,
|
||||
'default': helpers.uuid
|
||||
},
|
||||
var GroupSchema = new Schema({
|
||||
_id: {type: String, 'default': helpers.uuid},
|
||||
name: String,
|
||||
description: String,
|
||||
leader: {
|
||||
@@ -43,7 +35,9 @@ GroupSchema = new Schema({
|
||||
Number: Number,
|
||||
'default': 0
|
||||
},
|
||||
websites: Array,
|
||||
chat: Array,
|
||||
|
||||
/*
|
||||
# [{
|
||||
# timestamp: Date
|
||||
@@ -58,10 +52,41 @@ GroupSchema = new Schema({
|
||||
balance: Number,
|
||||
logo: String,
|
||||
leaderMessage: String
|
||||
}, {
|
||||
strict: 'throw'
|
||||
});
|
||||
}, {strict: 'throw'});
|
||||
|
||||
|
||||
/**
|
||||
* Derby duplicated stuff. This is a temporary solution, once we're completely off derby we'll run an mongo migration
|
||||
* to remove duplicates, then take these fucntions out
|
||||
*/
|
||||
function removeDuplicates(doc){
|
||||
// Remove duplicate members
|
||||
if (doc.members) {
|
||||
var uniqMembers = _.uniq(doc.members);
|
||||
if (uniqMembers.length != doc.members.length) {
|
||||
doc.members = uniqMembers;
|
||||
}
|
||||
}
|
||||
|
||||
if (doc.websites) {
|
||||
var uniqWebsites = _.uniq(doc.websites);
|
||||
if (uniqWebsites.length != doc.websites.length) {
|
||||
doc.websites = uniqWebsites;
|
||||
}
|
||||
console.log(doc.websites);
|
||||
}
|
||||
}
|
||||
|
||||
GroupSchema.pre('save', function(next){
|
||||
removeDuplicates(this);
|
||||
next();
|
||||
})
|
||||
|
||||
GroupSchema.methods.toJSON = function(){
|
||||
var doc = this.toObject();
|
||||
removeDuplicates(doc);
|
||||
return doc;
|
||||
}
|
||||
|
||||
module.exports.schema = GroupSchema;
|
||||
|
||||
module.exports.model = mongoose.model("Group", GroupSchema);
|
||||
+13
-26
@@ -1,14 +1,9 @@
|
||||
var Schema, UserSchema, helpers, mongoose, _;
|
||||
var mongoose = require("mongoose");
|
||||
var Schema = mongoose.Schema;
|
||||
var helpers = require('habitrpg-shared/script/helpers');
|
||||
var _ = require('lodash');
|
||||
|
||||
mongoose = require("mongoose");
|
||||
|
||||
Schema = mongoose.Schema;
|
||||
|
||||
helpers = require('habitrpg-shared/script/helpers');
|
||||
|
||||
_ = require('lodash');
|
||||
|
||||
UserSchema = new Schema({
|
||||
var UserSchema = new Schema({
|
||||
_id: {
|
||||
type: String,
|
||||
'default': helpers.uuid
|
||||
@@ -274,20 +269,16 @@ UserSchema.post('init', function(doc) {
|
||||
|
||||
|
||||
UserSchema.methods.toJSON = function() {
|
||||
var doc;
|
||||
doc = this.toObject();
|
||||
var doc = this.toObject();
|
||||
doc.id = doc._id;
|
||||
_.each(['habit', 'daily', 'todo', 'reward'], function(type) {
|
||||
/* we use _.transform instead of a simple _.where in order to maintain sort-order*/
|
||||
|
||||
return doc["" + type + "s"] = _.transform(doc["" + type + "Ids"], function(result, tid) {
|
||||
return result.push(doc.tasks[tid]);
|
||||
// we use _.transform instead of a simple _.where in order to maintain sort-order
|
||||
doc["" + type + "s"] = _.transform(doc["" + type + "Ids"], function(result, tid) {
|
||||
result.push(doc.tasks[tid]);
|
||||
});
|
||||
/*delete doc["#{type}Ids"]*/
|
||||
|
||||
//delete doc["#{type}Ids"]
|
||||
});
|
||||
/*delete doc.tasks*/
|
||||
|
||||
//delete doc.tasks
|
||||
doc.filters = {};
|
||||
return doc;
|
||||
};
|
||||
@@ -296,16 +287,12 @@ UserSchema.methods.toJSON = function() {
|
||||
# FIXME - since we're using special @post('init') above, we need to flag when the original path was modified.
|
||||
# Custom setter/getter virtuals?
|
||||
*/
|
||||
|
||||
|
||||
UserSchema.pre('save', function(next) {
|
||||
this.markModified('tasks');
|
||||
/*our own version incrementer*/
|
||||
|
||||
//our own version incrementer
|
||||
this._v++;
|
||||
return next();
|
||||
next();
|
||||
});
|
||||
|
||||
module.exports.schema = UserSchema;
|
||||
|
||||
module.exports.model = mongoose.model("User", UserSchema);
|
||||
Reference in New Issue
Block a user