refactor: migrate user repository to kysely ()

* refactor: migrate user repository to kysely

* refactor: migrate user repository to kysely

* refactor: migrate user repository to kysely

* refactor: migrate user repository to kysely

* fix: test

* clean up

* fix: metadata retrieval bug

* use correct typeing for upsert metadata

* pr feedback

* pr feedback

* fix: add deletedAt check

* fix: get non deleted user by default

* remove console.log

* fix: stop kysely after command finishes

* final clean up

---------

Co-authored-by: Jason Rasmussen <jason@rasm.me>
This commit is contained in:
Alex 2025-01-13 19:30:34 -06:00 committed by GitHub
parent a6c8eb57f1
commit 3da750117f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
18 changed files with 447 additions and 312 deletions
server/src/queries

View file

@ -1,195 +1,222 @@
-- NOTE: This file is auto generated by ./sql-generator
-- UserRepository.get
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"profileChangedAt",
(
select
coalesce(json_agg(agg), '[]')
from
(
select
"user_metadata".*
from
"user_metadata"
where
"users"."id" = "user_metadata"."userId"
) as agg
) as "metadata"
from
"users"
where
"users"."id" = $1
and "users"."deletedAt" is null
-- UserRepository.getAdmin
SELECT
"UserEntity"."id" AS "UserEntity_id",
"UserEntity"."name" AS "UserEntity_name",
"UserEntity"."isAdmin" AS "UserEntity_isAdmin",
"UserEntity"."email" AS "UserEntity_email",
"UserEntity"."storageLabel" AS "UserEntity_storageLabel",
"UserEntity"."oauthId" AS "UserEntity_oauthId",
"UserEntity"."profileImagePath" AS "UserEntity_profileImagePath",
"UserEntity"."shouldChangePassword" AS "UserEntity_shouldChangePassword",
"UserEntity"."createdAt" AS "UserEntity_createdAt",
"UserEntity"."deletedAt" AS "UserEntity_deletedAt",
"UserEntity"."status" AS "UserEntity_status",
"UserEntity"."updatedAt" AS "UserEntity_updatedAt",
"UserEntity"."quotaSizeInBytes" AS "UserEntity_quotaSizeInBytes",
"UserEntity"."quotaUsageInBytes" AS "UserEntity_quotaUsageInBytes",
"UserEntity"."profileChangedAt" AS "UserEntity_profileChangedAt"
FROM
"users" "UserEntity"
WHERE
((("UserEntity"."isAdmin" = $1)))
AND ("UserEntity"."deletedAt" IS NULL)
LIMIT
1
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"profileChangedAt"
from
"users"
where
"users"."isAdmin" = $1
and "users"."deletedAt" is null
-- UserRepository.hasAdmin
SELECT
1 AS "row_exists"
FROM
(
SELECT
1 AS dummy_column
) "dummy_table"
WHERE
EXISTS (
SELECT
1
FROM
"users" "UserEntity"
WHERE
((("UserEntity"."isAdmin" = $1)))
AND ("UserEntity"."deletedAt" IS NULL)
)
LIMIT
1
select
"users"."id"
from
"users"
where
"users"."isAdmin" = $1
and "users"."deletedAt" is null
-- UserRepository.getByEmail
SELECT
"user"."id" AS "user_id",
"user"."name" AS "user_name",
"user"."isAdmin" AS "user_isAdmin",
"user"."email" AS "user_email",
"user"."storageLabel" AS "user_storageLabel",
"user"."oauthId" AS "user_oauthId",
"user"."profileImagePath" AS "user_profileImagePath",
"user"."shouldChangePassword" AS "user_shouldChangePassword",
"user"."createdAt" AS "user_createdAt",
"user"."deletedAt" AS "user_deletedAt",
"user"."status" AS "user_status",
"user"."updatedAt" AS "user_updatedAt",
"user"."quotaSizeInBytes" AS "user_quotaSizeInBytes",
"user"."quotaUsageInBytes" AS "user_quotaUsageInBytes",
"user"."profileChangedAt" AS "user_profileChangedAt"
FROM
"users" "user"
WHERE
("user"."email" = $1)
AND ("user"."deletedAt" IS NULL)
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"profileChangedAt"
from
"users"
where
"email" = $1
and "users"."deletedAt" is null
-- UserRepository.getByStorageLabel
SELECT
"UserEntity"."id" AS "UserEntity_id",
"UserEntity"."name" AS "UserEntity_name",
"UserEntity"."isAdmin" AS "UserEntity_isAdmin",
"UserEntity"."email" AS "UserEntity_email",
"UserEntity"."storageLabel" AS "UserEntity_storageLabel",
"UserEntity"."oauthId" AS "UserEntity_oauthId",
"UserEntity"."profileImagePath" AS "UserEntity_profileImagePath",
"UserEntity"."shouldChangePassword" AS "UserEntity_shouldChangePassword",
"UserEntity"."createdAt" AS "UserEntity_createdAt",
"UserEntity"."deletedAt" AS "UserEntity_deletedAt",
"UserEntity"."status" AS "UserEntity_status",
"UserEntity"."updatedAt" AS "UserEntity_updatedAt",
"UserEntity"."quotaSizeInBytes" AS "UserEntity_quotaSizeInBytes",
"UserEntity"."quotaUsageInBytes" AS "UserEntity_quotaUsageInBytes",
"UserEntity"."profileChangedAt" AS "UserEntity_profileChangedAt"
FROM
"users" "UserEntity"
WHERE
((("UserEntity"."storageLabel" = $1)))
AND ("UserEntity"."deletedAt" IS NULL)
LIMIT
1
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"profileChangedAt"
from
"users"
where
"users"."storageLabel" = $1
and "users"."deletedAt" is null
-- UserRepository.getByOAuthId
SELECT
"UserEntity"."id" AS "UserEntity_id",
"UserEntity"."name" AS "UserEntity_name",
"UserEntity"."isAdmin" AS "UserEntity_isAdmin",
"UserEntity"."email" AS "UserEntity_email",
"UserEntity"."storageLabel" AS "UserEntity_storageLabel",
"UserEntity"."oauthId" AS "UserEntity_oauthId",
"UserEntity"."profileImagePath" AS "UserEntity_profileImagePath",
"UserEntity"."shouldChangePassword" AS "UserEntity_shouldChangePassword",
"UserEntity"."createdAt" AS "UserEntity_createdAt",
"UserEntity"."deletedAt" AS "UserEntity_deletedAt",
"UserEntity"."status" AS "UserEntity_status",
"UserEntity"."updatedAt" AS "UserEntity_updatedAt",
"UserEntity"."quotaSizeInBytes" AS "UserEntity_quotaSizeInBytes",
"UserEntity"."quotaUsageInBytes" AS "UserEntity_quotaUsageInBytes",
"UserEntity"."profileChangedAt" AS "UserEntity_profileChangedAt"
FROM
"users" "UserEntity"
WHERE
((("UserEntity"."oauthId" = $1)))
AND ("UserEntity"."deletedAt" IS NULL)
LIMIT
1
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"profileChangedAt"
from
"users"
where
"users"."oauthId" = $1
and "users"."deletedAt" is null
-- UserRepository.getUserStats
SELECT
"users"."id" AS "userId",
"users"."name" AS "userName",
"users"."quotaSizeInBytes" AS "quotaSizeInBytes",
COUNT("assets"."id") FILTER (
WHERE
"assets"."type" = 'IMAGE'
AND "assets"."isVisible"
) AS "photos",
COUNT("assets"."id") FILTER (
WHERE
"assets"."type" = 'VIDEO'
AND "assets"."isVisible"
) AS "videos",
COALESCE(
SUM("exif"."fileSizeInByte") FILTER (
WHERE
"assets"."libraryId" IS NULL
select
"users"."id" as "userId",
"users"."name" as "userName",
"users"."quotaSizeInBytes" as "quotaSizeInBytes",
count(*) filter (
where
(
"assets"."type" = $1
and "assets"."isVisible" = $2
)
) as "photos",
count(*) filter (
where
(
"assets"."type" = $3
and "assets"."isVisible" = $4
)
) as "videos",
coalesce(
sum("exif"."fileSizeInByte") filter (
where
"assets"."libraryId" is null
),
0
) AS "usage",
COALESCE(
SUM("exif"."fileSizeInByte") FILTER (
WHERE
"assets"."libraryId" IS NULL
AND "assets"."type" = 'IMAGE'
) as "usage",
coalesce(
sum("exif"."fileSizeInByte") filter (
where
(
"assets"."libraryId" is null
and "assets"."type" = $5
)
),
0
) AS "usagePhotos",
COALESCE(
SUM("exif"."fileSizeInByte") FILTER (
WHERE
"assets"."libraryId" IS NULL
AND "assets"."type" = 'VIDEO'
) as "usagePhotos",
coalesce(
sum("exif"."fileSizeInByte") filter (
where
(
"assets"."libraryId" is null
and "assets"."type" = $6
)
),
0
) AS "usageVideos"
FROM
"users" "users"
LEFT JOIN "assets" "assets" ON "assets"."ownerId" = "users"."id"
AND ("assets"."deletedAt" IS NULL)
LEFT JOIN "exif" "exif" ON "exif"."assetId" = "assets"."id"
WHERE
"users"."deletedAt" IS NULL
GROUP BY
) as "usageVideos"
from
"users"
left join "assets" on "assets"."ownerId" = "users"."id"
left join "exif" on "exif"."assetId" = "assets"."id"
where
"assets"."deletedAt" is null
group by
"users"."id"
ORDER BY
"users"."createdAt" ASC
order by
"users"."createdAt" asc
-- UserRepository.updateUsage
UPDATE "users"
SET
"quotaUsageInBytes" = "quotaUsageInBytes" + 50,
"updatedAt" = CURRENT_TIMESTAMP
WHERE
"id" = $1
update "users"
set
"quotaUsageInBytes" = "quotaUsageInBytes" + $1,
"updatedAt" = $2
where
"id" = $3::uuid
and "users"."deletedAt" is null
-- UserRepository.syncUsage
UPDATE "users"
SET
update "users"
set
"quotaUsageInBytes" = (
SELECT
COALESCE(SUM(exif."fileSizeInByte"), 0)
FROM
"assets" "assets"
LEFT JOIN "exif" "exif" ON "exif"."assetId" = "assets"."id"
WHERE
"assets"."ownerId" = users.id
AND "assets"."libraryId" IS NULL
select
coalesce(sum("exif"."fileSizeInByte"), 0) as "usage"
from
"assets"
left join "exif" on "exif"."assetId" = "assets"."id"
where
"assets"."libraryId" is null
and "assets"."ownerId" = "users"."id"
),
"updatedAt" = CURRENT_TIMESTAMP
WHERE
users.id = $1
"updatedAt" = $1
where
"users"."deletedAt" is null
and "users"."id" = $2::uuid