refactor: migrate memory repository (#15532)

This commit is contained in:
Jason Rasmussen
2025-01-22 16:39:13 -05:00
committed by GitHub
parent ca3619658b
commit 1f19a65d1a
12 changed files with 47 additions and 55 deletions

View File

@@ -4,29 +4,28 @@ import { jsonArrayFrom } from 'kysely/helpers/postgres';
import { InjectKysely } from 'nestjs-kysely';
import { DB, Memories } from 'src/db';
import { Chunked, ChunkedSet, DummyValue, GenerateSql } from 'src/decorators';
import { MemoryEntity } from 'src/entities/memory.entity';
import { IMemoryRepository } from 'src/interfaces/memory.interface';
import { IBulkAsset } from 'src/utils/asset.util';
@Injectable()
export class MemoryRepository implements IMemoryRepository {
export class MemoryRepository implements IBulkAsset {
constructor(@InjectKysely() private db: Kysely<DB>) {}
@GenerateSql({ params: [DummyValue.UUID] })
search(ownerId: string): Promise<MemoryEntity[]> {
search(ownerId: string) {
return this.db
.selectFrom('memories')
.selectAll()
.where('ownerId', '=', ownerId)
.orderBy('memoryAt', 'desc')
.execute() as Promise<MemoryEntity[]>;
.execute();
}
@GenerateSql({ params: [DummyValue.UUID] })
get(id: string): Promise<MemoryEntity | undefined> {
return this.getByIdBuilder(id).executeTakeFirst() as unknown as Promise<MemoryEntity | undefined>;
get(id: string) {
return this.getByIdBuilder(id).executeTakeFirst();
}
async create(memory: Insertable<Memories>, assetIds: Set<string>): Promise<MemoryEntity> {
async create(memory: Insertable<Memories>, assetIds: Set<string>) {
const id = await this.db.transaction().execute(async (tx) => {
const { id } = await tx.insertInto('memories').values(memory).returning('id').executeTakeFirstOrThrow();
@@ -38,25 +37,25 @@ export class MemoryRepository implements IMemoryRepository {
return id;
});
return this.getByIdBuilder(id).executeTakeFirstOrThrow() as unknown as Promise<MemoryEntity>;
return this.getByIdBuilder(id).executeTakeFirstOrThrow();
}
@GenerateSql({ params: [DummyValue.UUID, { ownerId: DummyValue.UUID, isSaved: true }] })
async update(id: string, memory: Updateable<Memories>): Promise<MemoryEntity> {
async update(id: string, memory: Updateable<Memories>) {
await this.db.updateTable('memories').set(memory).where('id', '=', id).execute();
return this.getByIdBuilder(id).executeTakeFirstOrThrow() as unknown as Promise<MemoryEntity>;
return this.getByIdBuilder(id).executeTakeFirstOrThrow();
}
@GenerateSql({ params: [DummyValue.UUID] })
async delete(id: string): Promise<void> {
async delete(id: string) {
await this.db.deleteFrom('memories').where('id', '=', id).execute();
}
@GenerateSql({ params: [DummyValue.UUID, [DummyValue.UUID]] })
@ChunkedSet({ paramIndex: 1 })
async getAssetIds(id: string, assetIds: string[]): Promise<Set<string>> {
async getAssetIds(id: string, assetIds: string[]) {
if (assetIds.length === 0) {
return new Set();
return new Set<string>();
}
const results = await this.db
@@ -70,7 +69,7 @@ export class MemoryRepository implements IMemoryRepository {
}
@GenerateSql({ params: [DummyValue.UUID, [DummyValue.UUID]] })
async addAssetIds(id: string, assetIds: string[]): Promise<void> {
async addAssetIds(id: string, assetIds: string[]) {
if (assetIds.length === 0) {
return;
}
@@ -83,7 +82,7 @@ export class MemoryRepository implements IMemoryRepository {
@Chunked({ paramIndex: 1 })
@GenerateSql({ params: [DummyValue.UUID, [DummyValue.UUID]] })
async removeAssetIds(id: string, assetIds: string[]): Promise<void> {
async removeAssetIds(id: string, assetIds: string[]) {
if (assetIds.length === 0) {
return;
}