egirlskey/packages/backend/src/models/repositories/drive-folder.ts
syuilo 1c67c26bd8
refactor: migrate to typeorm 3.0 (#8443)
* wip

* wip

* wip

* Update following.ts

* wip

* wip

* wip

* Update resolve-user.ts

* maxQueryExecutionTime

* wip

* wip
2022-03-26 15:34:00 +09:00

42 lines
1 KiB
TypeScript

import { db } from '@/db/postgre.js';
import { DriveFolders, DriveFiles } from '../index.js';
import { DriveFolder } from '@/models/entities/drive-folder.js';
import { awaitAll } from '@/prelude/await-all.js';
import { Packed } from '@/misc/schema.js';
export const DriveFolderRepository = db.getRepository(DriveFolder).extend({
async pack(
src: DriveFolder['id'] | DriveFolder,
options?: {
detail: boolean
}
): Promise<Packed<'DriveFolder'>> {
const opts = Object.assign({
detail: false,
}, options);
const folder = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
return await awaitAll({
id: folder.id,
createdAt: folder.createdAt.toISOString(),
name: folder.name,
parentId: folder.parentId,
...(opts.detail ? {
foldersCount: DriveFolders.countBy({
parentId: folder.id,
}),
filesCount: DriveFiles.countBy({
folderId: folder.id,
}),
...(folder.parentId ? {
parent: this.pack(folder.parentId, {
detail: true,
}),
} : {}),
} : {}),
});
},
});