import { Prisma, PrismaClient } from '@prisma/client';
import { BaseInterfaceRepository } from './base.interface.repository';
export abstract class BaseAbstractRepository<T, CreateInput>
implements BaseInterfaceRepository<T, CreateInput>
{
protected readonly prisma: PrismaClient;
protected modelName: T; // What type should I use?
protected constructor(prisma: PrismaClient, modelName: T) {
this.prisma = prisma;
this.modelName = modelName;
}
public async create(
data: CreateInput,
transaction?: Prisma.TransactionClient,
): Promise<T> {
try {
if (transaction) {
return await transaction[this.modelName].create({ data });
}
return await this.modelName.create({ data });
} catch (error) {
throw new Error(`Failed to create entity: ${error.message}`);
}
}
public async findOneById(
id: number,
transaction?: Prisma.TransactionClient,
): Promise<T | null> {
try {
if (transaction) {
return await transaction[this.modelName].findUnique({ where: { id } });
}
return await this.modelName.findUnique({ where: { id } });
} catch (error) {
throw new Error(`Failed to find entity with id ${id}: ${error.message}`);
}
}
public async remove(
id: number,
transaction?: Prisma.TransactionClient,
): Promise<T> {
try {
if (transaction) {
return await transaction[this.modelName].delete({ where: { id } });
}
return await this.modelName.delete({ where: { id } });
} catch (error) {
throw new Error(
`Failed to remove entity with id ${id}: ${error.message}`,
);
}
}
}