refactor: migrate partner repo to kysely (#15366)

This commit is contained in:
Jason Rasmussen
2025-01-17 18:49:21 -05:00
committed by GitHub
parent d5a9294eeb
commit 097183b31d
5 changed files with 276 additions and 30 deletions

View File

@@ -1,37 +1,93 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ExpressionBuilder, Insertable, JoinBuilder, Kysely, Updateable } from 'kysely';
import { jsonObjectFrom } from 'kysely/helpers/postgres';
import { InjectKysely } from 'nestjs-kysely';
import { DB, Partners, Users } from 'src/db';
import { DummyValue, GenerateSql } from 'src/decorators';
import { PartnerEntity } from 'src/entities/partner.entity';
import { IPartnerRepository, PartnerIds } from 'src/interfaces/partner.interface';
import { DeepPartial, Repository } from 'typeorm';
const columns = ['id', 'name', 'email', 'profileImagePath', 'profileChangedAt'] as const;
const onSharedBy = (join: JoinBuilder<DB & { sharedBy: Users }, 'partners' | 'sharedBy'>) =>
join.onRef('partners.sharedById', '=', 'sharedBy.id').on('sharedBy.deletedAt', 'is', null);
const onSharedWith = (join: JoinBuilder<DB & { sharedWith: Users }, 'partners' | 'sharedWith'>) =>
join.onRef('partners.sharedWithId', '=', 'sharedWith.id').on('sharedWith.deletedAt', 'is', null);
const withSharedBy = (eb: ExpressionBuilder<DB, 'partners'>) => {
return jsonObjectFrom(
eb.selectFrom('users as sharedBy').select(columns).whereRef('sharedBy.id', '=', 'partners.sharedById'),
).as('sharedBy');
};
const withSharedWith = (eb: ExpressionBuilder<DB, 'partners'>) => {
return jsonObjectFrom(
eb.selectFrom('users as sharedWith').select(columns).whereRef('sharedWith.id', '=', 'partners.sharedWithId'),
).as('sharedWith');
};
@Injectable()
export class PartnerRepository implements IPartnerRepository {
constructor(@InjectRepository(PartnerEntity) private repository: Repository<PartnerEntity>) {}
constructor(@InjectKysely() private db: Kysely<DB>) {}
@GenerateSql({ params: [DummyValue.UUID] })
getAll(userId: string): Promise<PartnerEntity[]> {
return this.repository.find({ where: [{ sharedWithId: userId }, { sharedById: userId }] });
return this.db
.selectFrom('partners')
.innerJoin('users as sharedBy', onSharedBy)
.innerJoin('users as sharedWith', onSharedWith)
.selectAll('partners')
.select(withSharedBy)
.select(withSharedWith)
.where((eb) => eb.or([eb('sharedWithId', '=', userId), eb('sharedById', '=', userId)]))
.execute() as Promise<PartnerEntity[]>;
}
get({ sharedWithId, sharedById }: PartnerIds): Promise<PartnerEntity | null> {
return this.repository.findOne({ where: { sharedById, sharedWithId } });
@GenerateSql({ params: [{ sharedWithId: DummyValue.UUID, sharedById: DummyValue.UUID }] })
get({ sharedWithId, sharedById }: PartnerIds): Promise<PartnerEntity | undefined> {
return this.db
.selectFrom('partners')
.innerJoin('users as sharedBy', onSharedBy)
.innerJoin('users as sharedWith', onSharedWith)
.selectAll('partners')
.select(withSharedBy)
.select(withSharedWith)
.where('sharedWithId', '=', sharedWithId)
.where('sharedById', '=', sharedById)
.executeTakeFirst() as unknown as Promise<PartnerEntity | undefined>;
}
create({ sharedById, sharedWithId }: PartnerIds): Promise<PartnerEntity> {
return this.save({ sharedBy: { id: sharedById }, sharedWith: { id: sharedWithId } });
@GenerateSql({ params: [{ sharedWithId: DummyValue.UUID, sharedById: DummyValue.UUID }] })
create(values: Insertable<Partners>): Promise<PartnerEntity> {
return this.db
.insertInto('partners')
.values(values)
.returningAll()
.returning(withSharedBy)
.returning(withSharedWith)
.executeTakeFirstOrThrow() as unknown as Promise<PartnerEntity>;
}
async remove(entity: PartnerEntity): Promise<void> {
await this.repository.remove(entity);
@GenerateSql({ params: [{ sharedWithId: DummyValue.UUID, sharedById: DummyValue.UUID }, { inTimeline: true }] })
update({ sharedWithId, sharedById }: PartnerIds, values: Updateable<Partners>): Promise<PartnerEntity> {
return this.db
.updateTable('partners')
.set(values)
.where('sharedWithId', '=', sharedWithId)
.where('sharedById', '=', sharedById)
.returningAll()
.returning(withSharedBy)
.returning(withSharedWith)
.executeTakeFirstOrThrow() as unknown as Promise<PartnerEntity>;
}
update(entity: Partial<PartnerEntity>): Promise<PartnerEntity> {
return this.save(entity);
}
private async save(entity: DeepPartial<PartnerEntity>): Promise<PartnerEntity> {
await this.repository.save(entity);
return this.repository.findOneOrFail({
where: { sharedById: entity.sharedById, sharedWithId: entity.sharedWithId },
});
@GenerateSql({ params: [{ sharedWithId: DummyValue.UUID, sharedById: DummyValue.UUID }] })
async remove({ sharedWithId, sharedById }: PartnerIds): Promise<void> {
await this.db
.deleteFrom('partners')
.where('sharedWithId', '=', sharedWithId)
.where('sharedById', '=', sharedById)
.execute();
}
}