diff options
Diffstat (limited to 'util/src/models/BaseClass.ts')
-rw-r--r-- | util/src/models/BaseClass.ts | 24 |
1 files changed, 13 insertions, 11 deletions
diff --git a/util/src/models/BaseClass.ts b/util/src/models/BaseClass.ts index 78cd329c..d4f635f6 100644 --- a/util/src/models/BaseClass.ts +++ b/util/src/models/BaseClass.ts @@ -1,22 +1,24 @@ import "reflect-metadata"; -import { BaseEntity, Column } from "typeorm"; +import { BaseEntity, BeforeInsert, BeforeUpdate, Column, PrimaryGeneratedColumn } from "typeorm"; +import { Snowflake } from "../util/Snowflake"; +import { IsString, validateOrReject } from "class-validator"; export class BaseClass extends BaseEntity { + @PrimaryGeneratedColumn() @Column() - id?: string; + @IsString() + id: string; - constructor(props?: any) { + constructor(props?: any, opts: { id?: string } = {}) { super(); - BaseClass.assign(props, this, "body."); + this.id = opts.id || Snowflake.generate(); + Object.defineProperties(this, props); } - private static assign(props: any, object: any, path?: string): any { - const expectedType = Reflect.getMetadata("design:type", object, props); - console.log(expectedType, object, props, path, typeof object); - - if (typeof object !== typeof props) throw new Error(`Property at ${path} must be`); - if (typeof object === "object") - return Object.keys(object).map((key) => BaseClass.assign(props[key], object[key], `${path}.${key}`)); + @BeforeUpdate() + @BeforeInsert() + async validate() { + await validateOrReject(this, {}); } } |