Answering this seemingly simple question has proven to be quite challenging.
In my backend application using Express and Typescript, I am working on implementing a password reset feature. The database I am using is Postgres with Typeorm for data manipulation. Within my database, I have an entity called User that consists of two columns:
@Column({
unique: true,
nullable: true,
})
resetPasswordToken!: string;
@Column({ nullable: true, type: 'timestamp with time zone' })
resetPasswordExpiresAt!: Date;
When a user requests a password reset token, both the resetPasswordToken and resetPasswordExpiresAt fields are populated. Upon receiving the token via email, the user can proceed to reset their password. After the password has been successfully reset, I intend to clear these fields by setting them to null:
user.resetPasswordToken = null;
user.resetPasswordExpiresAt = null;
user.save()
However, attempting to assign the value of null triggers errors in Typescript:
Type 'null' is not assignable to type 'string'.
and
Type 'null' is not assignable to type 'Date'.
To resolve these errors, I modified the entity column definitions to allow for null values like so:
resetPasswordToken!: string | null;
...
resetPasswordExpiresAt!: Date | null;
But upon launching my Express application, I encountered a new issue when Typeorm attempted to connect to the database:
Data type "Object" in "User.resetPasswordToken" is not supported by "postgres" database.
How can I successfully set these fields to null without encountering errors?