Is there a proper way or a design pattern that just fits when adding types of different parts of layers in a ts project?
I'm using Prisma ORM, and have these dtos, repositories, and services. I just want Types to be as seperated as possible for a given API endpoint layer by layer. My motivation to do so is to seperate concerns over repositories, services and client communication over dtos. Repositories stay as a reflection of database as is, and so on.
Thanks in advance for thoughts.
#Layering of Types
13 messages · Page 1 of 1 (latest)
I failed to understand what you mean by that, can you post exemples of type you aren't satisfied with?
He is basically self-answering his question xD
two schools of thought regarding modules/directory structure are feature-based and function-based organization. it sounds like you currently have function-based but want to move to feature-based? like group all code related to users in one directory (including controllers, services, repositories, etc), all code related to widgets in another directory, etc, rather than grouping all services together, all controllers together, etc. did i understand correctly?
you asked specifically about types but i don't know why those are special; they're just code and should follow the same organizational principles as everything else
in general i define named types in the files where they are used/owned, rather than like having a separate types.ts file (in case that's what you're doing now)
like if your UserRepository has a method that returns a UserMetadata object, then i'd probably put the definition for UserMetadata in the file where UserRepository is implemented
import { Prisma, Driver } from '@prisma/client';
export type DriverRecord = Driver;
export type CreateDriverInput = Prisma.DriverCreateInput;
import { PrismaClient } from '@prisma/client';
import { DriverRecord, CreateDriverInput } from '../types/repository';
export class DriverRepository {
constructor(private prisma: PrismaClient) {}
async findById(id: string): Promise<DriverRecord | null> {
return this.prisma.driver.findUnique({ where: { id } });
}
async create(data: CreateDriverInput): Promise<DriverRecord> {
return this.prisma.driver.create({ data });
}
}``` this is somewhat what I had in mind, and creating the same concept for each service and controller layer of this. I believe my question wasn't as clear as it was in my head, but what I try to find out is, how to write these kinds of things in more idiomatic way in Typescript. Because the general principle of these kinds of stuff comes from OOP, having things like mapper patterns, anti-corruption-layer, and dtos
I literally use the same pattern dude
I don't think there is a better way just other schools of thought
@fleet chasm
I don't see any heresy, it's a wrapper but it's pretty pristine from the looks of it
Thanks y'all. 