import { EntityRepository, Repository } from 'typeorm'; import { DriveFolders, DriveFiles } from '../index'; import { DriveFolder } from '@/models/entities/drive-folder'; import { awaitAll } from '@/prelude/await-all'; import { Packed } from '@/misc/schema'; @EntityRepository(DriveFolder) export class DriveFolderRepository extends Repository { public async pack( src: DriveFolder['id'] | DriveFolder, options?: { detail: boolean } ): Promise> { const opts = Object.assign({ detail: false, }, options); const folder = typeof src === 'object' ? src : await this.findOneOrFail(src); return await awaitAll({ id: folder.id, createdAt: folder.createdAt.toISOString(), name: folder.name, parentId: folder.parentId, ...(opts.detail ? { foldersCount: DriveFolders.count({ parentId: folder.id, }), filesCount: DriveFiles.count({ folderId: folder.id, }), ...(folder.parentId ? { parent: this.pack(folder.parentId, { detail: true, }), } : {}), } : {}), }); } }