-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: migrate partner repo to kysely #15366
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
-- NOTE: This file is auto generated by ./sql-generator | ||
|
||
-- PartnerRepository.getAll | ||
select | ||
"partners".*, | ||
( | ||
select | ||
to_json(obj) | ||
from | ||
( | ||
select | ||
"id", | ||
"name", | ||
"email", | ||
"profileImagePath", | ||
"profileChangedAt" | ||
from | ||
"users" as "sharedBy" | ||
where | ||
"sharedBy"."id" = "partners"."sharedById" | ||
) as obj | ||
) as "sharedBy", | ||
( | ||
select | ||
to_json(obj) | ||
from | ||
( | ||
select | ||
"id", | ||
"name", | ||
"email", | ||
"profileImagePath", | ||
"profileChangedAt" | ||
from | ||
"users" as "sharedWith" | ||
where | ||
"sharedWith"."id" = "partners"."sharedWithId" | ||
) as obj | ||
) as "sharedWith" | ||
from | ||
"partners" | ||
inner join "users" as "sharedBy" on "partners"."sharedById" = "sharedBy"."id" | ||
and "sharedBy"."deletedAt" is null | ||
inner join "users" as "sharedWith" on "partners"."sharedWithId" = "sharedWith"."id" | ||
and "sharedWith"."deletedAt" is null | ||
where | ||
( | ||
"sharedWithId" = $1 | ||
or "sharedById" = $2 | ||
) | ||
|
||
-- PartnerRepository.get | ||
select | ||
"partners".*, | ||
( | ||
select | ||
to_json(obj) | ||
from | ||
( | ||
select | ||
"id", | ||
"name", | ||
"email", | ||
"profileImagePath", | ||
"profileChangedAt" | ||
from | ||
"users" as "sharedBy" | ||
where | ||
"sharedBy"."id" = "partners"."sharedById" | ||
) as obj | ||
) as "sharedBy", | ||
( | ||
select | ||
to_json(obj) | ||
from | ||
( | ||
select | ||
"id", | ||
"name", | ||
"email", | ||
"profileImagePath", | ||
"profileChangedAt" | ||
from | ||
"users" as "sharedWith" | ||
where | ||
"sharedWith"."id" = "partners"."sharedWithId" | ||
) as obj | ||
) as "sharedWith" | ||
from | ||
"partners" | ||
inner join "users" as "sharedBy" on "partners"."sharedById" = "sharedBy"."id" | ||
and "sharedBy"."deletedAt" is null | ||
inner join "users" as "sharedWith" on "partners"."sharedWithId" = "sharedWith"."id" | ||
and "sharedWith"."deletedAt" is null | ||
where | ||
"sharedWithId" = $1 | ||
and "sharedById" = $2 | ||
|
||
-- PartnerRepository.create | ||
insert into | ||
"partners" ("sharedWithId", "sharedById") | ||
values | ||
($1, $2) | ||
returning | ||
*, | ||
( | ||
select | ||
to_json(obj) | ||
from | ||
( | ||
select | ||
"id", | ||
"name", | ||
"email", | ||
"profileImagePath", | ||
"profileChangedAt" | ||
from | ||
"users" as "sharedBy" | ||
where | ||
"sharedBy"."id" = "partners"."sharedById" | ||
) as obj | ||
) as "sharedBy", | ||
( | ||
select | ||
to_json(obj) | ||
from | ||
( | ||
select | ||
"id", | ||
"name", | ||
"email", | ||
"profileImagePath", | ||
"profileChangedAt" | ||
from | ||
"users" as "sharedWith" | ||
where | ||
"sharedWith"."id" = "partners"."sharedWithId" | ||
) as obj | ||
) as "sharedWith" | ||
|
||
-- PartnerRepository.update | ||
update "partners" | ||
set | ||
"inTimeline" = $1 | ||
where | ||
"sharedWithId" = $2 | ||
and "sharedById" = $3 | ||
returning | ||
*, | ||
( | ||
select | ||
to_json(obj) | ||
from | ||
( | ||
select | ||
"id", | ||
"name", | ||
"email", | ||
"profileImagePath", | ||
"profileChangedAt" | ||
from | ||
"users" as "sharedBy" | ||
where | ||
"sharedBy"."id" = "partners"."sharedById" | ||
) as obj | ||
) as "sharedBy", | ||
( | ||
select | ||
to_json(obj) | ||
from | ||
( | ||
select | ||
"id", | ||
"name", | ||
"email", | ||
"profileImagePath", | ||
"profileChangedAt" | ||
from | ||
"users" as "sharedWith" | ||
where | ||
"sharedWith"."id" = "partners"."sharedWithId" | ||
) as obj | ||
) as "sharedWith" | ||
|
||
-- PartnerRepository.remove | ||
delete from "partners" | ||
where | ||
"sharedWithId" = $1 | ||
and "sharedById" = $2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }] }); | ||
} | ||
|
||
get({ sharedWithId, sharedById }: PartnerIds): Promise<PartnerEntity | null> { | ||
return this.repository.findOne({ where: { sharedById, sharedWithId } }); | ||
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[]>; | ||
} | ||
|
||
create({ sharedById, sharedWithId }: PartnerIds): Promise<PartnerEntity> { | ||
return this.save({ sharedBy: { id: sharedById }, sharedWith: { id: 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) | ||
jrasm91 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.where('sharedWithId', '=', sharedWithId) | ||
.where('sharedById', '=', sharedById) | ||
.executeTakeFirst() as unknown as Promise<PartnerEntity | undefined>; | ||
} | ||
|
||
async remove(entity: PartnerEntity): Promise<void> { | ||
await this.repository.remove(entity); | ||
@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>; | ||
} | ||
|
||
update(entity: Partial<PartnerEntity>): Promise<PartnerEntity> { | ||
return this.save(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>; | ||
} | ||
|
||
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
withSharedBy
/withSharedWith
effectively repeat the work that the inner joins do, so it's better to select from the already-joined tables.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.