Overview
In my Node API, I have a bunch of repository and service classes that all do the same things for basic CRUD operations using Prisma. Rather than copy/paste a bunch of boilerplate, I've tried to make a base class that the entities can extend that holds these operations.
The solution that I've arrived at looks pretty nasty, and I hardly understand it myself. Snippet (with my shoddy notes to self) and full links are below.
Question
Does anyone know of a more elegant way to get the DTO type arguments in the repository...
export abstract class BaseRepository<PrismaType, DTO> {
from the service base class?
// Create a new type named RepositoryType that takes a Repo type parameter.
// Repo must be type that extends (inherits from) BaseRepository.
type RepositoryTypes<Repo extends BaseRepository<any, any>> =
// Now to define the type:
// Use the "extands" conditional to access the type parameters from
// BaseRepository - P and D - and cram them into a type object.
Repo extends BaseRepository<infer P, infer D> ? { prisma: P; dto: D } : never;
export abstract class BaseService<Repo extends BaseRepository<any, any>> {
Links
Contribute to mpjovanovich/horse-race-raffle-tracker development by creating an account on GitHub.
Contribute to mpjovanovich/horse-race-raffle-tracker development by creating an account on GitHub.
Contribute to mpjovanovich/horse-race-raffle-tracker development by creating an account on GitHub.