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 : apply particular stash to current branch.
git stash drop : 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.
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
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
@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
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
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
app.useGlobalInterceptors(new TransformInterceptor());
await app.listen(3000);
}
bootstrap();
Top comments (0)