Index on Tag for name, item id, item type.

This commit is contained in:
gaba
2017-04-21 10:34:57 -07:00
parent 962a9bd6c9
commit 91af8c2ece
4 changed files with 33 additions and 4 deletions
+2 -2
View File
@@ -8,7 +8,7 @@ const TagModel = require('../../models/tag');
/**
* Gets tags based on their item id's.
*/
const genTagsByItemID = (_, item_ids) => {
const getByItemID = (_, item_ids) => {
return TagsService
.findByItemIdArray(item_ids)
.then(util.arrayJoinBy(item_ids, 'item_id'));
@@ -31,7 +31,7 @@ const getItemIdsByItemType = (_, item_type) => {
*/
module.exports = (context) => ({
Tags: {
getByID: new DataLoader((ids) => genTagsByItemID(context, ids)),
getByID: new DataLoader((ids) => getByItemID(context, ids)),
getByTypes: ({item_type}) => getItemIdsByItemType(context, item_type)
}
});
+5 -2
View File
@@ -6,6 +6,9 @@ const Comment = {
return Comments.get.load(parent_id);
},
tags({id}, _, {loaders: {Tags}}) {
return Tags.getByID.load([id]);
},
user({author_id}, _, {loaders: {Users}}) {
return Users.getByID.load(author_id);
},
@@ -23,9 +26,9 @@ const Comment = {
},
replyCount({id}, {excludeIgnored}, {user, loaders: {Comments}}) {
if (user && excludeIgnored) {
return Comments.countByParentIDPersonalized({id, excludeIgnored});
return Comments.countByParentIDPersonalized({id, excludeIgnored});
}
return Comments.countByParentID.load(id);
return Comments.countByParentID.load(id);
},
actions({id}, _, {user, loaders: {Actions}}) {
+10
View File
@@ -62,6 +62,16 @@ const TagSchema = new Schema({
}
});
// Add the indixies on the tag on an item.
TagSchema.index({
'item_type': 1,
'item_id': 1,
'name': 1
}, {
unique: true,
background: false
});
const Tag = mongoose.model('Tag', TagSchema);
module.exports = Tag;
+16
View File
@@ -28,6 +28,22 @@ module.exports = class TagsService {
});
}
/**
* Finds actions in an array of ids.
* @param {String} ids array of user identifiers (uuid)
*/
static async findByItemIdArray(item_ids) {
let tags = await TagModel.find({
'item_id': {$in: item_ids}
});
if (tags === null) {
return [];
}
return tags;
}
/**
* Add a tag.
* @param {string} name the actual tag