Answer the question
In order to leave comments, you need to log in
How to create relationships in a database?
Hello. I have tables. users, candidate, recruiter
When I create users
, I automatically create candidate
or recruiter
depending on which role the user has chosen on the front.
The columns ( photoURL, username ) in each of the tables can be empty.
const credentials = {
username: "One",
photoURL: "http://image-way.jpg"
email: "[email protected]",
role: "recruiter"
}
user = await this.userService.create(credentials);
const userCreationData = {
fullName: credentials.username,
profilePhoto: credentials.photoURL,
};
this.userService.createUserByRole(user, userCreationData);
const jwtAccessToken = this.generateAccessToken(user);
response = {
accessToken: jwtAccessToken,
redirectURL: `/sign-up/${user.role}`,
};
export class CreateUserByRoleDto {
fullName: string;
profilePhoto: string;
}
async createUserByRole(
userData: User,
createUserByRoleDto?: CreateUserByRoleDto,
): Promise<void> {
const user = await this.userRepository.findOne({ email: userData.email });
if (!user) throw new NotFoundException('User not found');
let createdUser: Recruiter | Candidate;
if (user.role === 'recruiter') {
createdUser = this.recruiterRepository.create();
createdUser = Object.assign(createdUser, createUserByRoleDto, {
user, // вот это создает мне userId в таблице recruiter
});
} else {
createdUser = this.candidateRepository.create();
createdUser = Object.assign(createdUser, createUserByRoleDto, {
user, // вот это создает мне userId в таблице candidate
});
}
await createdUser.save();
}
import {
BaseEntity,
Entity,
PrimaryGeneratedColumn,
Column,
Unique,
} from 'typeorm';
import { hash } from 'bcrypt';
@Entity()
@Unique(['email'])
export class User extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column()
role: string;
@Column()
email: string;
@Column()
salt: string;
@Column()
password: string;
@Column({ default: false })
agreement: boolean;
@Column({ default: '' })
photoURL: string;
@Column({ default: '' })
username: string;
async validatePassword(password: string): Promise<boolean> {
const hashedPassword = await hash(password, this.salt);
return hashedPassword === this.password;
}
}
import { User } from 'src/users/user.entity';
import {
BaseEntity,
Column,
Entity,
JoinColumn,
OneToOne,
PrimaryGeneratedColumn,
} from 'typeorm';
@Entity()
export class Candidate extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@OneToOne(
type => User,
(user: User) => user.id,
)
@JoinColumn()
user: number; // Вот тут вся магия
@Column({ default: '' })
primaryCategory: string;
@Column({ default: '' })
experience: string;
@Column({ default: 0 })
salary: number;
@Column({ default: '' })
location: string;
@Column({ default: '' })
fullName: string;
/** It should be related with another table which get files from aws s3 */
@Column({ default: '' })
profilePhoto: string;
}
users
and in candidate
and in recruiter
(photoURL, username )?Answer the question
In order to leave comments, you need to log in
Believe me, everyone will have their own criteria for "correctness".
Should I make a OneToOne relationship with other similar fields
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question