mirror of
https://github.com/immich-app/immich.git
synced 2025-12-27 01:11:42 +03:00
fix(server): album perf query (#5232)
* Revert "fix: album performances (#5224)" This reverts commitc438e17954. * Revert "fix: album sorting options (#5127)" This reverts commit725f30c494.
This commit is contained in:
@@ -9,7 +9,6 @@ import {
|
||||
OneToMany,
|
||||
PrimaryGeneratedColumn,
|
||||
UpdateDateColumn,
|
||||
VirtualColumn,
|
||||
} from 'typeorm';
|
||||
import { AssetEntity } from './asset.entity';
|
||||
import { SharedLinkEntity } from './shared-link.entity';
|
||||
@@ -60,34 +59,4 @@ export class AlbumEntity {
|
||||
|
||||
@Column({ default: true })
|
||||
isActivityEnabled!: boolean;
|
||||
|
||||
@VirtualColumn({
|
||||
query: (alias) => `
|
||||
SELECT MIN(assets."fileCreatedAt")
|
||||
FROM "assets" assets
|
||||
JOIN "albums_assets_assets" aa ON aa."assetsId" = assets.id
|
||||
WHERE aa."albumsId" = ${alias}.id
|
||||
`,
|
||||
})
|
||||
startDate!: Date | null;
|
||||
|
||||
@VirtualColumn({
|
||||
query: (alias) => `
|
||||
SELECT MAX(assets."fileCreatedAt")
|
||||
FROM "assets" assets
|
||||
JOIN "albums_assets_assets" aa ON aa."assetsId" = assets.id
|
||||
WHERE aa."albumsId" = ${alias}.id
|
||||
`,
|
||||
})
|
||||
endDate!: Date | null;
|
||||
|
||||
@VirtualColumn({
|
||||
query: (alias) => `
|
||||
SELECT COUNT(assets."id")
|
||||
FROM "assets" assets
|
||||
JOIN "albums_assets_assets" aa ON aa."assetsId" = assets.id
|
||||
WHERE aa."albumsId" = ${alias}.id
|
||||
`,
|
||||
})
|
||||
assetCount!: number;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { AlbumAsset, AlbumAssets, AlbumInfoOptions, IAlbumRepository } from '@app/domain';
|
||||
import { AlbumAsset, AlbumAssetCount, AlbumAssets, AlbumInfoOptions, IAlbumRepository } from '@app/domain';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectDataSource, InjectRepository } from '@nestjs/typeorm';
|
||||
import { DataSource, FindOptionsOrder, FindOptionsRelations, In, IsNull, Not, Repository } from 'typeorm';
|
||||
@@ -56,10 +56,31 @@ export class AlbumRepository implements IAlbumRepository {
|
||||
],
|
||||
relations: { owner: true, sharedUsers: true },
|
||||
order: { createdAt: 'DESC' },
|
||||
relationLoadStrategy: 'query',
|
||||
});
|
||||
}
|
||||
|
||||
async getAssetCountForIds(ids: string[]): Promise<AlbumAssetCount[]> {
|
||||
// Guard against running invalid query when ids list is empty.
|
||||
if (!ids.length) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// Only possible with query builder because of GROUP BY.
|
||||
const countByAlbums = await this.repository
|
||||
.createQueryBuilder('album')
|
||||
.select('album.id')
|
||||
.addSelect('COUNT(albums_assets.assetsId)', 'asset_count')
|
||||
.leftJoin('albums_assets_assets', 'albums_assets', 'albums_assets.albumsId = album.id')
|
||||
.where('album.id IN (:...ids)', { ids })
|
||||
.groupBy('album.id')
|
||||
.getRawMany();
|
||||
|
||||
return countByAlbums.map<AlbumAssetCount>((albumCount) => ({
|
||||
albumId: albumCount['album_id'],
|
||||
assetCount: Number(albumCount['asset_count']),
|
||||
}));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the album IDs that have an invalid thumbnail, when:
|
||||
* - Thumbnail references an asset outside the album
|
||||
@@ -92,7 +113,6 @@ export class AlbumRepository implements IAlbumRepository {
|
||||
relations: { sharedUsers: true, sharedLinks: true, owner: true },
|
||||
where: { ownerId },
|
||||
order: { createdAt: 'DESC' },
|
||||
relationLoadStrategy: 'query',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -108,7 +128,6 @@ export class AlbumRepository implements IAlbumRepository {
|
||||
{ ownerId, sharedUsers: { id: Not(IsNull()) } },
|
||||
],
|
||||
order: { createdAt: 'DESC' },
|
||||
relationLoadStrategy: 'query',
|
||||
});
|
||||
}
|
||||
|
||||
@@ -120,7 +139,6 @@ export class AlbumRepository implements IAlbumRepository {
|
||||
relations: { sharedUsers: true, sharedLinks: true, owner: true },
|
||||
where: { ownerId, sharedUsers: { id: IsNull() }, sharedLinks: { id: IsNull() } },
|
||||
order: { createdAt: 'DESC' },
|
||||
relationLoadStrategy: 'query',
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user