From 3eac3a05ec822259a2aaba8dc661a8260d841eb8 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 3 Sep 2021 03:38:19 +0200 Subject: :art: update entities --- util/src/entities/BaseClass.ts | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'util/src/entities/BaseClass.ts') diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts index 403a6fc6..9b2ce058 100644 --- a/util/src/entities/BaseClass.ts +++ b/util/src/entities/BaseClass.ts @@ -6,11 +6,7 @@ import "missing-native-js-functions"; // TODO use class-validator https://typeorm.io/#/validation with class annotators (isPhone/isEmail) combined with types from typescript-json-schema // btw. we don't use class-validator for everything, because we need to explicitly set the type instead of deriving it from typescript also it doesn't easily support nested objects -export class BaseClass extends BaseEntity { - @PrimaryColumn() - id: string; - - // @ts-ignore +export class BaseClassWithoutId extends BaseEntity { constructor(private props?: any) { super(); this.assign(props); @@ -24,8 +20,7 @@ export class BaseClass extends BaseEntity { return this.construct.getRepository().metadata as EntityMetadata; } - assign(props: any) { - if (!props || typeof props !== "object") return; + assign(props: any = {}) { delete props.opts; delete props.props; @@ -48,8 +43,6 @@ export class BaseClass extends BaseEntity { this[key] = props[key]; } } - - if (!this.id) this.id = Snowflake.generate(); } @BeforeUpdate() @@ -77,3 +70,14 @@ export class BaseClass extends BaseEntity { return repository.decrement(conditions, propertyPath, value); } } + +export class BaseClass extends BaseClassWithoutId { + @PrimaryColumn() + id: string; + + assign(props: any = {}) { + super.assign(props); + if (!this.id) this.id = Snowflake.generate(); + return this; + } +} -- cgit 1.5.1