Yuki Takei 2 лет назад
Родитель
Сommit
192da4ec84
2 измененных файлов с 26 добавлено и 21 удалено
  1. 23 17
      apps/app/src/server/models/page-tag-relation.ts
  2. 3 4
      apps/app/src/server/models/tag.ts

+ 23 - 17
apps/app/src/server/models/page-tag-relation.ts

@@ -1,13 +1,15 @@
 import type { ITag } from '@growi/core';
 import type { ITag } from '@growi/core';
 import type { Document, Model } from 'mongoose';
 import type { Document, Model } from 'mongoose';
-import mongoose from 'mongoose';
+import mongoose, { ObjectId } from 'mongoose';
 import mongoosePaginate from 'mongoose-paginate-v2';
 import mongoosePaginate from 'mongoose-paginate-v2';
 import uniqueValidator from 'mongoose-unique-validator';
 import uniqueValidator from 'mongoose-unique-validator';
 
 
 import type { IPageTagRelation } from '~/interfaces/page-tag-relation';
 import type { IPageTagRelation } from '~/interfaces/page-tag-relation';
 
 
+import type { ObjectIdLike } from '../interfaces/mongoose-utils';
 import { getOrCreateModel } from '../util/mongoose-utils';
 import { getOrCreateModel } from '../util/mongoose-utils';
 
 
+import type { IdToNameMap } from './tag';
 import Tag from './tag';
 import Tag from './tag';
 
 
 
 
@@ -32,15 +34,17 @@ type CreateTagListWithCountResult = {
   data: ITag[],
   data: ITag[],
   totalCount: number
   totalCount: number
 }
 }
-type CreateTagListWithCount = (opts?: CreateTagListWithCountOpts) => Promise<CreateTagListWithCountResult>;
+type CreateTagListWithCount = (this: PageTagRelationModel, opts?: CreateTagListWithCountOpts) => Promise<CreateTagListWithCountResult>;
 
 
-type UpdatePageTags = (pageId: string, tags: string[]) => Promise<void>
+type GetIdToTagNamesMap = (this: PageTagRelationModel, pageIds: string[]) => Promise<IdToNameMap>;
+
+type UpdatePageTags = (this: PageTagRelationModel, pageId: string, tags: string[]) => Promise<void>
 
 
 export interface PageTagRelationModel extends Model<PageTagRelationDocument> {
 export interface PageTagRelationModel extends Model<PageTagRelationDocument> {
   createTagListWithCount: CreateTagListWithCount
   createTagListWithCount: CreateTagListWithCount
   findByPageId(pageId: string, options?: { nullable?: boolean }): Promise<PageTagRelationDocument[]>
   findByPageId(pageId: string, options?: { nullable?: boolean }): Promise<PageTagRelationDocument[]>
   listTagNamesByPage(pageId: string): Promise<PageTagRelationDocument[]>
   listTagNamesByPage(pageId: string): Promise<PageTagRelationDocument[]>
-  getIdToTagNamesMap(): any
+  getIdToTagNamesMap: GetIdToTagNamesMap
   updatePageTags: UpdatePageTags
   updatePageTags: UpdatePageTags
 }
 }
 
 
@@ -73,12 +77,12 @@ schema.index({ relatedPage: 1, relatedTag: 1 }, { unique: true });
 schema.plugin(mongoosePaginate);
 schema.plugin(mongoosePaginate);
 schema.plugin(uniqueValidator);
 schema.plugin(uniqueValidator);
 
 
-const createTagListWithCount: CreateTagListWithCount = async function(opts) {
+const createTagListWithCount: CreateTagListWithCount = async function(this, opts) {
   const sortOpt = opts?.sortOpt || {};
   const sortOpt = opts?.sortOpt || {};
-  const offset = opts?.offset;
+  const offset = opts?.offset ?? 0;
   const limit = opts?.limit;
   const limit = opts?.limit;
 
 
-  const tags = await this.aggregate()
+  let query = this.aggregate()
     .match({ isPageTrashed: false })
     .match({ isPageTrashed: false })
     .lookup({
     .lookup({
       from: 'tags',
       from: 'tags',
@@ -89,12 +93,15 @@ const createTagListWithCount: CreateTagListWithCount = async function(opts) {
     .unwind('$tag')
     .unwind('$tag')
     .group({ _id: '$relatedTag', count: { $sum: 1 }, name: { $first: '$tag.name' } })
     .group({ _id: '$relatedTag', count: { $sum: 1 }, name: { $first: '$tag.name' } })
     .sort(sortOpt)
     .sort(sortOpt)
-    .skip(offset)
-    .limit(limit);
+    .skip(offset);
+
+  if (limit != null) {
+    query = query.limit(limit);
+  }
 
 
   const totalCount = (await this.find({ isPageTrashed: false }).distinct('relatedTag')).length;
   const totalCount = (await this.find({ isPageTrashed: false }).distinct('relatedTag')).length;
 
 
-  return { data: tags, totalCount };
+  return { data: await query.exec(), totalCount };
 };
 };
 schema.statics.createTagListWithCount = createTagListWithCount;
 schema.statics.createTagListWithCount = createTagListWithCount;
 
 
@@ -109,10 +116,8 @@ schema.statics.listTagNamesByPage = async function(pageId) {
   return relations.map((relation) => { return relation.relatedTag.name });
   return relations.map((relation) => { return relation.relatedTag.name });
 };
 };
 
 
-/**
- * @return {object} key: Page._id, value: array of tag names
- */
-schema.statics.getIdToTagNamesMap = async function(pageIds) {
+
+const getIdToTagNamesMap: GetIdToTagNamesMap = async function(this, pageIds) {
   /**
   /**
    * @see https://docs.mongodb.com/manual/reference/operator/aggregation/group/#pivot-data
    * @see https://docs.mongodb.com/manual/reference/operator/aggregation/group/#pivot-data
    *
    *
@@ -123,7 +128,7 @@ schema.statics.getIdToTagNamesMap = async function(pageIds) {
    *   ...
    *   ...
    * ]
    * ]
    */
    */
-  const results = await this.aggregate()
+  const results = await this.aggregate<{ _id: ObjectId, tagIds: ObjectIdLike[] }>()
     .match({ relatedPage: { $in: pageIds } })
     .match({ relatedPage: { $in: pageIds } })
     .group({ _id: '$relatedPage', tagIds: { $push: '$relatedTag' } });
     .group({ _id: '$relatedPage', tagIds: { $push: '$relatedTag' } });
 
 
@@ -145,14 +150,15 @@ schema.statics.getIdToTagNamesMap = async function(pageIds) {
   const idToTagNamesMap = {};
   const idToTagNamesMap = {};
   results.forEach((result) => {
   results.forEach((result) => {
     const tagNames = result.tagIds
     const tagNames = result.tagIds
-      .map(tagId => tagIdToNameMap[tagId])
+      .map(tagId => tagIdToNameMap[tagId.toString()])
       .filter(tagName => tagName != null); // filter null object
       .filter(tagName => tagName != null); // filter null object
 
 
-    idToTagNamesMap[result._id] = tagNames;
+    idToTagNamesMap[result._id.toString()] = tagNames;
   });
   });
 
 
   return idToTagNamesMap;
   return idToTagNamesMap;
 };
 };
+schema.statics.getIdToTagNamesMap = getIdToTagNamesMap;
 
 
 const updatePageTags: UpdatePageTags = async function(pageId, tags) {
 const updatePageTags: UpdatePageTags = async function(pageId, tags) {
   if (pageId == null || tags == null) {
   if (pageId == null || tags == null) {

+ 3 - 4
apps/app/src/server/models/tag.ts

@@ -1,8 +1,7 @@
-import {
-  Types, Model, Schema,
-} from 'mongoose';
+import type { Types, Model } from 'mongoose';
+import { Schema } from 'mongoose';
 
 
-import { ObjectIdLike } from '../interfaces/mongoose-utils';
+import type { ObjectIdLike } from '../interfaces/mongoose-utils';
 import { getOrCreateModel } from '../util/mongoose-utils';
 import { getOrCreateModel } from '../util/mongoose-utils';
 
 
 const mongoosePaginate = require('mongoose-paginate-v2');
 const mongoosePaginate = require('mongoose-paginate-v2');