summary refs log tree commit diff
path: root/util/src/entities/BaseClass.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-31 18:10:36 +0200
committerGitHub <noreply@github.com>2021-08-31 18:10:36 +0200
commitef5df63501fa6ffee170150816878a14cd8a6cdc (patch)
tree1c9708b20b8e49d0cae48cba1030558dbbb07db8 /util/src/entities/BaseClass.ts
parentCreated list of all possible api errors and made them throwable in routes code (diff)
parentMerge branch 'typeorm' of https://github.com/fosscord/fosscord-api into typeorm (diff)
downloadserver-ef5df63501fa6ffee170150816878a14cd8a6cdc.tar.xz
Merge branch 'typeorm' into typeorm
Diffstat (limited to 'util/src/entities/BaseClass.ts')
-rw-r--r--util/src/entities/BaseClass.ts34
1 files changed, 15 insertions, 19 deletions
diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts

index 63ce5836..0856ccd1 100644 --- a/util/src/entities/BaseClass.ts +++ b/util/src/entities/BaseClass.ts
@@ -1,13 +1,5 @@ import "reflect-metadata"; -import { - BaseEntity, - BeforeInsert, - BeforeUpdate, - EntityMetadata, - FindConditions, - FindManyOptions, - PrimaryColumn, -} from "typeorm"; +import { BaseEntity, BeforeInsert, BeforeUpdate, EntityMetadata, FindConditions, PrimaryColumn } from "typeorm"; import { Snowflake } from "../util/Snowflake"; import "missing-native-js-functions"; @@ -16,13 +8,12 @@ import "missing-native-js-functions"; export class BaseClass extends BaseEntity { @PrimaryColumn() - id: string; + id: string = Snowflake.generate(); // @ts-ignore - constructor(public props?: any, public opts: { id?: string } = {}) { + constructor(public props?: any) { super(); - - this.id = this.opts.id || Snowflake.generate(); + this.assign(props); } get construct(): any { @@ -35,13 +26,15 @@ export class BaseClass extends BaseEntity { assign(props: any) { if (!props || typeof props !== "object") return; - - delete props.id; delete props.opts; delete props.props; - const properties = new Set(this.metadata.columns.map((x: any) => x.propertyName)); - // will not include relational properties (e.g. @RelationId @ManyToMany) + const properties = new Set( + this.metadata.columns + .map((x: any) => x.propertyName) + .concat(this.metadata.relations.map((x) => x.propertyName)) + ); + // will not include relational properties for (const key in props) { if (!properties.has(key)) continue; @@ -65,8 +58,11 @@ export class BaseClass extends BaseEntity { } toJSON(): any { - // @ts-ignore - return Object.fromEntries(this.metadata.columns.map((x) => [x.propertyName, this[x.propertyName]])); + return Object.fromEntries( + this.metadata.columns // @ts-ignore + .map((x) => [x.propertyName, this[x.propertyName]]) // @ts-ignore + .concat(this.metadata.relations.map((x) => [x.propertyName, this[x.propertyName]])) + ); } static increment<T extends BaseClass>(conditions: FindConditions<T>, propertyPath: string, value: number | string) {