我有两个实体:User和Habit。一个用户可以创建多个习惯,因此我在用户上使用OneToMany关系(分别在习惯上使用ManyToOne)。
import {Entity, PrimaryGeneratedColumn, Column, CreateDateColumn, UpdateDateColumn, BeforeInsert, BeforeUpdate, OneToMany} from "typeorm";
import * as bcrypt from "bcryptjs";
import { Habit } from "../habits/habits.entity";
@Entity()
export class User {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
name: string;
@Column()
email: string;
@Column()
password: string;
@OneToMany(type => Habit, habit => habit.author)
habits: Habit[];
@CreateDateColumn()
dateCreated: Date;
@UpdateDateColumn()
dateUpdated: Date;
@BeforeInsert()
@BeforeUpdate()
async hashPassword(): Promise<void> {
this.password = await bcrypt.hash(this.password,10);
}
async comparePassword(password: string): Promise<boolean> { …Run Code Online (Sandbox Code Playgroud)