Context: I'm using prisma ORM with typescript. I'm applying a repository pattern abstraction such that I have an interface 'IRepository', and an impure abstract class 'BaseRepository' which implements this interface to take care of basic CRUD operations (so that I don't have to re-write the implementation of these operations). Model Repositories can simply extend the base repository to get access to these basic CRUD functions.
code examples:
export abstract class BaseRepository<T extends BaseDTO> implements IRepository<T> {
public prisma = new PrismaClient();
async read(options?: ReadQueryOptions | undefined): Promise<T[]> {
// prisma query
}
async create(data: Partial<T>): Promise<T> {
}
async update(options: Partial<T>, data: Partial<T>): Promise<T[]> {
}
delete(options?: QueryOptions | undefined): Promise<T[]> {
}
}
a class 'UserRepository' can simply extend it:
export class UserRepository extends BaseRepository
The Problem Since I'm making an abstract class, It can't relate to a specific model which extends it. thus, indexing the prismaClient becomes a problem.:
export abstract class BaseRepository<T extends BaseDTO, U extends keyof PrismaClient> implements IRepository<T> {
// doesn't work, no type inference:
const modelName: U
this.prisma[modelName]
// using string also doesn't work:
const modelName: string
this.prisma[string]
{
As you can see, I've tried:
- using string
- using keyof PrismaClient
How can I achieve the type inference and code completion in this abstract class?