Skip to content

High performance self-hosted photo and video management solution.

refactor: selected columns in queries (#17589)

Daniel Dietzler0b69d1c14708
diff --git a/server/src/database.ts b/server/src/database.ts
index 4435a17871..bb8189ddd4 100644
--- a/server/src/database.ts
+++ b/server/src/database.ts
@@ -256,6 +256,7 @@ export type AssetJobStatus = Selectable<DatabaseAssetJobStatus> & {
const userColumns = ['id', 'name', 'email', 'profileImagePath', 'profileChangedAt'] as const;
export const columns = {
+ assetFiles: ['asset_files.id', 'asset_files.path', 'asset_files.type'],
authUser: [
'users.id',
'users.name',
diff --git a/server/src/entities/asset.entity.ts b/server/src/entities/asset.entity.ts
index 7a2e722eaa..c285961e64 100644
--- a/server/src/entities/asset.entity.ts
+++ b/server/src/entities/asset.entity.ts
@@ -1,6 +1,6 @@
import { DeduplicateJoinsPlugin, ExpressionBuilder, Kysely, SelectQueryBuilder, sql } from 'kysely';
import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
-import { AssetFace, AssetFile, AssetJobStatus, Exif, Stack, Tag, User } from 'src/database';
+import { AssetFace, AssetFile, AssetJobStatus, columns, Exif, Stack, Tag, User } from 'src/database';
import { DB } from 'src/db';
import { AlbumEntity } from 'src/entities/album.entity';
import { SharedLinkEntity } from 'src/entities/shared-link.entity';
@@ -81,7 +81,7 @@ export function withFaces(eb: ExpressionBuilder<DB, 'assets'>, withDeletedFace?:
return jsonArrayFrom(
eb
.selectFrom('asset_faces')
- .selectAll()
+ .selectAll('asset_faces')
.whereRef('asset_faces.assetId', '=', 'assets.id')
.$if(!withDeletedFace, (qb) => qb.where('asset_faces.deletedAt', 'is', null)),
).as('faces');
@@ -91,7 +91,7 @@ export function withFiles(eb: ExpressionBuilder<DB, 'assets'>, type?: AssetFileT
return jsonArrayFrom(
eb
.selectFrom('asset_files')
- .selectAll('asset_files')
+ .select(columns.assetFiles)
.whereRef('asset_files.assetId', '=', 'assets.id')
.$if(!!type, (qb) => qb.where('asset_files.type', '=', type!)),
).as('files');
@@ -170,7 +170,7 @@ export function withAlbums<O>(qb: SelectQueryBuilder<DB, 'assets', O>, { albumId
jsonArrayFrom(
eb
.selectFrom('albums')
- .selectAll()
+ .selectAll('albums')
.innerJoin('albums_assets_assets', (join) =>
join
.onRef('albums.id', '=', 'albums_assets_assets.albumsId')
@@ -196,7 +196,7 @@ export function withTags(eb: ExpressionBuilder<DB, 'assets'>) {
return jsonArrayFrom(
eb
.selectFrom('tags')
- .selectAll('tags')
+ .select(columns.tag)
.innerJoin('tag_asset', 'tags.id', 'tag_asset.tagsId')
.whereRef('assets.id', '=', 'tag_asset.assetsId'),
).as('tags');
diff --git a/server/src/queries/album.repository.sql b/server/src/queries/album.repository.sql
index 0c3f1dec81..b89cbfb0b9 100644
--- a/server/src/queries/album.repository.sql
+++ b/server/src/queries/album.repository.sql
@@ -10,19 +10,9 @@ select
(
select
"id",
+ "name",
"email",
- "createdAt",
"profileImagePath",
- "isAdmin",
- "shouldChangePassword",
- "deletedAt",
- "oauthId",
- "updatedAt",
- "storageLabel",
- "name",
- "quotaSizeInBytes",
- "quotaUsageInBytes",
- "status",
"profileChangedAt"
from
"users"
@@ -36,7 +26,7 @@ select
from
(
select
- "album_users".*,
+ "album_users"."role",
(
select
to_json(obj)
@@ -44,19 +34,9 @@ select
(
select
"id",
+ "name",
"email",
- "createdAt",
"profileImagePath",
- "isAdmin",
- "shouldChangePassword",
- "deletedAt",
- "oauthId",
- "updatedAt",
- "storageLabel",
- "name",
- "quotaSizeInBytes",
- "quotaUsageInBytes",
- "status",
"profileChangedAt"
from
"users"
@@ -118,19 +98,9 @@ select
(
select
"id",
+ "name",
"email",
- "createdAt",
"profileImagePath",
- "isAdmin",
- "shouldChangePassword",
- "deletedAt",
- "oauthId",
- "updatedAt",
- "storageLabel",
- "name",
- "quotaSizeInBytes",
- "quotaUsageInBytes",
- "status",
"profileChangedAt"
from
"users"
@@ -144,7 +114,7 @@ select
from
(
select
- "album_users".*,
+ "album_users"."role",
(
select
to_json(obj)
@@ -152,19 +122,9 @@ select
(
select
"id",
+ "name",
"email",
- "createdAt",
"profileImagePath",
- "isAdmin",
- "shouldChangePassword",
- "deletedAt",
- "oauthId",
- "updatedAt",
- "storageLabel",
- "name",
- "quotaSizeInBytes",
- "quotaUsageInBytes",
- "status",
"profileChangedAt"
from
"users"
@@ -229,19 +189,9 @@ select
(
select
"id",
+ "name",
"email",
- "createdAt",
"profileImagePath",
- "isAdmin",
- "shouldChangePassword",
- "deletedAt",
- "oauthId",
- "updatedAt",
- "storageLabel",
- "name",
- "quotaSizeInBytes",
- "quotaUsageInBytes",
- "status",
"profileChangedAt"
from
"users"
@@ -255,7 +205,7 @@ select
from
(
select
- "album_users".*,
+ "album_users"."role",
(
select
to_json(obj)
@@ -263,19 +213,9 @@ select
(
select
"id",
+ "name",
"email",
- "createdAt",
"profileImagePath",
- "isAdmin",
- "shouldChangePassword",
- "deletedAt",
- "oauthId",
- "updatedAt",
- "storageLabel",
- "name",
- "quotaSizeInBytes",
- "quotaUsageInBytes",
- "status",
"profileChangedAt"
from
"users"
@@ -319,7 +259,7 @@ select
from
(
select
- "album_users".*,
+ "album_users"."role",
(
select
to_json(obj)
@@ -327,19 +267,9 @@ select
(
select
"id",
+ "name",
"email",
- "createdAt",
"profileImagePath",
- "isAdmin",
- "shouldChangePassword",
- "deletedAt",
- "oauthId",
- "updatedAt",
- "storageLabel",
- "name",
- "quotaSizeInBytes",
- "quotaUsageInBytes",
- "status",
"profileChangedAt"
from
"users"
@@ -360,19 +290,9 @@ select
(
select
"id",
+ "name",
"email",
- "createdAt",
"profileImagePath",
- "isAdmin",
- "shouldChangePassword",
- "deletedAt",
- "oauthId",
- "updatedAt",
- "storageLabel",
- "name",
- "quotaSizeInBytes",
- "quotaUsageInBytes",
- "status",
"profileChangedAt"
from
"users"
@@ -431,19 +351,9 @@ select
(
select
"id",
+ "name",
"email",
- "createdAt",
"profileImagePath",
- "isAdmin",
- "shouldChangePassword",
- "deletedAt",
- "oauthId",
- "updatedAt",
- "storageLabel",
- "name",
- "quotaSizeInBytes",
- "quotaUsageInBytes",
- "status",
"profileChangedAt"
from
"users"
diff --git a/server/src/queries/asset.repository.sql b/server/src/queries/asset.repository.sql
index d840a7693c..a72e6ee8cf 100644
--- a/server/src/queries/asset.repository.sql
+++ b/server/src/queries/asset.repository.sql
@@ -110,7 +110,12 @@ select
from
(
select
- "tags".*
+ "tags"."id",
+ "tags"."value",
+ "tags"."createdAt",
+ "tags"."updatedAt",
+ "tags"."color",
+ "tags"."parentId"
from
"tags"
inner join "tag_asset" on "tags"."id" = "tag_asset"."tagsId"
@@ -194,7 +199,9 @@ select
from
(
select
- "asset_files".*
+ "asset_files"."id",
+ "asset_files"."path",
+ "asset_files"."type"
from
"asset_files"
where
diff --git a/server/src/queries/shared.link.repository.sql b/server/src/queries/shared.link.repository.sql
index 641996e2f4..1044c1c883 100644
--- a/server/src/queries/shared.link.repository.sql
+++ b/server/src/queries/shared.link.repository.sql
@@ -100,7 +100,7 @@ order by
-- SharedLinkRepository.getAll
select distinct
on ("shared_links"."createdAt") "shared_links".*,
- to_json("assets") as "assets",
+ "assets"."assets",
to_json("album") as "album"
from
"shared_links"
diff --git a/server/src/queries/tag.repository.sql b/server/src/queries/tag.repository.sql
index c3eb1424fd..d728d3af88 100644
--- a/server/src/queries/tag.repository.sql
+++ b/server/src/queries/tag.repository.sql
@@ -37,7 +37,12 @@ on conflict ("userId", "value") do update
set
"parentId" = $4
returning
- *
+ "tags"."id",
+ "tags"."value",
+ "tags"."createdAt",
+ "tags"."updatedAt",
+ "tags"."color",
+ "tags"."parentId"
rollback
-- TagRepository.getAll
diff --git a/server/src/repositories/album.repository.ts b/server/src/repositories/album.repository.ts
index 3211527531..e21d5d73cd 100644
--- a/server/src/repositories/album.repository.ts
+++ b/server/src/repositories/album.repository.ts
@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { ExpressionBuilder, Insertable, Kysely, sql, Updateable } from 'kysely';
import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
import { InjectKysely } from 'nestjs-kysely';
+import { columns } from 'src/database';
import { Albums, DB } from 'src/db';
import { Chunked, ChunkedArray, ChunkedSet, DummyValue, GenerateSql } from 'src/decorators';
import { AlbumUserCreateDto } from 'src/dtos/album.dto';
@@ -19,26 +20,8 @@ export interface AlbumInfoOptions {
withAssets: boolean;
}
-const userColumns = [
- 'id',
- 'email',
- 'createdAt',
- 'profileImagePath',
- 'isAdmin',
- 'shouldChangePassword',
- 'deletedAt',
- 'oauthId',
- 'updatedAt',
- 'storageLabel',
- 'name',
- 'quotaSizeInBytes',
- 'quotaUsageInBytes',
- 'status',
- 'profileChangedAt',
-] as const;
-
const withOwner = (eb: ExpressionBuilder<DB, 'albums'>) => {
- return jsonObjectFrom(eb.selectFrom('users').select(userColumns).whereRef('users.id', '=', 'albums.ownerId')).as(
+ return jsonObjectFrom(eb.selectFrom('users').select(columns.user).whereRef('users.id', '=', 'albums.ownerId')).as(
'owner',
);
};
@@ -47,9 +30,9 @@ const withAlbumUsers = (eb: ExpressionBuilder<DB, 'albums'>) => {
return jsonArrayFrom(
eb
.selectFrom('albums_shared_users_users as album_users')
- .selectAll('album_users')
+ .select('album_users.role')
.select((eb) =>
- jsonObjectFrom(eb.selectFrom('users').select(userColumns).whereRef('users.id', '=', 'album_users.usersId')).as(
+ jsonObjectFrom(eb.selectFrom('users').select(columns.user).whereRef('users.id', '=', 'album_users.usersId')).as(
'user',
),
)
diff --git a/server/src/repositories/shared-link.repository.ts b/server/src/repositories/shared-link.repository.ts
index 52b5b7a2fe..272d7f3794 100644
--- a/server/src/repositories/shared-link.repository.ts
+++ b/server/src/repositories/shared-link.repository.ts
@@ -114,7 +114,7 @@ export class SharedLinkRepository {
.as('assets'),
(join) => join.onTrue(),
)
- .select((eb) => eb.fn.toJson('assets').as('assets'))
+ .select('assets.assets')
.leftJoinLateral(
(eb) =>
eb
diff --git a/server/src/repositories/tag.repository.ts b/server/src/repositories/tag.repository.ts
index 1236d80efa..9a3b33188f 100644
--- a/server/src/repositories/tag.repository.ts
+++ b/server/src/repositories/tag.repository.ts
@@ -38,7 +38,7 @@ export class TagRepository {
.insertInto('tags')
.values({ userId, value, parentId })
.onConflict((oc) => oc.columns(['userId', 'value']).doUpdateSet({ parentId }))
- .returningAll()
+ .returning(columns.tag)
.executeTakeFirstOrThrow();
// update closure table