2024-04-02 10:23:17 -04:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2025-01-15 11:34:11 -05:00
|
|
|
import { Insertable, Kysely, Updateable } from 'kysely';
|
|
|
|
|
import { jsonArrayFrom } from 'kysely/helpers/postgres';
|
|
|
|
|
import { InjectKysely } from 'nestjs-kysely';
|
|
|
|
|
import { DB, Memories } from 'src/db';
|
2024-04-02 10:23:17 -04:00
|
|
|
import { Chunked, ChunkedSet, DummyValue, GenerateSql } from 'src/decorators';
|
|
|
|
|
import { MemoryEntity } from 'src/entities/memory.entity';
|
|
|
|
|
import { IMemoryRepository } from 'src/interfaces/memory.interface';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class MemoryRepository implements IMemoryRepository {
|
2025-01-15 11:34:11 -05:00
|
|
|
constructor(@InjectKysely() private db: Kysely<DB>) {}
|
2024-04-02 10:23:17 -04:00
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
@GenerateSql({ params: [DummyValue.UUID] })
|
2024-04-02 10:23:17 -04:00
|
|
|
search(ownerId: string): Promise<MemoryEntity[]> {
|
2025-01-15 11:34:11 -05:00
|
|
|
return this.db
|
|
|
|
|
.selectFrom('memories')
|
|
|
|
|
.selectAll()
|
|
|
|
|
.where('ownerId', '=', ownerId)
|
|
|
|
|
.orderBy('memoryAt', 'desc')
|
|
|
|
|
.execute() as Promise<MemoryEntity[]>;
|
2024-04-02 10:23:17 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
@GenerateSql({ params: [DummyValue.UUID] })
|
2025-01-22 15:17:42 -05:00
|
|
|
get(id: string): Promise<MemoryEntity | undefined> {
|
|
|
|
|
return this.getByIdBuilder(id).executeTakeFirst() as unknown as Promise<MemoryEntity | undefined>;
|
2024-04-02 10:23:17 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
async create(memory: Insertable<Memories>, assetIds: Set<string>): Promise<MemoryEntity> {
|
|
|
|
|
const id = await this.db.transaction().execute(async (tx) => {
|
|
|
|
|
const { id } = await tx.insertInto('memories').values(memory).returning('id').executeTakeFirstOrThrow();
|
|
|
|
|
|
|
|
|
|
if (assetIds.size > 0) {
|
|
|
|
|
const values = [...assetIds].map((assetId) => ({ memoriesId: id, assetsId: assetId }));
|
|
|
|
|
await tx.insertInto('memories_assets_assets').values(values).execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return id;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this.getByIdBuilder(id).executeTakeFirstOrThrow() as unknown as Promise<MemoryEntity>;
|
2024-04-02 10:23:17 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
@GenerateSql({ params: [DummyValue.UUID, { ownerId: DummyValue.UUID, isSaved: true }] })
|
|
|
|
|
async update(id: string, memory: Updateable<Memories>): Promise<MemoryEntity> {
|
|
|
|
|
await this.db.updateTable('memories').set(memory).where('id', '=', id).execute();
|
|
|
|
|
return this.getByIdBuilder(id).executeTakeFirstOrThrow() as unknown as Promise<MemoryEntity>;
|
2024-04-02 10:23:17 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
@GenerateSql({ params: [DummyValue.UUID] })
|
2024-04-02 10:23:17 -04:00
|
|
|
async delete(id: string): Promise<void> {
|
2025-01-15 11:34:11 -05:00
|
|
|
await this.db.deleteFrom('memories').where('id', '=', id).execute();
|
2024-04-02 10:23:17 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@GenerateSql({ params: [DummyValue.UUID, [DummyValue.UUID]] })
|
|
|
|
|
@ChunkedSet({ paramIndex: 1 })
|
|
|
|
|
async getAssetIds(id: string, assetIds: string[]): Promise<Set<string>> {
|
|
|
|
|
if (assetIds.length === 0) {
|
|
|
|
|
return new Set();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
const results = await this.db
|
|
|
|
|
.selectFrom('memories_assets_assets')
|
|
|
|
|
.select(['assetsId'])
|
|
|
|
|
.where('memoriesId', '=', id)
|
|
|
|
|
.where('assetsId', 'in', assetIds)
|
|
|
|
|
.execute();
|
2024-04-02 10:23:17 -04:00
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
return new Set(results.map(({ assetsId }) => assetsId));
|
2024-04-02 10:23:17 -04:00
|
|
|
}
|
|
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
@GenerateSql({ params: [DummyValue.UUID, [DummyValue.UUID]] })
|
2024-04-02 10:23:17 -04:00
|
|
|
async addAssetIds(id: string, assetIds: string[]): Promise<void> {
|
2025-01-22 15:17:42 -05:00
|
|
|
if (assetIds.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
await this.db
|
|
|
|
|
.insertInto('memories_assets_assets')
|
2024-04-02 10:23:17 -04:00
|
|
|
.values(assetIds.map((assetId) => ({ memoriesId: id, assetsId: assetId })))
|
|
|
|
|
.execute();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Chunked({ paramIndex: 1 })
|
2025-01-15 11:34:11 -05:00
|
|
|
@GenerateSql({ params: [DummyValue.UUID, [DummyValue.UUID]] })
|
2024-04-02 10:23:17 -04:00
|
|
|
async removeAssetIds(id: string, assetIds: string[]): Promise<void> {
|
2025-01-22 15:17:42 -05:00
|
|
|
if (assetIds.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
await this.db
|
|
|
|
|
.deleteFrom('memories_assets_assets')
|
|
|
|
|
.where('memoriesId', '=', id)
|
|
|
|
|
.where('assetsId', 'in', assetIds)
|
2024-04-02 10:23:17 -04:00
|
|
|
.execute();
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-15 11:34:11 -05:00
|
|
|
private getByIdBuilder(id: string) {
|
|
|
|
|
return this.db
|
|
|
|
|
.selectFrom('memories')
|
|
|
|
|
.selectAll('memories')
|
|
|
|
|
.select((eb) =>
|
|
|
|
|
jsonArrayFrom(
|
|
|
|
|
eb
|
|
|
|
|
.selectFrom('assets')
|
|
|
|
|
.selectAll('assets')
|
|
|
|
|
.innerJoin('memories_assets_assets', 'assets.id', 'memories_assets_assets.assetsId')
|
|
|
|
|
.whereRef('memories_assets_assets.memoriesId', '=', 'memories.id')
|
|
|
|
|
.where('assets.deletedAt', 'is', null),
|
|
|
|
|
).as('assets'),
|
|
|
|
|
)
|
|
|
|
|
.where('id', '=', id)
|
|
|
|
|
.where('deletedAt', 'is', null);
|
2024-04-02 10:23:17 -04:00
|
|
|
}
|
|
|
|
|
}
|