This is already an old topic, but I still would like to share my solution, maybe it will help somebody. I use Express, but my example is probably suitable for this case as well.
So in your entity class you just define an additional static `removePassword()` method, which receives an instance of entity itself, and you send an object created by this method instead of original entity object you got from DB:
```typescript
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity({ name: 'users' })
export class User {
@PrimaryGeneratedColumn('uuid')
id: string | undefined;
@Column({ type: 'varchar', length: 100, unique: true })
email: string | undefined;
@Column({ type: 'text' })
password: string | undefined;
static removePassword(userObj: User) {
return Object.fromEntries(
Object.entries(userObj).filter(([key, val]) => key !== 'password')
);
}
}
```
This is basically like calling a `filter()` method on an array, but slightly more complicated: you create a new object (the one you'll send eventually) from entries array, produced by filtering out a password entry from original entries array (with this exact `filter()` method).
In your route handlers though you'll just always do something like this:
```typescript
import { Router, Request, Response, NextFunction } from 'express';
import { User } from '../../entity/User';
import { getRepository } from 'typeorm';
const router = Router();
router.post(
'/api/users/signin',
(req: Request, res: Response, next: NextFunction) => {
const { email } = req.body;
getRepository(User)
.findOne({ email })
.then(user =>
user ? res.send(User.removePassword(user)) : res.send('No such user:(')
)
.catch(err => next(new Error(err.message)));
}
);
export { router as signinRouter };
```
You may also use regular method:
```typescript
withoutPassword() {
return Object.fromEntries(
Object.entries(this).filter(([key, val]) => key !== 'password')
);
}
```
and in your route handler:
```typescript
res.send(user.withoutPassword());
```