When user registers, I want to him to send his account name, so I will have "Accounts" table with reference to the user entity. I'm using Nest.js.
I'm looking for alternative to the following logic in my users.service.ts Register method:
- Find Account by Name
- If Account not found, Create It
- Create User with the Account found above
Here is my Account Entity:
@Entity()
export class Account extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ unique: true })
name: string;
@Column()
@CreateDateColumn()
createdAt: Date;
}
And my User Entity:
@Entity()
export class User extends BaseEntity {
@PrimaryGeneratedColumn('uuid')
id: string;
@Column({ unique: true })
email: string;
@Column()
password: string;
@ManyToOne(() => Account, (Account) => Account.name, { cascade:true })
@JoinColumn({name: 'name'})
account: Account;
}
My CreateUserDTO:
export class CreateUserDto {
@IsEmail()
email: string;
@IsNotEmpty()
password: string;
@IsNotEmpty()
account: string;
}
And this is the error when I try to do User.create(dto):
Type 'string' is not assignable to type 'Account | DeepPartial<Account>'.
In addition, for some reason, the User.create(dto) returns array of users and not single user, and I don't understand why.