P
Prisma•4w ago
RedRoss

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}`,
);
}
}
}
2 Replies
Nurul
Nurul•3w ago
Hey @RedRoss 👋 Did you had a chance to look at this GitHub Issue? https://github.com/prisma/prisma/issues/5273 I believe it's related to what you are looking for. There are a few workarounds listed there.
GitHub
Service Classes (e.g. for NestJS) · Issue #5273 · prisma/prisma
Dear Prisma Team, for my upcoming project, i would like to use Prisma, since it is ready to be used in production. I have been around for a year or so, but now finally use it in a concrete project....
RedRoss
RedRossOP•3w ago
yes I have read this issue but I am still having problems
Want results from more Discord servers?
Add your server