summary refs log tree commit diff
path: root/util/src/entities
diff options
context:
space:
mode:
Diffstat (limited to 'util/src/entities')
-rw-r--r--util/src/entities/BaseClass.ts58
-rw-r--r--util/src/entities/Message.ts3
-rw-r--r--util/src/entities/RateLimit.ts3
-rw-r--r--util/src/entities/User.ts9
-rw-r--r--util/src/entities/Webhook.ts3
5 files changed, 9 insertions, 67 deletions
diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts
index beccf04b..43b69c76 100644
--- a/util/src/entities/BaseClass.ts
+++ b/util/src/entities/BaseClass.ts
@@ -1,19 +1,8 @@
 import "reflect-metadata";
-import {
-	BaseEntity,
-	BeforeInsert,
-	BeforeUpdate,
-	EntityMetadata,
-	FindConditions,
-	ObjectIdColumn,
-	PrimaryColumn,
-} from "typeorm";
+import { BaseEntity, EntityMetadata, FindConditions, ObjectIdColumn, PrimaryColumn } from "typeorm";
 import { Snowflake } from "../util/Snowflake";
 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 BaseClassWithoutId extends BaseEntity {
 	constructor(props?: any) {
 		super();
@@ -38,11 +27,12 @@ export class BaseClassWithoutId extends BaseEntity {
 				.concat(this.metadata.relations.map((x) => x.propertyName))
 		);
 		// will not include relational properties
+		console.log(properties);
 
 		for (const key in props) {
 			if (!properties.has(key)) continue;
 			// @ts-ignore
-			const setter = this[`set${key.capitalize()}`];
+			const setter = this[`set${key.capitalize()}`]; // use setter function if it exists
 
 			if (setter) {
 				setter.call(this, props[key]);
@@ -53,12 +43,6 @@ export class BaseClassWithoutId extends BaseEntity {
 		}
 	}
 
-	@BeforeUpdate()
-	@BeforeInsert()
-	validate() {
-		return this;
-	}
-
 	toJSON(): any {
 		return Object.fromEntries(
 			this.metadata.columns // @ts-ignore
@@ -76,42 +60,6 @@ export class BaseClassWithoutId extends BaseEntity {
 		const repository = this.getRepository();
 		return repository.decrement(conditions, propertyPath, value);
 	}
-
-	// static async delete<T>(criteria: FindConditions<T>, options?: RemoveOptions) {
-	// 	if (!criteria) throw new Error("You need to specify delete criteria");
-
-	// 	const repository = this.getRepository();
-	// 	const promises = repository.metadata.relations.map(async (x) => {
-	// 		if (x.orphanedRowAction !== "delete") return;
-
-	// 		const foreignKey =
-	// 			x.foreignKeys.find((key) => key.entityMetadata === repository.metadata) ||
-	// 			x.inverseRelation?.foreignKeys[0]; // find foreign key for this entity
-	// 		if (!foreignKey) {
-	// 			throw new Error(
-	// 				`Foreign key not found for entity ${repository.metadata.name} in relation ${x.propertyName}`
-	// 			);
-	// 		}
-	// 		const id = (criteria as any)[foreignKey.referencedColumnNames[0]];
-	// 		if (!id) throw new Error("id missing in criteria options " + foreignKey.referencedColumnNames);
-
-	// 		if (x.relationType === "many-to-many") {
-	// 			return getConnection()
-	// 				.createQueryBuilder()
-	// 				.relation(this, x.propertyName)
-	// 				.of(id)
-	// 				.remove({ [foreignKey.columnNames[0]]: id });
-	// 		} else if (
-	// 			x.relationType === "one-to-one" ||
-	// 			x.relationType === "many-to-one" ||
-	// 			x.relationType === "one-to-many"
-	// 		) {
-	// 			return (x.inverseEntityMetadata.target as any).delete({ [foreignKey.columnNames[0]]: id });
-	// 		}
-	// 	});
-	// 	await Promise.all(promises);
-	// 	return super.delete(criteria, options);
-	// }
 }
 
 export const PrimaryIdColumn = process.env.DATABASE?.startsWith("mongodb") ? ObjectIdColumn : PrimaryColumn;
diff --git a/util/src/entities/Message.ts b/util/src/entities/Message.ts
index 04c3c7aa..c7f7a1d1 100644
--- a/util/src/entities/Message.ts
+++ b/util/src/entities/Message.ts
@@ -46,9 +46,6 @@ export enum MessageType {
 
 @Entity("messages")
 export class Message extends BaseClass {
-	@Column()
-	id: string;
-
 	@Column({ nullable: true })
 	@RelationId((message: Message) => message.channel)
 	channel_id: string;
diff --git a/util/src/entities/RateLimit.ts b/util/src/entities/RateLimit.ts
index fa9c32c1..f5916f6b 100644
--- a/util/src/entities/RateLimit.ts
+++ b/util/src/entities/RateLimit.ts
@@ -3,9 +3,6 @@ import { BaseClass } from "./BaseClass";
 
 @Entity("rate_limits")
 export class RateLimit extends BaseClass {
-	@Column()
-	id: "global" | "error" | string; // channel_239842397 | guild_238927349823 | webhook_238923423498
-
 	@Column() // no relation as it also
 	executor_id: string;
 
diff --git a/util/src/entities/User.ts b/util/src/entities/User.ts
index 97564af3..662ab031 100644
--- a/util/src/entities/User.ts
+++ b/util/src/entities/User.ts
@@ -198,7 +198,7 @@ export class User extends BaseClass {
 		// randomly generates a discriminator between 1 and 9999 and checks max five times if it already exists
 		// if it all five times already exists, abort with USERNAME_TOO_MANY_USERS error
 		// else just continue
-		// TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the mongodb database?
+		// TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the database?
 		for (let tries = 0; tries < 5; tries++) {
 			discriminator = Math.randomIntBetween(1, 9999).toString().padStart(4, "0");
 			exists = await User.findOne({ where: { discriminator, username: username }, select: ["id"] });
@@ -219,7 +219,7 @@ export class User extends BaseClass {
 		// if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false
 		const language = req.language === "en" ? "en-US" : req.language || "en-US";
 
-		const user = await new User({
+		const user = new User({
 			created_at: new Date(),
 			username: username,
 			discriminator,
@@ -246,7 +246,10 @@ export class User extends BaseClass {
 			},
 			settings: { ...defaultSettings, locale: language },
 			fingerprints: [],
-		}).save();
+		});
+
+		console.log(user);
+		await user.save();
 
 		if (Config.get().guild.autoJoin.enabled) {
 			for (const guild of Config.get().guild.autoJoin.guilds || []) {
diff --git a/util/src/entities/Webhook.ts b/util/src/entities/Webhook.ts
index 8382435f..9d8609ae 100644
--- a/util/src/entities/Webhook.ts
+++ b/util/src/entities/Webhook.ts
@@ -12,9 +12,6 @@ export enum WebhookType {
 
 @Entity("webhooks")
 export class Webhook extends BaseClass {
-	@Column()
-	id: string;
-
 	@Column({ type: "simple-enum", enum: WebhookType })
 	type: WebhookType;