refactor: selected columns in queries (#17589)

This commit is contained in:
Daniel Dietzler 2025-04-14 20:34:06 +02:00 committed by GitHub
parent 5a51ad3622
commit 0b69d1c147
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 50 additions and 144 deletions

View File

@ -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',

View File

@ -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');

View File

@ -10,19 +10,9 @@ select
(
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"email",
"profileImagePath",
"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",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"email",
"profileImagePath",
"profileChangedAt"
from
"users"
@ -118,19 +98,9 @@ select
(
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"email",
"profileImagePath",
"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",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"email",
"profileImagePath",
"profileChangedAt"
from
"users"
@ -229,19 +189,9 @@ select
(
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"email",
"profileImagePath",
"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",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"email",
"profileImagePath",
"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",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"email",
"profileImagePath",
"profileChangedAt"
from
"users"
@ -360,19 +290,9 @@ select
(
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"email",
"profileImagePath",
"profileChangedAt"
from
"users"
@ -431,19 +351,9 @@ select
(
select
"id",
"email",
"createdAt",
"profileImagePath",
"isAdmin",
"shouldChangePassword",
"deletedAt",
"oauthId",
"updatedAt",
"storageLabel",
"name",
"quotaSizeInBytes",
"quotaUsageInBytes",
"status",
"email",
"profileImagePath",
"profileChangedAt"
from
"users"

View File

@ -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

View File

@ -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"

View File

@ -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

View File

@ -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',
),
)

View File

@ -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

View File

@ -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