mirror of
https://github.com/wassname/talk.git
synced 2026-07-29 11:28:24 +08:00
Index on Tag for name, item id, item type.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
});
|
||||
|
||||
@@ -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}}) {
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user