Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate shared-link repository to kysely #15289

Merged
merged 5 commits into from
Jan 18, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions e2e/src/api/specs/shared-link.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@
expect(status).toBe(200);
expect(body).toEqual(
expect.objectContaining({
album,
album: expect.objectContaining({ id: album.id }),
userId: user1.userId,
type: SharedLinkType.Album,
}),
Expand Down Expand Up @@ -208,7 +208,7 @@
expect(status).toBe(200);
expect(body).toEqual(
expect.objectContaining({
album,
album: expect.objectContaining({ id: album.id }),
userId: user1.userId,
type: SharedLinkType.Album,
}),
Expand All @@ -220,7 +220,7 @@

expect(status).toBe(200);
expect(body.assets).toHaveLength(1);
expect(body.assets[0]).toEqual(

Check failure on line 223 in e2e/src/api/specs/shared-link.e2e-spec.ts

View workflow job for this annotation

GitHub Actions / End-to-End Tests (Server & CLI)

src/api/specs/shared-link.e2e-spec.ts > /shared-links > GET /shared-links/me > should return metadata for album shared link

AssertionError: expected { …(25) } to deeply equal ObjectContaining{…} - Expected + Received - ObjectContaining { - "exifInfo": Any<Object>, - "fileCreatedAt": Any<String>, - "localDateTime": Any<String>, + Object { + "checksum": "s2L1+OUcGNlbd1Vr0904+kxCNO0=", + "deviceAssetId": "test-1", + "deviceId": "test", + "duplicateId": null, + "duration": "0:00:00.00000", + "fileCreatedAt": "2025-01-17T15:59:28.089+00:00", + "fileModifiedAt": "2025-01-17T15:59:28.089+00:00", + "hasMetadata": true, + "id": "fd211338-1942-40a1-b73f-1e174d481ebc", + "isArchived": false, + "isFavorite": false, + "isOffline": false, + "isTrashed": false, + "libraryId": null, + "livePhotoVideoId": null, + "localDateTime": "2025-01-17T15:59:28.089+00:00", "originalFileName": "example.png", + "originalMimeType": "image/png", + "originalPath": "upload/upload/c960d375-5db8-46a3-9fd3-59f7c4d06879/98/00/9800a03c-90cb-4686-bf97-26e2a029daf9.png", + "ownerId": "c960d375-5db8-46a3-9fd3-59f7c4d06879", + "people": Array [], + "resized": true, + "thumbhash": "AAgCBwAAAAAAAAAAAAAAAAAAAAAAAAAA", + "type": "IMAGE", + "updatedAt": "2025-01-17T15:59:28.093567+00:00", } ❯ src/api/specs/shared-link.e2e-spec.ts:223:30
expect.objectContaining({
originalFileName: 'example.png',
localDateTime: expect.any(String),
Expand Down Expand Up @@ -262,7 +262,7 @@
expect(status).toBe(200);
expect(body).toEqual(
expect.objectContaining({
album,
album: expect.objectContaining({ id: album.id }),
userId: user1.userId,
type: SharedLinkType.Album,
}),
Expand Down
10 changes: 6 additions & 4 deletions server/src/interfaces/shared-link.interface.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Insertable, Updateable } from 'kysely';
import { SharedLinks } from 'src/db';
import { SharedLinkEntity } from 'src/entities/shared-link.entity';

export const ISharedLinkRepository = 'ISharedLinkRepository';

export interface ISharedLinkRepository {
getAll(userId: string): Promise<SharedLinkEntity[]>;
get(userId: string, id: string): Promise<SharedLinkEntity | null>;
getByKey(key: Buffer): Promise<SharedLinkEntity | null>;
create(entity: Partial<SharedLinkEntity>): Promise<SharedLinkEntity>;
update(entity: Partial<SharedLinkEntity>): Promise<SharedLinkEntity>;
get(userId: string, id: string): Promise<SharedLinkEntity | undefined>;
getByKey(key: Buffer): Promise<SharedLinkEntity | undefined>;
create(entity: Insertable<SharedLinks> & { assetIds?: string[] }): Promise<SharedLinkEntity>;
update(entity: Updateable<SharedLinks> & { id: string; assetIds?: string[] }): Promise<SharedLinkEntity>;
remove(entity: SharedLinkEntity): Promise<void>;
}
510 changes: 188 additions & 322 deletions server/src/queries/shared.link.repository.sql

Large diffs are not rendered by default.

288 changes: 225 additions & 63 deletions server/src/repositories/shared-link.repository.ts
Original file line number Diff line number Diff line change
@@ -1,90 +1,252 @@
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Insertable, Kysely, sql, Updateable } from 'kysely';
import { jsonObjectFrom } from 'kysely/helpers/postgres';
import _ from 'lodash';
import { InjectKysely } from 'nestjs-kysely';
import { DB, SharedLinks } from 'src/db';
import { DummyValue, GenerateSql } from 'src/decorators';
import { SharedLinkEntity } from 'src/entities/shared-link.entity';
import { SharedLinkType } from 'src/enum';
import { ISharedLinkRepository } from 'src/interfaces/shared-link.interface';
import { Repository } from 'typeorm';

@Injectable()
export class SharedLinkRepository implements ISharedLinkRepository {
constructor(@InjectRepository(SharedLinkEntity) private repository: Repository<SharedLinkEntity>) {}
constructor(@InjectKysely() private db: Kysely<DB>) {}

@GenerateSql({ params: [DummyValue.UUID, DummyValue.UUID] })
get(userId: string, id: string): Promise<SharedLinkEntity | null> {
return this.repository.findOne({
where: {
id,
userId,
},
relations: {
assets: {
exifInfo: true,
},
album: {
assets: {
exifInfo: true,
},
owner: true,
},
},
order: {
createdAt: 'DESC',
assets: {
fileCreatedAt: 'ASC',
},
album: {
assets: {
fileCreatedAt: 'ASC',
},
},
},
});
get(userId: string, id: string): Promise<SharedLinkEntity | undefined> {
return this.db
.selectFrom('shared_links')
.selectAll('shared_links')
.leftJoinLateral(
(eb) =>
eb
.selectFrom('shared_link__asset')
.whereRef('shared_links.id', '=', 'shared_link__asset.sharedLinksId')
.innerJoin('assets', 'assets.id', 'shared_link__asset.assetsId')
.where('assets.deletedAt', 'is', null)
.selectAll('assets')
.innerJoinLateral(
(eb) => eb.selectFrom('exif').selectAll('exif').whereRef('exif.assetId', '=', 'assets.id').as('exifInfo'),
(join) => join.onTrue(),
)
.select((eb) => eb.fn.toJson('exifInfo').as('exifInfo'))
.orderBy('assets.fileCreatedAt', 'asc')
.as('a'),
(join) => join.onTrue(),
)
.leftJoinLateral(
(eb) =>
eb
.selectFrom('albums')
.selectAll('albums')
.whereRef('albums.id', '=', 'shared_links.albumId')
.where('albums.deletedAt', 'is', null)
.leftJoin('albums_assets_assets', 'albums_assets_assets.albumsId', 'albums.id')
.leftJoinLateral(
(eb) =>
eb
.selectFrom('assets')
.selectAll('assets')
.whereRef('albums_assets_assets.assetsId', '=', 'assets.id')
.where('assets.deletedAt', 'is', null)
.innerJoinLateral(
(eb) =>
eb.selectFrom('exif').selectAll('exif').whereRef('exif.assetId', '=', 'assets.id').as('exifInfo'),
(join) => join.onTrue(),
)
.select((eb) => eb.fn.toJson(eb.table('exifInfo')).as('exifInfo'))
.orderBy('assets.fileCreatedAt', 'asc')
.as('assets'),
(join) => join.onTrue(),
)
.innerJoinLateral(
(eb) =>
eb
.selectFrom('users')
.selectAll('users')
.whereRef('users.id', '=', 'albums.ownerId')
.where('users.deletedAt', 'is', null)
.as('owner'),
(join) => join.onTrue(),
)
.select((eb) =>
eb.fn.coalesce(eb.fn.jsonAgg('assets').filterWhere('assets.id', 'is not', null), sql`'[]'`).as('assets'),
)
.select((eb) => eb.fn.toJson('owner').as('owner'))
.groupBy(['albums.id', sql`"owner".*`])
.as('album'),
(join) => join.onTrue(),
)
.select((eb) => eb.fn.coalesce(eb.fn.jsonAgg('a').filterWhere('a.id', 'is not', null), sql`'[]'`).as('assets'))
.groupBy(['shared_links.id', sql`"album".*`])
.select((eb) => eb.fn.toJson('album').as('album'))
.where('shared_links.id', '=', id)
.where('shared_links.userId', '=', userId)
.where((eb) => eb.or([eb('shared_links.type', '=', SharedLinkType.INDIVIDUAL), eb('album.id', 'is not', null)]))
.orderBy('shared_links.createdAt', 'desc')
.executeTakeFirst() as Promise<SharedLinkEntity | undefined>;
}

@GenerateSql({ params: [DummyValue.UUID] })
getAll(userId: string): Promise<SharedLinkEntity[]> {
return this.repository.find({
where: {
userId,
},
relations: {
assets: true,
album: {
owner: true,
},
},
order: {
createdAt: 'DESC',
},
});
return this.db
.selectFrom('shared_links')
.selectAll('shared_links')
.where('shared_links.userId', '=', userId)
.leftJoin('shared_link__asset', 'shared_link__asset.sharedLinksId', 'shared_links.id')
.leftJoinLateral(
(eb) =>
eb
.selectFrom('assets')
.whereRef('assets.id', '=', 'shared_link__asset.assetsId')
.where('assets.deletedAt', 'is', null)
.selectAll('assets')
.as('assets'),
(join) => join.onTrue(),
)
.leftJoinLateral(
(eb) =>
eb
.selectFrom('albums')
.selectAll('albums')
.whereRef('albums.id', '=', 'shared_links.albumId')
.innerJoinLateral(
(eb) =>
eb
.selectFrom('users')
.select([
'users.id',
'users.email',
'users.createdAt',
'users.profileImagePath',
'users.isAdmin',
'users.shouldChangePassword',
'users.deletedAt',
'users.oauthId',
'users.updatedAt',
'users.storageLabel',
'users.name',
'users.quotaSizeInBytes',
'users.quotaUsageInBytes',
'users.status',
'users.profileChangedAt',
])
.whereRef('users.id', '=', 'albums.ownerId')
.where('users.deletedAt', 'is', null)
.as('owner'),
(join) => join.onTrue(),
)
.select((eb) => eb.fn.toJson('owner').as('owner'))
.where('albums.deletedAt', 'is', null)
.as('album'),
(join) => join.onTrue(),
)
.select((eb) => eb.fn.toJson('album').as('album'))
.where((eb) => eb.or([eb('shared_links.type', '=', SharedLinkType.INDIVIDUAL), eb('album.id', 'is not', null)]))
.orderBy('album.createdAt', 'desc')
.distinctOn(['album.createdAt', 'shared_links.id'])
.execute() as unknown as Promise<SharedLinkEntity[]>;
}

@GenerateSql({ params: [DummyValue.BUFFER] })
async getByKey(key: Buffer): Promise<SharedLinkEntity | null> {
return await this.repository.findOne({
where: {
key,
},
relations: {
user: true,
},
});
async getByKey(key: Buffer): Promise<SharedLinkEntity | undefined> {
return this.db
.selectFrom('shared_links')
.selectAll('shared_links')
.where('shared_links.key', '=', key)
.leftJoin('albums', 'albums.id', 'shared_links.albumId')
.where('albums.deletedAt', 'is', null)
.select((eb) =>
jsonObjectFrom(
eb
.selectFrom('users')
.select([
'users.id',
'users.email',
'users.createdAt',
'users.profileImagePath',
'users.isAdmin',
'users.shouldChangePassword',
'users.deletedAt',
'users.oauthId',
'users.updatedAt',
'users.storageLabel',
'users.name',
'users.quotaSizeInBytes',
'users.quotaUsageInBytes',
'users.status',
'users.profileChangedAt',
])
.whereRef('users.id', '=', 'shared_links.userId'),
).as('user'),
)
.where((eb) => eb.or([eb('shared_links.type', '=', SharedLinkType.INDIVIDUAL), eb('albums.id', 'is not', null)]))
.executeTakeFirst() as Promise<SharedLinkEntity | undefined>;
}

create(entity: Partial<SharedLinkEntity>): Promise<SharedLinkEntity> {
return this.save(entity);
async create(entity: Insertable<SharedLinks> & { assetIds?: string[] }): Promise<SharedLinkEntity> {
const { id } = await this.db
.insertInto('shared_links')
.values(_.omit(entity, 'assetIds'))
.returningAll()
.executeTakeFirstOrThrow();

if (entity.assetIds && entity.assetIds.length > 0) {
await this.db
.insertInto('shared_link__asset')
.values(entity.assetIds!.map((assetsId) => ({ assetsId, sharedLinksId: id })))
.execute();
}

return this.getSharedLinks(id);
}

update(entity: Partial<SharedLinkEntity>): Promise<SharedLinkEntity> {
return this.save(entity);
async update(entity: Updateable<SharedLinks> & { id: string; assetIds?: string[] }): Promise<SharedLinkEntity> {
const { id } = await this.db
.updateTable('shared_links')
.set(_.omit(entity, 'assets', 'album', 'assetIds'))
.where('shared_links.id', '=', entity.id)
.returningAll()
.executeTakeFirstOrThrow();

if (entity.assetIds && entity.assetIds.length > 0) {
await this.db
.insertInto('shared_link__asset')
.values(entity.assetIds!.map((assetsId) => ({ assetsId, sharedLinksId: id })))
.execute();
}

return this.getSharedLinks(id);
}

async remove(entity: SharedLinkEntity): Promise<void> {
await this.repository.remove(entity);
await this.db.deleteFrom('shared_links').where('shared_links.id', '=', entity.id).execute();
}

private async save(entity: Partial<SharedLinkEntity>): Promise<SharedLinkEntity> {
await this.repository.save(entity);
return this.repository.findOneOrFail({ where: { id: entity.id } });
private getSharedLinks(id: string) {
return this.db
.selectFrom('shared_links')
.selectAll('shared_links')
.where('shared_links.id', '=', id)
.leftJoin('shared_link__asset', 'shared_link__asset.sharedLinksId', 'shared_links.id')
.leftJoinLateral(
(eb) =>
eb
.selectFrom('assets')
.whereRef('assets.id', '=', 'shared_link__asset.assetsId')
.selectAll('assets')
.innerJoinLateral(
(eb) => eb.selectFrom('exif').whereRef('exif.assetId', '=', 'assets.id').selectAll().as('exif'),
(join) => join.onTrue(),
)
.as('assets'),
(join) => join.onTrue(),
)
.select((eb) =>
eb.fn.coalesce(eb.fn.jsonAgg('assets').filterWhere('assets.id', 'is not', null), sql`'[]'`).as('assets'),
)
.groupBy('shared_links.id')
.executeTakeFirstOrThrow() as Promise<SharedLinkEntity>;
}
}
1 change: 0 additions & 1 deletion server/src/services/auth.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ describe('AuthService', () => {

describe('validate - shared key', () => {
it('should not accept a non-existent key', async () => {
sharedLinkMock.getByKey.mockResolvedValue(null);
await expect(
sut.authenticate({
headers: { 'x-immich-share-key': 'key' },
Expand Down
Loading
Loading