Skip to content

Commit

Permalink
refactor: migration tag repository to kysely
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasm91 committed Feb 27, 2025
1 parent 082471d commit 16d4137
Show file tree
Hide file tree
Showing 12 changed files with 344 additions and 246 deletions.
1 change: 1 addition & 0 deletions server/src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,6 @@ export const columns = {
'shared_links.password',
],
userDto: ['id', 'name', 'email', 'profileImagePath', 'profileChangedAt'],
tagDto: ['id', 'value', 'createdAt', 'updatedAt', 'color', 'parentId'],
apiKey: ['id', 'name', 'userId', 'createdAt', 'updatedAt', 'permissions'],
} as const;
3 changes: 2 additions & 1 deletion server/src/dtos/tag.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsHexColor, IsNotEmpty, IsString } from 'class-validator';
import { TagEntity } from 'src/entities/tag.entity';
import { TagItem } from 'src/types';
import { Optional, ValidateHexColor, ValidateUUID } from 'src/validation';

export class TagCreateDto {
Expand Down Expand Up @@ -51,7 +52,7 @@ export class TagResponseDto {
color?: string;
}

export function mapTag(entity: TagEntity): TagResponseDto {
export function mapTag(entity: TagItem | TagEntity): TagResponseDto {
return {
id: entity.id,
parentId: entity.parentId ?? undefined,
Expand Down
124 changes: 116 additions & 8 deletions server/src/queries/tag.repository.sql
Original file line number Diff line number Diff line change
@@ -1,10 +1,118 @@
-- NOTE: This file is auto generated by ./sql-generator

-- TagRepository.getAssetIds
SELECT
"tag_asset"."assetsId" AS "assetId"
FROM
"tag_asset" "tag_asset"
WHERE
"tag_asset"."tagsId" = $1
AND "tag_asset"."assetsId" IN ($2)
-- TagRepository.get
select
"id",
"value",
"createdAt",
"updatedAt",
"color",
"parentId"
from
"tags"
where
"id" = $1

-- TagRepository.getByValue
select
"id",
"value",
"createdAt",
"updatedAt",
"color",
"parentId"
from
"tags"
where
"userId" = $1
and "value" = $2

-- TagRepository.upsertValue
begin
insert into
"tags" ("userId", "value", "parentId")
values
($1, $2, $3)
on conflict ("userId", "value") do update
set
"parentId" = $4
returning
*
rollback

-- TagRepository.getAll
select
"id",
"value",
"createdAt",
"updatedAt",
"color",
"parentId"
from
"tags"
where
"userId" = $1
order by
"value" asc

-- TagRepository.create
insert into
"tags" ("userId", "color", "value")
values
($1, $2, $3)
returning
*

-- TagRepository.update
update "tags"
set
"color" = $1
where
"id" = $2
returning
*

-- TagRepository.delete
delete from "tags"
where
"id" = $1

-- TagRepository.addAssetIds
insert into
"tag_asset" ("tagsId", "assetsId")
values
($1, $2)

-- TagRepository.removeAssetIds
delete from "tag_asset"
where
"tagsId" = $1
and "assetsId" in ($2)

-- TagRepository.replaceAssetTags
begin
delete from "tag_asset"
where
"assetsId" = $1
insert into
"tag_asset" ("tagsId", "assetsId")
values
($1, $2)
on conflict do nothing
returning
*
rollback

-- TagRepository.deleteEmptyTags
begin
select
"tags"."id",
count("assets"."id") as "count"
from
"assets"
inner join "tag_asset" on "tag_asset"."assetsId" = "assets"."id"
inner join "tags_closure" on "tags_closure"."id_descendant" = "tag_asset"."tagsId"
inner join "tags" on "tags"."id" = "tags_closure"."id_descendant"
group by
"tags"."id"
commit
Loading

0 comments on commit 16d4137

Please sign in to comment.