mirror of
https://github.com/immich-app/immich.git
synced 2025-05-30 19:54:52 -04:00
refactor: selected columns in queries (#17589)
This commit is contained in:
parent
5a51ad3622
commit
0b69d1c147
@ -256,6 +256,7 @@ export type AssetJobStatus = Selectable<DatabaseAssetJobStatus> & {
|
|||||||
const userColumns = ['id', 'name', 'email', 'profileImagePath', 'profileChangedAt'] as const;
|
const userColumns = ['id', 'name', 'email', 'profileImagePath', 'profileChangedAt'] as const;
|
||||||
|
|
||||||
export const columns = {
|
export const columns = {
|
||||||
|
assetFiles: ['asset_files.id', 'asset_files.path', 'asset_files.type'],
|
||||||
authUser: [
|
authUser: [
|
||||||
'users.id',
|
'users.id',
|
||||||
'users.name',
|
'users.name',
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { DeduplicateJoinsPlugin, ExpressionBuilder, Kysely, SelectQueryBuilder, sql } from 'kysely';
|
import { DeduplicateJoinsPlugin, ExpressionBuilder, Kysely, SelectQueryBuilder, sql } from 'kysely';
|
||||||
import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
|
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 { DB } from 'src/db';
|
||||||
import { AlbumEntity } from 'src/entities/album.entity';
|
import { AlbumEntity } from 'src/entities/album.entity';
|
||||||
import { SharedLinkEntity } from 'src/entities/shared-link.entity';
|
import { SharedLinkEntity } from 'src/entities/shared-link.entity';
|
||||||
@ -81,7 +81,7 @@ export function withFaces(eb: ExpressionBuilder<DB, 'assets'>, withDeletedFace?:
|
|||||||
return jsonArrayFrom(
|
return jsonArrayFrom(
|
||||||
eb
|
eb
|
||||||
.selectFrom('asset_faces')
|
.selectFrom('asset_faces')
|
||||||
.selectAll()
|
.selectAll('asset_faces')
|
||||||
.whereRef('asset_faces.assetId', '=', 'assets.id')
|
.whereRef('asset_faces.assetId', '=', 'assets.id')
|
||||||
.$if(!withDeletedFace, (qb) => qb.where('asset_faces.deletedAt', 'is', null)),
|
.$if(!withDeletedFace, (qb) => qb.where('asset_faces.deletedAt', 'is', null)),
|
||||||
).as('faces');
|
).as('faces');
|
||||||
@ -91,7 +91,7 @@ export function withFiles(eb: ExpressionBuilder<DB, 'assets'>, type?: AssetFileT
|
|||||||
return jsonArrayFrom(
|
return jsonArrayFrom(
|
||||||
eb
|
eb
|
||||||
.selectFrom('asset_files')
|
.selectFrom('asset_files')
|
||||||
.selectAll('asset_files')
|
.select(columns.assetFiles)
|
||||||
.whereRef('asset_files.assetId', '=', 'assets.id')
|
.whereRef('asset_files.assetId', '=', 'assets.id')
|
||||||
.$if(!!type, (qb) => qb.where('asset_files.type', '=', type!)),
|
.$if(!!type, (qb) => qb.where('asset_files.type', '=', type!)),
|
||||||
).as('files');
|
).as('files');
|
||||||
@ -170,7 +170,7 @@ export function withAlbums<O>(qb: SelectQueryBuilder<DB, 'assets', O>, { albumId
|
|||||||
jsonArrayFrom(
|
jsonArrayFrom(
|
||||||
eb
|
eb
|
||||||
.selectFrom('albums')
|
.selectFrom('albums')
|
||||||
.selectAll()
|
.selectAll('albums')
|
||||||
.innerJoin('albums_assets_assets', (join) =>
|
.innerJoin('albums_assets_assets', (join) =>
|
||||||
join
|
join
|
||||||
.onRef('albums.id', '=', 'albums_assets_assets.albumsId')
|
.onRef('albums.id', '=', 'albums_assets_assets.albumsId')
|
||||||
@ -196,7 +196,7 @@ export function withTags(eb: ExpressionBuilder<DB, 'assets'>) {
|
|||||||
return jsonArrayFrom(
|
return jsonArrayFrom(
|
||||||
eb
|
eb
|
||||||
.selectFrom('tags')
|
.selectFrom('tags')
|
||||||
.selectAll('tags')
|
.select(columns.tag)
|
||||||
.innerJoin('tag_asset', 'tags.id', 'tag_asset.tagsId')
|
.innerJoin('tag_asset', 'tags.id', 'tag_asset.tagsId')
|
||||||
.whereRef('assets.id', '=', 'tag_asset.assetsId'),
|
.whereRef('assets.id', '=', 'tag_asset.assetsId'),
|
||||||
).as('tags');
|
).as('tags');
|
||||||
|
@ -10,19 +10,9 @@ select
|
|||||||
(
|
(
|
||||||
select
|
select
|
||||||
"id",
|
"id",
|
||||||
"email",
|
|
||||||
"createdAt",
|
|
||||||
"profileImagePath",
|
|
||||||
"isAdmin",
|
|
||||||
"shouldChangePassword",
|
|
||||||
"deletedAt",
|
|
||||||
"oauthId",
|
|
||||||
"updatedAt",
|
|
||||||
"storageLabel",
|
|
||||||
"name",
|
"name",
|
||||||
"quotaSizeInBytes",
|
"email",
|
||||||
"quotaUsageInBytes",
|
"profileImagePath",
|
||||||
"status",
|
|
||||||
"profileChangedAt"
|
"profileChangedAt"
|
||||||
from
|
from
|
||||||
"users"
|
"users"
|
||||||
@ -36,7 +26,7 @@ select
|
|||||||
from
|
from
|
||||||
(
|
(
|
||||||
select
|
select
|
||||||
"album_users".*,
|
"album_users"."role",
|
||||||
(
|
(
|
||||||
select
|
select
|
||||||
to_json(obj)
|
to_json(obj)
|
||||||
@ -44,19 +34,9 @@ select
|
|||||||
(
|
(
|
||||||
select
|
select
|
||||||
"id",
|
"id",
|
||||||
"email",
|
|
||||||
"createdAt",
|
|
||||||
"profileImagePath",
|
|
||||||
"isAdmin",
|
|
||||||
"shouldChangePassword",
|
|
||||||
"deletedAt",
|
|
||||||
"oauthId",
|
|
||||||
"updatedAt",
|
|
||||||
"storageLabel",
|
|
||||||
"name",
|
"name",
|
||||||
"quotaSizeInBytes",
|
"email",
|
||||||
"quotaUsageInBytes",
|
"profileImagePath",
|
||||||
"status",
|
|
||||||
"profileChangedAt"
|
"profileChangedAt"
|
||||||
from
|
from
|
||||||
"users"
|
"users"
|
||||||
@ -118,19 +98,9 @@ select
|
|||||||
(
|
(
|
||||||
select
|
select
|
||||||
"id",
|
"id",
|
||||||
"email",
|
|
||||||
"createdAt",
|
|
||||||
"profileImagePath",
|
|
||||||
"isAdmin",
|
|
||||||
"shouldChangePassword",
|
|
||||||
"deletedAt",
|
|
||||||
"oauthId",
|
|
||||||
"updatedAt",
|
|
||||||
"storageLabel",
|
|
||||||
"name",
|
"name",
|
||||||
"quotaSizeInBytes",
|
"email",
|
||||||
"quotaUsageInBytes",
|
"profileImagePath",
|
||||||
"status",
|
|
||||||
"profileChangedAt"
|
"profileChangedAt"
|
||||||
from
|
from
|
||||||
"users"
|
"users"
|
||||||
@ -144,7 +114,7 @@ select
|
|||||||
from
|
from
|
||||||
(
|
(
|
||||||
select
|
select
|
||||||
"album_users".*,
|
"album_users"."role",
|
||||||
(
|
(
|
||||||
select
|
select
|
||||||
to_json(obj)
|
to_json(obj)
|
||||||
@ -152,19 +122,9 @@ select
|
|||||||
(
|
(
|
||||||
select
|
select
|
||||||
"id",
|
"id",
|
||||||
"email",
|
|
||||||
"createdAt",
|
|
||||||
"profileImagePath",
|
|
||||||
"isAdmin",
|
|
||||||
"shouldChangePassword",
|
|
||||||
"deletedAt",
|
|
||||||
"oauthId",
|
|
||||||
"updatedAt",
|
|
||||||
"storageLabel",
|
|
||||||
"name",
|
"name",
|
||||||
"quotaSizeInBytes",
|
"email",
|
||||||
"quotaUsageInBytes",
|
"profileImagePath",
|
||||||
"status",
|
|
||||||
"profileChangedAt"
|
"profileChangedAt"
|
||||||
from
|
from
|
||||||
"users"
|
"users"
|
||||||
@ -229,19 +189,9 @@ select
|
|||||||
(
|
(
|
||||||
select
|
select
|
||||||
"id",
|
"id",
|
||||||
"email",
|
|
||||||
"createdAt",
|
|
||||||
"profileImagePath",
|
|
||||||
"isAdmin",
|
|
||||||
"shouldChangePassword",
|
|
||||||
"deletedAt",
|
|
||||||
"oauthId",
|
|
||||||
"updatedAt",
|
|
||||||
"storageLabel",
|
|
||||||
"name",
|
"name",
|
||||||
"quotaSizeInBytes",
|
"email",
|
||||||
"quotaUsageInBytes",
|
"profileImagePath",
|
||||||
"status",
|
|
||||||
"profileChangedAt"
|
"profileChangedAt"
|
||||||
from
|
from
|
||||||
"users"
|
"users"
|
||||||
@ -255,7 +205,7 @@ select
|
|||||||
from
|
from
|
||||||
(
|
(
|
||||||
select
|
select
|
||||||
"album_users".*,
|
"album_users"."role",
|
||||||
(
|
(
|
||||||
select
|
select
|
||||||
to_json(obj)
|
to_json(obj)
|
||||||
@ -263,19 +213,9 @@ select
|
|||||||
(
|
(
|
||||||
select
|
select
|
||||||
"id",
|
"id",
|
||||||
"email",
|
|
||||||
"createdAt",
|
|
||||||
"profileImagePath",
|
|
||||||
"isAdmin",
|
|
||||||
"shouldChangePassword",
|
|
||||||
"deletedAt",
|
|
||||||
"oauthId",
|
|
||||||
"updatedAt",
|
|
||||||
"storageLabel",
|
|
||||||
"name",
|
"name",
|
||||||
"quotaSizeInBytes",
|
"email",
|
||||||
"quotaUsageInBytes",
|
"profileImagePath",
|
||||||
"status",
|
|
||||||
"profileChangedAt"
|
"profileChangedAt"
|
||||||
from
|
from
|
||||||
"users"
|
"users"
|
||||||
@ -319,7 +259,7 @@ select
|
|||||||
from
|
from
|
||||||
(
|
(
|
||||||
select
|
select
|
||||||
"album_users".*,
|
"album_users"."role",
|
||||||
(
|
(
|
||||||
select
|
select
|
||||||
to_json(obj)
|
to_json(obj)
|
||||||
@ -327,19 +267,9 @@ select
|
|||||||
(
|
(
|
||||||
select
|
select
|
||||||
"id",
|
"id",
|
||||||
"email",
|
|
||||||
"createdAt",
|
|
||||||
"profileImagePath",
|
|
||||||
"isAdmin",
|
|
||||||
"shouldChangePassword",
|
|
||||||
"deletedAt",
|
|
||||||
"oauthId",
|
|
||||||
"updatedAt",
|
|
||||||
"storageLabel",
|
|
||||||
"name",
|
"name",
|
||||||
"quotaSizeInBytes",
|
"email",
|
||||||
"quotaUsageInBytes",
|
"profileImagePath",
|
||||||
"status",
|
|
||||||
"profileChangedAt"
|
"profileChangedAt"
|
||||||
from
|
from
|
||||||
"users"
|
"users"
|
||||||
@ -360,19 +290,9 @@ select
|
|||||||
(
|
(
|
||||||
select
|
select
|
||||||
"id",
|
"id",
|
||||||
"email",
|
|
||||||
"createdAt",
|
|
||||||
"profileImagePath",
|
|
||||||
"isAdmin",
|
|
||||||
"shouldChangePassword",
|
|
||||||
"deletedAt",
|
|
||||||
"oauthId",
|
|
||||||
"updatedAt",
|
|
||||||
"storageLabel",
|
|
||||||
"name",
|
"name",
|
||||||
"quotaSizeInBytes",
|
"email",
|
||||||
"quotaUsageInBytes",
|
"profileImagePath",
|
||||||
"status",
|
|
||||||
"profileChangedAt"
|
"profileChangedAt"
|
||||||
from
|
from
|
||||||
"users"
|
"users"
|
||||||
@ -431,19 +351,9 @@ select
|
|||||||
(
|
(
|
||||||
select
|
select
|
||||||
"id",
|
"id",
|
||||||
"email",
|
|
||||||
"createdAt",
|
|
||||||
"profileImagePath",
|
|
||||||
"isAdmin",
|
|
||||||
"shouldChangePassword",
|
|
||||||
"deletedAt",
|
|
||||||
"oauthId",
|
|
||||||
"updatedAt",
|
|
||||||
"storageLabel",
|
|
||||||
"name",
|
"name",
|
||||||
"quotaSizeInBytes",
|
"email",
|
||||||
"quotaUsageInBytes",
|
"profileImagePath",
|
||||||
"status",
|
|
||||||
"profileChangedAt"
|
"profileChangedAt"
|
||||||
from
|
from
|
||||||
"users"
|
"users"
|
||||||
|
@ -110,7 +110,12 @@ select
|
|||||||
from
|
from
|
||||||
(
|
(
|
||||||
select
|
select
|
||||||
"tags".*
|
"tags"."id",
|
||||||
|
"tags"."value",
|
||||||
|
"tags"."createdAt",
|
||||||
|
"tags"."updatedAt",
|
||||||
|
"tags"."color",
|
||||||
|
"tags"."parentId"
|
||||||
from
|
from
|
||||||
"tags"
|
"tags"
|
||||||
inner join "tag_asset" on "tags"."id" = "tag_asset"."tagsId"
|
inner join "tag_asset" on "tags"."id" = "tag_asset"."tagsId"
|
||||||
@ -194,7 +199,9 @@ select
|
|||||||
from
|
from
|
||||||
(
|
(
|
||||||
select
|
select
|
||||||
"asset_files".*
|
"asset_files"."id",
|
||||||
|
"asset_files"."path",
|
||||||
|
"asset_files"."type"
|
||||||
from
|
from
|
||||||
"asset_files"
|
"asset_files"
|
||||||
where
|
where
|
||||||
|
@ -100,7 +100,7 @@ order by
|
|||||||
-- SharedLinkRepository.getAll
|
-- SharedLinkRepository.getAll
|
||||||
select distinct
|
select distinct
|
||||||
on ("shared_links"."createdAt") "shared_links".*,
|
on ("shared_links"."createdAt") "shared_links".*,
|
||||||
to_json("assets") as "assets",
|
"assets"."assets",
|
||||||
to_json("album") as "album"
|
to_json("album") as "album"
|
||||||
from
|
from
|
||||||
"shared_links"
|
"shared_links"
|
||||||
|
@ -37,7 +37,12 @@ on conflict ("userId", "value") do update
|
|||||||
set
|
set
|
||||||
"parentId" = $4
|
"parentId" = $4
|
||||||
returning
|
returning
|
||||||
*
|
"tags"."id",
|
||||||
|
"tags"."value",
|
||||||
|
"tags"."createdAt",
|
||||||
|
"tags"."updatedAt",
|
||||||
|
"tags"."color",
|
||||||
|
"tags"."parentId"
|
||||||
rollback
|
rollback
|
||||||
|
|
||||||
-- TagRepository.getAll
|
-- TagRepository.getAll
|
||||||
|
@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { ExpressionBuilder, Insertable, Kysely, sql, Updateable } from 'kysely';
|
import { ExpressionBuilder, Insertable, Kysely, sql, Updateable } from 'kysely';
|
||||||
import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
|
import { jsonArrayFrom, jsonObjectFrom } from 'kysely/helpers/postgres';
|
||||||
import { InjectKysely } from 'nestjs-kysely';
|
import { InjectKysely } from 'nestjs-kysely';
|
||||||
|
import { columns } from 'src/database';
|
||||||
import { Albums, DB } from 'src/db';
|
import { Albums, DB } from 'src/db';
|
||||||
import { Chunked, ChunkedArray, ChunkedSet, DummyValue, GenerateSql } from 'src/decorators';
|
import { Chunked, ChunkedArray, ChunkedSet, DummyValue, GenerateSql } from 'src/decorators';
|
||||||
import { AlbumUserCreateDto } from 'src/dtos/album.dto';
|
import { AlbumUserCreateDto } from 'src/dtos/album.dto';
|
||||||
@ -19,26 +20,8 @@ export interface AlbumInfoOptions {
|
|||||||
withAssets: boolean;
|
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'>) => {
|
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',
|
'owner',
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
@ -47,9 +30,9 @@ const withAlbumUsers = (eb: ExpressionBuilder<DB, 'albums'>) => {
|
|||||||
return jsonArrayFrom(
|
return jsonArrayFrom(
|
||||||
eb
|
eb
|
||||||
.selectFrom('albums_shared_users_users as album_users')
|
.selectFrom('albums_shared_users_users as album_users')
|
||||||
.selectAll('album_users')
|
.select('album_users.role')
|
||||||
.select((eb) =>
|
.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',
|
'user',
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -114,7 +114,7 @@ export class SharedLinkRepository {
|
|||||||
.as('assets'),
|
.as('assets'),
|
||||||
(join) => join.onTrue(),
|
(join) => join.onTrue(),
|
||||||
)
|
)
|
||||||
.select((eb) => eb.fn.toJson('assets').as('assets'))
|
.select('assets.assets')
|
||||||
.leftJoinLateral(
|
.leftJoinLateral(
|
||||||
(eb) =>
|
(eb) =>
|
||||||
eb
|
eb
|
||||||
|
@ -38,7 +38,7 @@ export class TagRepository {
|
|||||||
.insertInto('tags')
|
.insertInto('tags')
|
||||||
.values({ userId, value, parentId })
|
.values({ userId, value, parentId })
|
||||||
.onConflict((oc) => oc.columns(['userId', 'value']).doUpdateSet({ parentId }))
|
.onConflict((oc) => oc.columns(['userId', 'value']).doUpdateSet({ parentId }))
|
||||||
.returningAll()
|
.returning(columns.tag)
|
||||||
.executeTakeFirstOrThrow();
|
.executeTakeFirstOrThrow();
|
||||||
|
|
||||||
// update closure table
|
// update closure table
|
||||||
|
Loading…
x
Reference in New Issue
Block a user