refactor: migrate activity repo to kysely (#15203)

Co-authored-by: Jason Rasmussen <jason@rasm.me>
This commit is contained in:
Daniel Dietzler
2025-01-09 20:31:46 +01:00
committed by GitHub
parent 2e12c46980
commit 1fb2b3f899
4 changed files with 95 additions and 136 deletions

View File

@@ -1,9 +1,12 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ExpressionBuilder, Insertable, Kysely } from 'kysely';
import { jsonObjectFrom } from 'kysely/helpers/postgres';
import { InjectKysely } from 'nestjs-kysely';
import { Activity, DB } from 'src/db';
import { DummyValue, GenerateSql } from 'src/decorators';
import { ActivityEntity } from 'src/entities/activity.entity';
import { IActivityRepository } from 'src/interfaces/activity.interface';
import { IsNull, Repository } from 'typeorm';
import { asUuid } from 'src/utils/database';
export interface ActivitySearch {
albumId?: string;
@@ -12,73 +15,71 @@ export interface ActivitySearch {
isLiked?: boolean;
}
const withUser = (eb: ExpressionBuilder<DB, 'activity'>) => {
return jsonObjectFrom(
eb
.selectFrom('users')
.selectAll()
.whereRef('users.id', '=', 'activity.userId')
.where('users.deletedAt', 'is', null),
).as('user');
};
@Injectable()
export class ActivityRepository implements IActivityRepository {
constructor(@InjectRepository(ActivityEntity) private repository: Repository<ActivityEntity>) {}
constructor(@InjectKysely() private db: Kysely<DB>) {}
@GenerateSql({ params: [{ albumId: DummyValue.UUID }] })
search(options: ActivitySearch): Promise<ActivityEntity[]> {
const { userId, assetId, albumId, isLiked } = options;
return this.repository.find({
where: {
userId,
assetId: assetId === null ? IsNull() : assetId,
albumId,
isLiked,
asset: {
deletedAt: IsNull(),
},
user: {
deletedAt: IsNull(),
},
},
relations: {
user: true,
},
order: {
createdAt: 'ASC',
},
});
return this.db
.selectFrom('activity')
.selectAll('activity')
.select(withUser)
.leftJoin('assets', (join) => join.onRef('assets.id', '=', 'activity.assetId').on('assets.deletedAt', 'is', null))
.$if(!!userId, (qb) => qb.where('activity.userId', '=', userId!))
.$if(assetId === null, (qb) => qb.where('assetId', 'is', null))
.$if(!!assetId, (qb) => qb.where('activity.assetId', '=', assetId!))
.$if(!!albumId, (qb) => qb.where('activity.albumId', '=', albumId!))
.$if(isLiked !== undefined, (qb) => qb.where('activity.isLiked', '=', isLiked!))
.orderBy('activity.createdAt', 'asc')
.execute() as unknown as Promise<ActivityEntity[]>;
}
create(entity: Partial<ActivityEntity>): Promise<ActivityEntity> {
return this.save(entity);
async create(activity: Insertable<Activity>) {
return this.save(activity);
}
async delete(id: string): Promise<void> {
await this.repository.delete(id);
await this.db.deleteFrom('activity').where('id', '=', asUuid(id)).execute();
}
@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID] })
getStatistics(assetId: string, albumId: string): Promise<number> {
return this.repository.count({
where: {
assetId,
albumId,
isLiked: false,
asset: {
deletedAt: IsNull(),
},
user: {
deletedAt: IsNull(),
},
},
relations: {
user: true,
},
withDeleted: true,
});
@GenerateSql({ params: [{ albumId: DummyValue.UUID, assetId: DummyValue.UUID }] })
async getStatistics({ albumId, assetId }: { albumId: string; assetId?: string }): Promise<number> {
const { count } = await this.db
.selectFrom('activity')
.select((eb) => eb.fn.countAll().as('count'))
.leftJoin('users', 'users.id', 'activity.userId')
.leftJoin('assets', 'assets.id', 'activity.assetId')
.$if(!!assetId, (qb) => qb.where('activity.assetId', '=', assetId!))
.where('activity.albumId', '=', albumId)
.where('activity.isLiked', '=', false)
.where('users.deletedAt', 'is', null)
.where('assets.deletedAt', 'is', null)
.executeTakeFirstOrThrow();
return count as number;
}
private async save(entity: Partial<ActivityEntity>) {
const { id } = await this.repository.save(entity);
return this.repository.findOneOrFail({
where: {
id,
},
relations: {
user: true,
},
});
private async save(entity: Insertable<Activity>) {
const { id } = await this.db.insertInto('activity').values(entity).returning('id').executeTakeFirstOrThrow();
return this.db
.selectFrom('activity')
.selectAll('activity')
.select(withUser)
.where('activity.id', '=', asUuid(id))
.executeTakeFirstOrThrow() as unknown as Promise<ActivityEntity>;
}
}