Dev note 7JAN2021
Tags: git
Published: 2022-01-07 17:45:30

## Git
### git diff
`git diff` command is to view the changes.

**git diff** - list all the changes in our working directory that are `not staged`.
**git diff HEAD** - list all the changes between working directory and HEAD.
**git diff branch1 branch2** - list all the changes between branch1 and branch2
**git diff --staged** - list all the changes between working directory and staged.

### git stash
- when you want to temporarily save changes without committing them before switching over to different branch.
- is not mandatory command.
- it just makes more convenient.

**git stash**: stash the current work.
**git stash pop** : pop recent stash.
**git stash apply**: User to apply whatever stashed away, without removing it from the stash. This can be useful if you want to apply stashed changes to multiple branches.
**git stash list** : shows all the stashes.
**git stash apply <stash-id>**: apply particular stash to current branch.
**git stash drop <stash-id>** : delete particular stash.

---
## Nest.js
### Hashing Password
**Hashed password cannot be decrypted.**

To secure user from rainbow table, need to use `salt`.

So the step to save password is.
- when user inputs plain text version of password.
- salt it.
- then hash it.
- then save it in db.

Library to encrypt password.
**bcrypt**
- bcrypt not only hash password, but also salt it.

``` ts
import * as bcrypt from 'bcrypt'

const salt = bcrypt.genSalt()
const hashedPassword = bcrypt.hash(password, salt)
const createdUser = user.create({username,password:hashedPassword})
user.save(createdUser)
```

### Many to One and One to Many
/tasks.entity.ts
``` ts
import { Exclude } from 'class-transformer';
import { User } from 'src/users/user.entity';
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class Task {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column()
    title: string;

    @ManyToOne((_type) => User, (user) => user.tasks, { eager: false })
    @Exclude({ toPlainOnly: true })
    user: User;
}
```
/user.entity.ts
``` ts
@Entity()
export class User {
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Column({ unique: true })
    username: string;

    @Column()
    password: string;

    @OneToMany((_type) => Task, (task) => task.user, { eager: true })
    tasks: Task[];
}
```
/transform.interceptor.ts
```ts
import {
    CallHandler,
    ExecutionContext,
    Injectable,
    NestInterceptor,
} from '@nestjs/common';
import { classToPlain, instanceToPlain } from 'class-transformer';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class TransformInterceptor implements NestInterceptor {
    intercept(
        context: ExecutionContext,
        next: CallHandler<any>,
    ): Observable<any> | Promise<Observabl
e<any>> {
        return next.handle().pipe(map((data) => instanceToPlain(data)));
    }
}

```
/main.ts
```ts
async function bootstrap() {
    const app = await NestFactory.create(AppModule);

    app.useGlobalPipes(new ValidationPipe());
    app.useGlobalInterceptors(new TransformInterceptor());

    await app.listen(3000);
}
bootstrap();
```