mirror of
https://github.com/immich-app/immich.git
synced 2025-02-11 01:18:24 -05:00
refactor: get map markers database query (#15899)
This commit is contained in:
parent
99de52479e
commit
58bf58b393
2 changed files with 41 additions and 24 deletions
24
server/src/queries/map.repository.sql
Normal file
24
server/src/queries/map.repository.sql
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
-- NOTE: This file is auto generated by ./sql-generator
|
||||||
|
|
||||||
|
-- MapRepository.getMapMarkers
|
||||||
|
select
|
||||||
|
"id",
|
||||||
|
"exif"."latitude" as "lat",
|
||||||
|
"exif"."longitude" as "lon",
|
||||||
|
"exif"."city",
|
||||||
|
"exif"."state",
|
||||||
|
"exif"."country"
|
||||||
|
from
|
||||||
|
"assets"
|
||||||
|
inner join "exif" on "assets"."id" = "exif"."assetId"
|
||||||
|
and "exif"."latitude" is not null
|
||||||
|
and "exif"."longitude" is not null
|
||||||
|
left join "albums_assets_assets" on "assets"."id" = "albums_assets_assets"."assetsId"
|
||||||
|
where
|
||||||
|
"isVisible" = $1
|
||||||
|
and "deletedAt" is null
|
||||||
|
and "exif"."latitude" is not null
|
||||||
|
and "exif"."longitude" is not null
|
||||||
|
and "ownerId" in ($2)
|
||||||
|
order by
|
||||||
|
"fileCreatedAt" desc
|
|
@ -8,7 +8,7 @@ import { readFile } from 'node:fs/promises';
|
||||||
import readLine from 'node:readline';
|
import readLine from 'node:readline';
|
||||||
import { citiesFile } from 'src/constants';
|
import { citiesFile } from 'src/constants';
|
||||||
import { DB, GeodataPlaces, NaturalearthCountries } from 'src/db';
|
import { DB, GeodataPlaces, NaturalearthCountries } from 'src/db';
|
||||||
import { AssetEntity, withExif } from 'src/entities/asset.entity';
|
import { DummyValue, GenerateSql } from 'src/decorators';
|
||||||
import { NaturalEarthCountriesTempEntity } from 'src/entities/natural-earth-countries.entity';
|
import { NaturalEarthCountriesTempEntity } from 'src/entities/natural-earth-countries.entity';
|
||||||
import { LogLevel, SystemMetadataKey } from 'src/enum';
|
import { LogLevel, SystemMetadataKey } from 'src/enum';
|
||||||
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
|
import { ISystemMetadataRepository } from 'src/interfaces/system-metadata.interface';
|
||||||
|
@ -76,17 +76,19 @@ export class MapRepository {
|
||||||
this.logger.log('Geodata import completed');
|
this.logger.log('Geodata import completed');
|
||||||
}
|
}
|
||||||
|
|
||||||
async getMapMarkers(
|
@GenerateSql({ params: [[DummyValue.UUID], []] })
|
||||||
ownerIds: string[],
|
getMapMarkers(ownerIds: string[], albumIds: string[], options: MapMarkerSearchOptions = {}) {
|
||||||
albumIds: string[],
|
|
||||||
options: MapMarkerSearchOptions = {},
|
|
||||||
): Promise<MapMarker[]> {
|
|
||||||
const { isArchived, isFavorite, fileCreatedAfter, fileCreatedBefore } = options;
|
const { isArchived, isFavorite, fileCreatedAfter, fileCreatedBefore } = options;
|
||||||
|
|
||||||
const assets = (await this.db
|
return this.db
|
||||||
.selectFrom('assets')
|
.selectFrom('assets')
|
||||||
.$call(withExif)
|
.innerJoin('exif', (builder) =>
|
||||||
.select('id')
|
builder
|
||||||
|
.onRef('assets.id', '=', 'exif.assetId')
|
||||||
|
.on('exif.latitude', 'is not', null)
|
||||||
|
.on('exif.longitude', 'is not', null),
|
||||||
|
)
|
||||||
|
.select(['id', 'exif.latitude as lat', 'exif.longitude as lon', 'exif.city', 'exif.state', 'exif.country'])
|
||||||
.leftJoin('albums_assets_assets', (join) => join.onRef('assets.id', '=', 'albums_assets_assets.assetsId'))
|
.leftJoin('albums_assets_assets', (join) => join.onRef('assets.id', '=', 'albums_assets_assets.assetsId'))
|
||||||
.where('isVisible', '=', true)
|
.where('isVisible', '=', true)
|
||||||
.$if(isArchived !== undefined, (q) => q.where('isArchived', '=', isArchived!))
|
.$if(isArchived !== undefined, (q) => q.where('isArchived', '=', isArchived!))
|
||||||
|
@ -96,30 +98,21 @@ export class MapRepository {
|
||||||
.where('deletedAt', 'is', null)
|
.where('deletedAt', 'is', null)
|
||||||
.where('exif.latitude', 'is not', null)
|
.where('exif.latitude', 'is not', null)
|
||||||
.where('exif.longitude', 'is not', null)
|
.where('exif.longitude', 'is not', null)
|
||||||
.where((eb) => {
|
.where((builder) => {
|
||||||
const ors: Expression<SqlBool>[] = [];
|
const expression: Expression<SqlBool>[] = [];
|
||||||
|
|
||||||
if (ownerIds.length > 0) {
|
if (ownerIds.length > 0) {
|
||||||
ors.push(eb('ownerId', 'in', ownerIds));
|
expression.push(builder('ownerId', 'in', ownerIds));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (albumIds.length > 0) {
|
if (albumIds.length > 0) {
|
||||||
ors.push(eb('albums_assets_assets.albumsId', 'in', albumIds));
|
expression.push(builder('albums_assets_assets.albumsId', 'in', albumIds));
|
||||||
}
|
}
|
||||||
|
|
||||||
return eb.or(ors);
|
return builder.or(expression);
|
||||||
})
|
})
|
||||||
.orderBy('fileCreatedAt', 'desc')
|
.orderBy('fileCreatedAt', 'desc')
|
||||||
.execute()) as any as AssetEntity[];
|
.execute() as Promise<MapMarker[]>;
|
||||||
|
|
||||||
return assets.map((asset) => ({
|
|
||||||
id: asset.id,
|
|
||||||
lat: asset.exifInfo!.latitude!,
|
|
||||||
lon: asset.exifInfo!.longitude!,
|
|
||||||
city: asset.exifInfo!.city,
|
|
||||||
state: asset.exifInfo!.state,
|
|
||||||
country: asset.exifInfo!.country,
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async reverseGeocode(point: GeoPoint): Promise<ReverseGeocodeResult> {
|
async reverseGeocode(point: GeoPoint): Promise<ReverseGeocodeResult> {
|
||||||
|
|
Loading…
Add table
Reference in a new issue