import { Injectable } from '@nestjs/common'; import { ExpressionBuilder, Insertable, Kysely, Updateable } from 'kysely'; import { jsonObjectFrom } from 'kysely/helpers/postgres'; import { InjectKysely } from 'nestjs-kysely'; import { DB, Libraries } from 'src/db'; import { DummyValue, GenerateSql } from 'src/decorators'; import { LibraryStatsResponseDto } from 'src/dtos/library.dto'; import { LibraryEntity } from 'src/entities/library.entity'; import { AssetType } from 'src/enum'; import { ILibraryRepository } from 'src/interfaces/library.interface'; const userColumns = [ 'users.id', 'users.email', 'users.createdAt', 'users.profileImagePath', 'users.isAdmin', 'users.shouldChangePassword', 'users.deletedAt', 'users.oauthId', 'users.updatedAt', 'users.storageLabel', 'users.name', 'users.quotaSizeInBytes', 'users.quotaUsageInBytes', 'users.status', 'users.profileChangedAt', ] as const; const withOwner = (eb: ExpressionBuilder) => { return jsonObjectFrom(eb.selectFrom('users').whereRef('users.id', '=', 'libraries.ownerId').select(userColumns)).as( 'owner', ); }; @Injectable() export class LibraryRepository implements ILibraryRepository { constructor(@InjectKysely() private db: Kysely) {} @GenerateSql({ params: [DummyValue.UUID] }) get(id: string, withDeleted = false): Promise { return this.db .selectFrom('libraries') .selectAll('libraries') .select(withOwner) .where('libraries.id', '=', id) .$if(!withDeleted, (qb) => qb.where('libraries.deletedAt', 'is', null)) .executeTakeFirst() as Promise; } @GenerateSql({ params: [] }) getAll(withDeleted = false): Promise { return this.db .selectFrom('libraries') .selectAll('libraries') .select(withOwner) .orderBy('createdAt', 'asc') .$if(!withDeleted, (qb) => qb.where('libraries.deletedAt', 'is', null)) .execute() as unknown as Promise; } @GenerateSql() getAllDeleted(): Promise { return this.db .selectFrom('libraries') .selectAll('libraries') .select(withOwner) .where('libraries.deletedAt', 'is not', null) .orderBy('createdAt', 'asc') .execute() as unknown as Promise; } create(library: Insertable): Promise { return this.db .insertInto('libraries') .values(library) .returningAll() .executeTakeFirstOrThrow() as Promise; } async delete(id: string): Promise { await this.db.deleteFrom('libraries').where('libraries.id', '=', id).execute(); } async softDelete(id: string): Promise { await this.db.updateTable('libraries').set({ deletedAt: new Date() }).where('libraries.id', '=', id).execute(); } update(id: string, library: Updateable): Promise { return this.db .updateTable('libraries') .set(library) .where('libraries.id', '=', id) .returningAll() .executeTakeFirstOrThrow() as Promise; } @GenerateSql({ params: [DummyValue.UUID] }) async getStatistics(id: string): Promise { const stats = await this.db .selectFrom('libraries') .innerJoin('assets', 'assets.libraryId', 'libraries.id') .innerJoin('exif', 'exif.assetId', 'assets.id') .select((eb) => eb.fn .count('assets.id') .filterWhere((eb) => eb.and([eb('assets.type', '=', AssetType.IMAGE), eb('assets.isVisible', '=', true)])) .as('photos'), ) .select((eb) => eb.fn .countAll() .filterWhere((eb) => eb.and([eb('assets.type', '=', AssetType.VIDEO), eb('assets.isVisible', '=', true)])) .as('videos'), ) .select((eb) => eb.fn.coalesce((eb) => eb.fn.sum('exif.fileSizeInByte'), eb.val(0)).as('usage')) .groupBy('libraries.id') .where('libraries.id', '=', id) .executeTakeFirst(); if (!stats) { return; } return { photos: Number(stats.photos), videos: Number(stats.videos), usage: Number(stats.usage), total: Number(stats.photos) + Number(stats.videos), }; } }