I am using sequelize-typescript in this app and I have a model that currently is trying to hash a password before saving it
account.model.ts
@Table({
freezeTableName: true,
tableName: 'accounts',
})
export default class Account extends Model {
// ... other fields
@Length({ min: 0, max: 255 })
@Column({
comment:
'optional for social login users and compulsory for users signing up with an email password combination',
})
password: string;
// ...more fields
@BeforeUpdate
@BeforeCreate
static async hashPassword(instance: Account) {
instance.password = await buildHash(instance.password);
}
}
My buildHash function is asynchronous in nature and looks like this
import hasher from 'argon2';
const buildHash = (item) => hasher.hash(item);
I am getting an accountId not exists error when I try setting users on the account object
account.service.ts
static async create(createBody: {
email: string;
authenticationTypeId: string;
emailVerified?: boolean;
isPrimary?: boolean;
password?: string;
pictureUrl?: string;
socialAccountId?: string;
username?: string;
users?: User[];
}) {
console.log(createBody);
const createdAccount: Account = new Account(createBody);
console.log(createdAccount);
if (createBody.users) createdAccount.$set('users', createBody.users);
return createdAccount.save();
}
Very specifically it has an error at this line saying accountId does not exist
if (createBody.users) createdAccount.$set('users', createBody.users);
If I replace my hasher with a sync function from bcrypt, it works perfectly
import hasher from 'bcrypt';
const buildHash = (item) => hasher.hashSync(item, 12);
The model hook changed appropriately when using bcrypt
@BeforeUpdate
@BeforeCreate
static hashPassword(instance: Account) {
instance.password = buildHash(instance.password);
}
Unfortunately argon2 doesnt have a SYNC function and I dont want to change my hashing algorithm back to bcrypt
Can someone kindly tell me how I can wait for the new Account to be created before attempting to update the many to many relationship?