mongoose-taggable-via-redis
Version:
a mongoose plugin provide fast and flexible tagging ablity to datamodle via redis, supports scope tagging
148 lines (138 loc) • 5.02 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var assert, debuglog, exports, findCallbackFromArguments, getIdFromResult, mongoose, taggable, util;
taggable = require("taggable-via-redis");
assert = require("assert");
debuglog = require("debug")("mongoose-taggable");
mongoose = require('mongoose');
util = require("util");
getIdFromResult = function(result) {
return result._id || result.id;
};
findCallbackFromArguments = function(args) {
var i, theCallback, _i, _ref;
theCallback = null;
for (i = _i = 0, _ref = args.length; 0 <= _ref ? _i < _ref : _i > _ref; i = 0 <= _ref ? ++_i : --_i) {
if ("function" === typeof args[i]) {
return i;
}
}
return -1;
};
module.exports = exports = function(schema, options) {
assert(schema, "missing argument: schema");
assert(options, "missing argument: options");
assert(options.taggable, "missing argument: options.taggable");
if (options.redisClient) {
taggable.init(options.redisClient);
}
schema["__taggable"] = options;
schema.virtual('tags').set(function(val) {
return this._tags = val;
}).get(function() {
return this._tags;
});
schema.set('toJSON', {
virtuals: true
});
schema.set('toObject', {
virtuals: true
});
schema.methods.setTags = function(tags, callback) {
var scope;
debuglog("[setTags] moduleName:" + options.taggable + ", id:" + this.id + ", tags:" + tags);
scope = options.getScope ? options.getScope.apply(this) : null;
taggable.set(options.taggable, this.id, tags, scope, callback);
};
schema.statics['popularTags'] = function(count, scope, callback) {
debuglog("[popularTags] count:" + count);
taggable.popular(options.taggable, count, scope, callback);
};
schema.statics['findByTags'] = function(tags, query, scope, callback) {
var _this = this;
debuglog("[findByTags] tags:" + tags + ", scope:" + scope);
if ('function' === typeof scope) {
callback = scope;
scope = null;
} else if ('function' === typeof query) {
callback = query;
scope = null;
query = null;
}
taggable.find(options.taggable, tags, scope, function(err, ids) {
if (err != null) {
return callback(err);
}
query = (query || _this).where({
_id: {
$in: ids
}
});
query.execWithTag(callback);
});
};
schema.statics['findWithTags'] = function(conditions, callback) {
debuglog("[findWithTags]");
if ("function" === typeof conditions) {
callback = conditions;
conditions = {};
}
this.find(conditions, function(err, results) {
var ids, scope;
if (err != null) {
return typeof callback === "function" ? callback(err) : void 0;
}
if (!(Array.isArray(results) && results.length > 0)) {
return typeof callback === "function" ? callback(null, results) : void 0;
}
ids = results.map(getIdFromResult);
scope = options.getScope ? options.getScope.apply(results[0]) : null;
debuglog("[findWithTags] ids:" + ids + ", scope:" + scope);
return taggable.get(options.taggable, ids, scope, function(err, tagsArray) {
var i, object, _i, _len;
if (err != null) {
return typeof callback === "function" ? callback(err) : void 0;
}
for (i = _i = 0, _len = results.length; _i < _len; i = ++_i) {
object = results[i];
object.tags = tagsArray[i];
}
if (typeof callback === "function") {
callback(null, results);
}
});
});
};
schema.post('remove', function(record) {
debuglog("[on remove] record:" + record);
record.setTags(null);
});
mongoose.Query.prototype.execWithTag = function(callback) {
var taggableOptions,
_this = this;
taggableOptions = this.model.schema.__taggable;
this.exec(function(err, results) {
var ids, scope;
if (err != null) {
return typeof callback === "function" ? callback(err) : void 0;
}
if (!(Array.isArray(results) && results.length > 0)) {
return typeof callback === "function" ? callback(null, results) : void 0;
}
ids = results.map(getIdFromResult);
scope = taggableOptions.getScope ? taggableOptions.getScope.apply(results[0]) : null;
taggable.get(taggableOptions.taggable, ids, scope, function(err, tags) {
var i, object, _i, _len;
if (err != null) {
return typeof callback === "function" ? callback(err) : void 0;
}
for (i = _i = 0, _len = results.length; _i < _len; i = ++_i) {
object = results[i];
object.tags = tags[i];
}
callback(null, results);
});
});
};
};
}).call(this);