|
1 | | -import { Collection, Db } from 'mongodb'; |
2 | | -import { ReleaseDBScheme } from '@hawk.so/types'; |
| 1 | +import { Collection, Db, ObjectId } from 'mongodb'; |
| 2 | +import { ReleaseDBScheme, SourceMapFileChunk } from '@hawk.so/types'; |
3 | 3 | import DataLoaders from '../dataLoaders'; |
4 | 4 |
|
| 5 | +interface ReleaseWithFileDetails extends ReleaseDBScheme { |
| 6 | + fileDetails?: SourceMapFileChunk[]; |
| 7 | +} |
| 8 | + |
5 | 9 | export default class ReleasesFactory { |
6 | 10 | /** |
7 | 11 | * Releases collection |
@@ -37,12 +41,54 @@ export default class ReleasesFactory { |
37 | 41 | } |
38 | 42 |
|
39 | 43 | /** |
40 | | - * Get releases by project identifier |
| 44 | + * Get releases by project identifier with file sizes |
41 | 45 | * @param projectId - project identifier |
42 | 46 | */ |
43 | 47 | public async findManyByProjectId(projectId: string): Promise<ReleaseDBScheme[]> { |
44 | 48 | try { |
45 | | - return await this.collection.find({ projectId: projectId }).toArray(); |
| 49 | + const releases = await this.collection.aggregate<ReleaseWithFileDetails>([ |
| 50 | + { |
| 51 | + $match: { |
| 52 | + projectId: projectId |
| 53 | + } |
| 54 | + }, |
| 55 | + { |
| 56 | + $lookup: { |
| 57 | + from: 'releases.files', |
| 58 | + let: { fileIds: '$files._id' }, |
| 59 | + pipeline: [ |
| 60 | + { |
| 61 | + $match: { |
| 62 | + $expr: { |
| 63 | + $in: ['$_id', '$$fileIds'] |
| 64 | + } |
| 65 | + } |
| 66 | + }, |
| 67 | + { |
| 68 | + $project: { |
| 69 | + _id: 1, |
| 70 | + length: 1, |
| 71 | + chunkSize: 1 |
| 72 | + } |
| 73 | + } |
| 74 | + ], |
| 75 | + as: 'fileDetails' |
| 76 | + } |
| 77 | + } |
| 78 | + ]).toArray(); |
| 79 | + |
| 80 | + return releases.map(release => ({ |
| 81 | + ...release, |
| 82 | + files: release.files?.map(file => { |
| 83 | + const fileDetail = release.fileDetails?.find( |
| 84 | + (detail: SourceMapFileChunk) => detail._id.toString() === file._id?.toString() |
| 85 | + ); |
| 86 | + return { |
| 87 | + ...file, |
| 88 | + size: fileDetail ? fileDetail.length : 0 |
| 89 | + }; |
| 90 | + }) |
| 91 | + })); |
46 | 92 | } catch (error) { |
47 | 93 | console.error(`[ReleasesFactory] Error in findManyByProjectId:`, error); |
48 | 94 | throw error; |
|
0 commit comments