I am using in my NestJs application type-orm. I have 2 entities:
- User
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ nullable: true })
gender: string;
@OneToMany(() => Address, (address) => address.user)
address: Address;
}
- Address
@Entity()
export class Address {
@PrimaryGeneratedColumn()
id: number;
@ManyToOne(() => User, (user) => user.address)
user: User;
}
The idea is next: Each user can have multiple addresses, this is why i used OneToMany.
My provider looks like this:
async add(): Promise < User > {
try {
const newPost = this.usersRepository.create({
gender: 'm',
});
return await this.usersRepository.save(newPost);
} catch (err) {
return err;
}
}
It works and in db, in the table user i can add gender: 'm'.
Issue: How to add in add() function and the addresses, because now i can add just the user.
Question: How to change the add() function to be able to add and addresses?