Repository
A repository is a type that hides how a collection of entities is stored. In TypeScript it is usually a generic interface, Repo<T, Id>, plus one implementation per storage engine.
Detailed Explanation
Callers should not import SQL or fetch in every feature module. They should import UserRepo and call find, save, list. The TypeScript form of that boundary is an interface, not a base class, unless implementations share real code (then an abstract class is fine).
A useful generic is:
interface Repository<T, Id> {
findById(id: Id): Promise<T | undefined>;
save(entity: T): Promise<T>;
}
Id is often a branded type (UserId) so you cannot pass an OrderId. T is the entity. Methods that are specific to users (findByEmail) do not belong on the generic; they belong on UserRepo extends Repository<User, UserId>.
Return types should admit misses: T | undefined, not T, unless the method is require and throws. List methods should return T[] or a typed page { items: T[]; next?: Cursor }. Do not return any from the ORM and "clean it up later." Map the row to T inside the repository so the rest of the app never sees the driver type.
Implementations: a MemoryRepository<T extends { id: Id }> for tests, a PrismaUserRepo that talks to Prisma types, an HTTP repo that fetches and validates. Each implements UserRepo. The mapper from Prisma's generated type to User is the place you absorb schema drift.
The repository is a good exhaustiveness boundary. If save can fail for a known reason, return Promise<Result<T, SaveError>> with a discriminated SaveError. Throwing is fine too; then the type is Promise<T> and callers use try/catch with unknown.
Do not make every table a repository by reflex. A query that joins three tables may be a function on a query service. The pattern earns its keep when you have more than one implementation or a test fake.
Code Examples
1. Generic + specific methods.
// 1. Generic + specific methods.
type UserId = string & { readonly __brand: "UserId" };
type User = { id: UserId; email: string };
interface Repository<T, Id> {
findById(id: Id): Promise<T | undefined>;
save(entity: T): Promise<T>;
}
interface UserRepo extends Repository<User, UserId> {
findByEmail(email: string): Promise<User | undefined>;
}2. In-memory implementation for tests.
// 2. In-memory implementation for tests.
class MemoryUserRepo implements UserRepo {
constructor(private rows: User[] = []) {}
async findById(id: UserId): Promise<User | undefined> {
return this.rows.find((u) => u.id === id);
}
async findByEmail(email: string): Promise<User | undefined> {
return this.rows.find((u) => u.email === email);
}
async save(entity: User): Promise<User> {
this.rows = this.rows.filter((u) => u.id !== entity.id).concat(entity);
return entity;
}
}3. Callers never see the engine.
// 3. Callers never see the engine.
async function emailOf(repo: UserRepo, id: UserId): Promise<string | undefined> {
return (await repo.findById(id))?.email;
}Common Gotchas
Returning the ORM model as `T` couples the app to the engine
map to a plain `User` in the repo.
`find` typed as `Promise<T>` lies about misses
use `T | undefined` or a `Result`.
Brand `Id` if mixing ids is a real bug
a generic `string` id will compile everywhere.
A 30-method generic `Repository<T>` will not fit every table
keep the generic small; put special queries on the specific interface.