ORM Models
An ORM model is the TypeScript type (and often class) that stands for a database table. It is a claim about columns. It is not the table. Generated models stay accurate; hand-written ones drift.
Detailed Explanation
ORMs in TypeScript (Prisma, TypeORM, Sequelize, Drizzle, Kysely) expose a type per table: fields for columns, maybe relations as nested types. You use that type in repositories and, if you are careless, all the way to the HTTP response.
The type-system issues are always the same.
The model is not your domain type. It has passwordHash, deletedAt, snake_case leftovers, and relation load flags (user.posts maybe Post[] | undefined depending on include). Map to a PublicUser at the edge. Pick / Omit help; an explicit adapter is clearer when names differ.
Relations change the type. find({ include: { posts: true } }) is a different type from find(). Prisma encodes this as generics on the query. TypeORM is looser. If you type the method as Promise<User> and sometimes include posts, you have lost the extra fields in the type or you have made them always optional. Prefer returning the specific generated type of that query, or mapping to a DTO that states whether posts are present.
Classes vs types. TypeORM and Sequelize use classes as both the runtime schema and the type. Decorators are erased; the class remains. Prisma and Kysely use generated types and a client object. You cannot instanceof a Prisma User type. You can instanceof a TypeORM entity if you actually constructed it, which you often did not (the ORM did).
Hand-written models (interface User { id: string }) next to a migrating schema will be wrong. Prefer generation from the schema, or a single schema that produces both SQL and types (Drizzle, Kysely codegen, Zod+Kysely). If you hand-write, put the model next to the SQL and review them as one change.
null vs undefined again. ORMs differ. Align the model with the column: nullable column → | null. Convert to undefined in the domain mapper if that is your app convention.
See Prisma types for the generated-client case, and repository for where models should stop.
Code Examples
1. ORM-shaped row vs a public DTO.
// 1. ORM-shaped row vs a public DTO.
type UserModel = {
id: string;
email: string;
passwordHash: string;
deletedAt: Date | null;
};
type PublicUser = { id: string; email: string };
function toPublic(model: UserModel): PublicUser {
return { id: model.id, email: model.email };
}2. Include changes the type — name both.
// 2. Include changes the type — name both.
type User = { id: string; email: string };
type Post = { id: string; title: string };
type UserWithPosts = User & { posts: Post[] };
function titles(user: UserWithPosts): string[] {
return user.posts.map((p) => p.title);
}3. Nullable column stays null until you map it.
// 3. Nullable column stays null until you map it.
function nickname(model: { nickname: string | null }): string | undefined {
return model.nickname ?? undefined;
}Common Gotchas
Returning the ORM model from an HTTP handler leaks columns
`passwordHash` is a type error you want, via `PublicUser`.
`Promise<User>` for every find hides `include`
the extra relations are either untyped or always optional. Name the variant.
You cannot `instanceof` a generated type alias
use a field check or a class entity.
Hand-written models drift
generate, or review SQL and types in the same change.