Skip to content
Snippets Groups Projects

Fix validate

Files

+ 6
4
@@ -2,8 +2,6 @@ import {
@@ -2,8 +2,6 @@ import {
BaseEntity,
BaseEntity,
CreateDateColumn,
CreateDateColumn,
UpdateDateColumn,
UpdateDateColumn,
BeforeInsert,
BeforeUpdate,
SaveOptions,
SaveOptions,
} from "typeorm";
} from "typeorm";
import { validateOrReject, IsDate, IsOptional } from "class-validator";
import { validateOrReject, IsDate, IsOptional } from "class-validator";
@@ -36,14 +34,18 @@ export default abstract class BaseModel extends BaseEntity {
@@ -36,14 +34,18 @@ export default abstract class BaseModel extends BaseEntity {
}
}
// validateOrReject to be run before saving/updating by TypeORM
// validateOrReject to be run before saving/updating by TypeORM
@BeforeInsert()
// @BeforeInsert and @BeforeUpdate have a bug and lock up the database connection
@BeforeUpdate()
// see also: https://github.com/typeorm/typeorm/issues/6779
 
// @BeforeInsert()
 
// @BeforeUpdate()
validateOrReject(): Promise<void> {
validateOrReject(): Promise<void> {
return validateOrReject(this);
return validateOrReject(this);
}
}
// reloads the entry after saving to the database
// reloads the entry after saving to the database
async save(options?: SaveOptions): Promise<this> {
async save(options?: SaveOptions): Promise<this> {
 
// run validateOrReject here as it blocks when run with @BeforeInsert and @BeforeUpdate
 
await this.validateOrReject();
await super.save(options);
await super.save(options);
await this.reload();
await this.reload();
return this;
return this;
Loading