|
| 1 | +// TODO: it would be great to move release logic from another factories/resolvers to this class |
| 2 | +import type { Collection, Db, ObjectId } from 'mongodb'; |
| 3 | +import type { ReleaseDBScheme } from '@hawk.so/types'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Interface representing how release files are stored in the DB |
| 7 | + */ |
| 8 | +export interface ReleaseFileDBScheme { |
| 9 | + /** |
| 10 | + * File's id |
| 11 | + */ |
| 12 | + _id: ObjectId; |
| 13 | + |
| 14 | + /** |
| 15 | + * File length in bytes |
| 16 | + */ |
| 17 | + length: number; |
| 18 | + |
| 19 | + /** |
| 20 | + * File upload date |
| 21 | + */ |
| 22 | + uploadDate: Date; |
| 23 | + |
| 24 | + /** |
| 25 | + * File chunk size |
| 26 | + */ |
| 27 | + chunkSize: number; |
| 28 | + |
| 29 | + /** |
| 30 | + * File map name |
| 31 | + */ |
| 32 | + filename: string; |
| 33 | + |
| 34 | + /** |
| 35 | + * File MD5 hash |
| 36 | + */ |
| 37 | + md5: string; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * ReleasesFactory |
| 42 | + * Helper for accessing releases collection |
| 43 | + */ |
| 44 | +export default class ReleasesFactory { |
| 45 | + /** |
| 46 | + * DataBase collection to work with |
| 47 | + */ |
| 48 | + private readonly collection: Collection<ReleaseDBScheme>; |
| 49 | + private readonly filesCollection: Collection<ReleaseFileDBScheme>; |
| 50 | + |
| 51 | + /** |
| 52 | + * Creates releases factory instance |
| 53 | + * @param dbConnection - connection to Events DB |
| 54 | + */ |
| 55 | + constructor(dbConnection: Db) { |
| 56 | + this.collection = dbConnection.collection<ReleaseDBScheme>('releases'); |
| 57 | + this.filesCollection = dbConnection.collection<ReleaseFileDBScheme>('releases.files'); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Find one release document by projectId and release label. |
| 62 | + * Tries both exact string match and numeric fallback (if release can be cast to number). |
| 63 | + */ |
| 64 | + public async findByProjectAndRelease( |
| 65 | + projectId: string | ObjectId, |
| 66 | + release: string |
| 67 | + ): Promise<ReleaseDBScheme | null> { |
| 68 | + const projectIdStr = projectId.toString(); |
| 69 | + |
| 70 | + // Try exact match as stored |
| 71 | + let doc = await this.collection.findOne({ |
| 72 | + projectId: projectIdStr, |
| 73 | + release: release as ReleaseDBScheme['release'], |
| 74 | + }); |
| 75 | + |
| 76 | + // Fallback if release stored as number |
| 77 | + if (!doc) { |
| 78 | + const asNumber = Number(release); |
| 79 | + |
| 80 | + if (!Number.isNaN(asNumber)) { |
| 81 | + doc = await this.collection.findOne({ |
| 82 | + projectId: projectIdStr, |
| 83 | + release: asNumber as unknown as ReleaseDBScheme['release'], |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return doc; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Find files by file ids |
| 93 | + * @param fileIds - file ids |
| 94 | + * @returns files |
| 95 | + */ |
| 96 | + public async findFilesByFileIds(fileIds: ObjectId[]): Promise<ReleaseFileDBScheme[]> { |
| 97 | + return this.filesCollection.find({ _id: { $in: fileIds } }).toArray(); |
| 98 | + } |
| 99 | +} |
0 commit comments