Files
dinggadingga-backend/src/database/brand.database.repository.ts
MastersGame 27d0e0149d feat(db): add database repositories
Upserts keyed on hiboutikId and deletion of the rows that no longer exist in Hiboutik.
2026-11-05 23:00:48 +01:00

37 lines
1.2 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../prisma/prisma.service';
import { Brand } from '../../prisma/generated/client';
import { BrandCreateInput } from '../../prisma/generated/models/Brand';
/** Database access to brands. Rows are matched with Hiboutik by `hiboutikId`. */
@Injectable()
export class BrandDatabaseRepository {
constructor(private readonly prisma: PrismaService) {}
async getAll(): Promise<Brand[]> {
return this.prisma.brand.findMany({
orderBy: { hiboutikId: 'asc' },
});
}
/** Creates or updates the brand with the same `hiboutikId`. */
async upsert(brand: BrandCreateInput): Promise<Brand> {
return this.prisma.brand.upsert({
where: { hiboutikId: brand.hiboutikId },
create: brand,
update: brand,
});
}
/**
* Deletes the brands that no longer exist in Hiboutik.
* Their products are kept: the relation is set to null (onDelete: SetNull).
*/
async deleteMissing(keptHiboutikIds: number[]): Promise<number> {
const result = await this.prisma.brand.deleteMany({
where: { hiboutikId: { notIn: keptHiboutikIds } },
});
return result.count;
}
}