RedRoss
Explore posts from serversPPrisma
•Created by RedRoss on 11/11/2024 in #help-and-questions
How generete enum type from hasura?
10 replies
PPrisma
•Created by RedRoss on 10/23/2024 in #help-and-questions
how can I implement an abstract class to apply the repository pattern?
I use nestjs and would like to implement the pattern repository as it is used in typeorm
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}`,
);
}
}
}
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}`,
);
}
}
}
5 replies