'How to insert id using typeorm mongodb
I am trying to insert the id of a record using typeorm mongodb.
I have created an Entity class for User, then I created a service for saving user into the database.
@Entity()
export class User {
@ObjectIdColumn()
@Transform(({ value }) => value.toHexString())
id: ObjectID;
@Column()
firstname: string;
@Column()
lastname: string;
@Column({ unique: true })
email: string;
@Column()
@CreateDateColumn()
createdAt: Date;
@Column()
@UpdateDateColumn()
updatedAt: Date;
constructor(user?: Partial<User>) {
Object.assign(this, user);
}
}
@Injectable()
export class UsersService implements OnModuleInit {
constructor(
@InjectRepository(User)
private readonly usersRepository: Repository<User>
) { }
async onModuleInit() {
console.log(await this.usersRepository.save(new User({
"id": "123456789012",
"lastname": "Admin",
"email": "[email protected]",
"password": "password"
})));
}
}
But, the log shows different id
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
