summary refs log tree commit diff
path: root/util
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-20 17:11:22 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-20 17:11:22 +0200
commite22ea776250381ab375de13a4e7eab9c16443935 (patch)
treea71887825b77264fd32c13686173810882240733 /util
parentMerge branch 'master' into auto-delete-relations (diff)
downloadserver-e22ea776250381ab375de13a4e7eab9c16443935.tar.xz
:construction: auto delete relations
Diffstat (limited to 'util')
-rw-r--r--util/package-lock.json98
-rw-r--r--util/src/entities/BaseClass.ts49
-rw-r--r--util/src/entities/Channel.ts12
-rw-r--r--util/src/util/Database.ts2
4 files changed, 98 insertions, 63 deletions
diff --git a/util/package-lock.json b/util/package-lock.json
index e5a62d0f..f4129614 100644
--- a/util/package-lock.json
+++ b/util/package-lock.json
@@ -226,19 +226,19 @@
 			}
 		},
 		"node_modules/@babel/helper-module-transforms": {
-			"version": "7.15.4",
-			"resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.15.4.tgz",
-			"integrity": "sha512-9fHHSGE9zTC++KuXLZcB5FKgvlV83Ox+NLUmQTawovwlJ85+QMhk1CnVk406CQVj97LaWod6KVjl2Sfgw9Aktw==",
+			"version": "7.15.7",
+			"resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.15.7.tgz",
+			"integrity": "sha512-ZNqjjQG/AuFfekFTY+7nY4RgBSklgTu970c7Rj3m/JOhIu5KPBUuTA9AY6zaKcUvk4g6EbDXdBnhi35FAssdSw==",
 			"dev": true,
 			"dependencies": {
 				"@babel/helper-module-imports": "^7.15.4",
 				"@babel/helper-replace-supers": "^7.15.4",
 				"@babel/helper-simple-access": "^7.15.4",
 				"@babel/helper-split-export-declaration": "^7.15.4",
-				"@babel/helper-validator-identifier": "^7.14.9",
+				"@babel/helper-validator-identifier": "^7.15.7",
 				"@babel/template": "^7.15.4",
 				"@babel/traverse": "^7.15.4",
-				"@babel/types": "^7.15.4"
+				"@babel/types": "^7.15.6"
 			},
 			"engines": {
 				"node": ">=6.9.0"
@@ -305,9 +305,9 @@
 			}
 		},
 		"node_modules/@babel/helper-validator-identifier": {
-			"version": "7.14.9",
-			"resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.14.9.tgz",
-			"integrity": "sha512-pQYxPY0UP6IHISRitNe8bsijHex4TWZXi2HwKVsjPiltzlhse2znVcm9Ace510VT1kxIHjGJCZZQBX2gJDbo0g==",
+			"version": "7.15.7",
+			"resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.15.7.tgz",
+			"integrity": "sha512-K4JvCtQqad9OY2+yTU8w+E82ywk/fe+ELNlt1G8z3bVGlZfn/hOcQQsUhGhW/N+tb3fxK800wLtKOE/aM0m72w==",
 			"dev": true,
 			"engines": {
 				"node": ">=6.9.0"
@@ -422,9 +422,9 @@
 			}
 		},
 		"node_modules/@babel/parser": {
-			"version": "7.15.6",
-			"resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.6.tgz",
-			"integrity": "sha512-S/TSCcsRuCkmpUuoWijua0Snt+f3ewU/8spLo+4AXJCZfT0bVCzLD5MuOKdrx0mlAptbKzn5AdgEIIKXxXkz9Q==",
+			"version": "7.15.7",
+			"resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.7.tgz",
+			"integrity": "sha512-rycZXvQ+xS9QyIcJ9HXeDWf1uxqlbVFAUq0Rq0dbc50Zb/+wUe/ehyfzGfm9KZZF0kBejYgxltBXocP+gKdL2g==",
 			"dev": true,
 			"bin": {
 				"parser": "bin/babel-parser.js"
@@ -1204,9 +1204,9 @@
 			}
 		},
 		"node_modules/@types/node": {
-			"version": "14.17.16",
-			"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.16.tgz",
-			"integrity": "sha512-WiFf2izl01P1CpeY8WqFAeKWwByMueBEkND38EcN8N68qb0aDG3oIS1P5MhAX5kUdr469qRyqsY/MjanLjsFbQ=="
+			"version": "14.17.17",
+			"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.17.tgz",
+			"integrity": "sha512-niAjcewgEYvSPCZm3OaM9y6YQrL2SEPH9PymtE6fuZAvFiP6ereCcvApGl2jKTq7copTIguX3PBvfP08LN4LvQ=="
 		},
 		"node_modules/@types/node-fetch": {
 			"version": "2.5.12",
@@ -1941,9 +1941,9 @@
 			}
 		},
 		"node_modules/caniuse-lite": {
-			"version": "1.0.30001257",
-			"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001257.tgz",
-			"integrity": "sha512-JN49KplOgHSXpIsVSF+LUyhD8PUp6xPpAXeRrrcBh4KBeP7W864jHn6RvzJgDlrReyeVjMFJL3PLpPvKIxlIHA==",
+			"version": "1.0.30001258",
+			"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001258.tgz",
+			"integrity": "sha512-RBByOG6xWXUp0CR2/WU2amXz3stjKpSl5J1xU49F1n2OxD//uBZO4wCKUiG+QMGf7CHGfDDcqoKriomoGVxTeA==",
 			"dev": true,
 			"funding": {
 				"type": "opencollective",
@@ -2498,9 +2498,9 @@
 			"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
 		},
 		"node_modules/electron-to-chromium": {
-			"version": "1.3.840",
-			"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.840.tgz",
-			"integrity": "sha512-yRoUmTLDJnkIJx23xLY7GbSvnmDCq++NSuxHDQ0jiyDJ9YZBUGJcrdUqm+ZwZFzMbCciVzfem2N2AWiHJcWlbw==",
+			"version": "1.3.843",
+			"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.843.tgz",
+			"integrity": "sha512-OWEwAbzaVd1Lk9MohVw8LxMXFlnYd9oYTYxfX8KS++kLLjDfbovLOcEEXwRhG612dqGQ6+44SZvim0GXuBRiKg==",
 			"dev": true
 		},
 		"node_modules/emittery": {
@@ -6249,13 +6249,12 @@
 			}
 		},
 		"node_modules/stack-utils": {
-			"version": "2.0.4",
-			"resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.4.tgz",
-			"integrity": "sha512-ERg+H//lSSYlZhBIUu+wJnqg30AbyBbpZlIhcshpn7BNzpoRODZgfyr9J+8ERf3ooC6af3u7Lcl01nleau7MrA==",
+			"version": "2.0.5",
+			"resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.5.tgz",
+			"integrity": "sha512-xrQcmYhOsn/1kX+Vraq+7j4oE2j/6BFscZ0etmYg81xuM8Gq0022Pxb8+IqgOFUIaxHs0KaSb7T1+OegiNrNFA==",
 			"dev": true,
 			"dependencies": {
-				"escape-string-regexp": "^2.0.0",
-				"source-map-support": "^0.5.20"
+				"escape-string-regexp": "^2.0.0"
 			},
 			"engines": {
 				"node": ">=10"
@@ -7535,19 +7534,19 @@
 			}
 		},
 		"@babel/helper-module-transforms": {
-			"version": "7.15.4",
-			"resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.15.4.tgz",
-			"integrity": "sha512-9fHHSGE9zTC++KuXLZcB5FKgvlV83Ox+NLUmQTawovwlJ85+QMhk1CnVk406CQVj97LaWod6KVjl2Sfgw9Aktw==",
+			"version": "7.15.7",
+			"resolved": "https://registry.npmjs.org/@babel/helper-module-transforms/-/helper-module-transforms-7.15.7.tgz",
+			"integrity": "sha512-ZNqjjQG/AuFfekFTY+7nY4RgBSklgTu970c7Rj3m/JOhIu5KPBUuTA9AY6zaKcUvk4g6EbDXdBnhi35FAssdSw==",
 			"dev": true,
 			"requires": {
 				"@babel/helper-module-imports": "^7.15.4",
 				"@babel/helper-replace-supers": "^7.15.4",
 				"@babel/helper-simple-access": "^7.15.4",
 				"@babel/helper-split-export-declaration": "^7.15.4",
-				"@babel/helper-validator-identifier": "^7.14.9",
+				"@babel/helper-validator-identifier": "^7.15.7",
 				"@babel/template": "^7.15.4",
 				"@babel/traverse": "^7.15.4",
-				"@babel/types": "^7.15.4"
+				"@babel/types": "^7.15.6"
 			}
 		},
 		"@babel/helper-optimise-call-expression": {
@@ -7596,9 +7595,9 @@
 			}
 		},
 		"@babel/helper-validator-identifier": {
-			"version": "7.14.9",
-			"resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.14.9.tgz",
-			"integrity": "sha512-pQYxPY0UP6IHISRitNe8bsijHex4TWZXi2HwKVsjPiltzlhse2znVcm9Ace510VT1kxIHjGJCZZQBX2gJDbo0g==",
+			"version": "7.15.7",
+			"resolved": "https://registry.npmjs.org/@babel/helper-validator-identifier/-/helper-validator-identifier-7.15.7.tgz",
+			"integrity": "sha512-K4JvCtQqad9OY2+yTU8w+E82ywk/fe+ELNlt1G8z3bVGlZfn/hOcQQsUhGhW/N+tb3fxK800wLtKOE/aM0m72w==",
 			"dev": true
 		},
 		"@babel/helper-validator-option": {
@@ -7688,9 +7687,9 @@
 			}
 		},
 		"@babel/parser": {
-			"version": "7.15.6",
-			"resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.6.tgz",
-			"integrity": "sha512-S/TSCcsRuCkmpUuoWijua0Snt+f3ewU/8spLo+4AXJCZfT0bVCzLD5MuOKdrx0mlAptbKzn5AdgEIIKXxXkz9Q==",
+			"version": "7.15.7",
+			"resolved": "https://registry.npmjs.org/@babel/parser/-/parser-7.15.7.tgz",
+			"integrity": "sha512-rycZXvQ+xS9QyIcJ9HXeDWf1uxqlbVFAUq0Rq0dbc50Zb/+wUe/ehyfzGfm9KZZF0kBejYgxltBXocP+gKdL2g==",
 			"dev": true
 		},
 		"@babel/plugin-syntax-async-generators": {
@@ -8336,9 +8335,9 @@
 			}
 		},
 		"@types/node": {
-			"version": "14.17.16",
-			"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.16.tgz",
-			"integrity": "sha512-WiFf2izl01P1CpeY8WqFAeKWwByMueBEkND38EcN8N68qb0aDG3oIS1P5MhAX5kUdr469qRyqsY/MjanLjsFbQ=="
+			"version": "14.17.17",
+			"resolved": "https://registry.npmjs.org/@types/node/-/node-14.17.17.tgz",
+			"integrity": "sha512-niAjcewgEYvSPCZm3OaM9y6YQrL2SEPH9PymtE6fuZAvFiP6ereCcvApGl2jKTq7copTIguX3PBvfP08LN4LvQ=="
 		},
 		"@types/node-fetch": {
 			"version": "2.5.12",
@@ -8945,9 +8944,9 @@
 			"dev": true
 		},
 		"caniuse-lite": {
-			"version": "1.0.30001257",
-			"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001257.tgz",
-			"integrity": "sha512-JN49KplOgHSXpIsVSF+LUyhD8PUp6xPpAXeRrrcBh4KBeP7W864jHn6RvzJgDlrReyeVjMFJL3PLpPvKIxlIHA==",
+			"version": "1.0.30001258",
+			"resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001258.tgz",
+			"integrity": "sha512-RBByOG6xWXUp0CR2/WU2amXz3stjKpSl5J1xU49F1n2OxD//uBZO4wCKUiG+QMGf7CHGfDDcqoKriomoGVxTeA==",
 			"dev": true
 		},
 		"caseless": {
@@ -9405,9 +9404,9 @@
 			"integrity": "sha1-WQxhFWsK4vTwJVcyoViyZrxWsh0="
 		},
 		"electron-to-chromium": {
-			"version": "1.3.840",
-			"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.840.tgz",
-			"integrity": "sha512-yRoUmTLDJnkIJx23xLY7GbSvnmDCq++NSuxHDQ0jiyDJ9YZBUGJcrdUqm+ZwZFzMbCciVzfem2N2AWiHJcWlbw==",
+			"version": "1.3.843",
+			"resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.3.843.tgz",
+			"integrity": "sha512-OWEwAbzaVd1Lk9MohVw8LxMXFlnYd9oYTYxfX8KS++kLLjDfbovLOcEEXwRhG612dqGQ6+44SZvim0GXuBRiKg==",
 			"dev": true
 		},
 		"emittery": {
@@ -12300,13 +12299,12 @@
 			}
 		},
 		"stack-utils": {
-			"version": "2.0.4",
-			"resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.4.tgz",
-			"integrity": "sha512-ERg+H//lSSYlZhBIUu+wJnqg30AbyBbpZlIhcshpn7BNzpoRODZgfyr9J+8ERf3ooC6af3u7Lcl01nleau7MrA==",
+			"version": "2.0.5",
+			"resolved": "https://registry.npmjs.org/stack-utils/-/stack-utils-2.0.5.tgz",
+			"integrity": "sha512-xrQcmYhOsn/1kX+Vraq+7j4oE2j/6BFscZ0etmYg81xuM8Gq0022Pxb8+IqgOFUIaxHs0KaSb7T1+OegiNrNFA==",
 			"dev": true,
 			"requires": {
-				"escape-string-regexp": "^2.0.0",
-				"source-map-support": "^0.5.20"
+				"escape-string-regexp": "^2.0.0"
 			}
 		},
 		"statuses": {
diff --git a/util/src/entities/BaseClass.ts b/util/src/entities/BaseClass.ts
index 9b2ce058..2a621f40 100644
--- a/util/src/entities/BaseClass.ts
+++ b/util/src/entities/BaseClass.ts
@@ -1,5 +1,14 @@
 import "reflect-metadata";
-import { BaseEntity, BeforeInsert, BeforeUpdate, EntityMetadata, FindConditions, PrimaryColumn } from "typeorm";
+import {
+	BaseEntity,
+	BeforeInsert,
+	BeforeUpdate,
+	EntityMetadata,
+	FindConditions,
+	getConnection,
+	PrimaryColumn,
+	RemoveOptions,
+} from "typeorm";
 import { Snowflake } from "../util/Snowflake";
 import "missing-native-js-functions";
 
@@ -69,6 +78,44 @@ 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((x) => {
+			if (x.orphanedRowAction !== "delete") return;
+			if (typeof x.type === "string") 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}`
+				);
+			}
+			console.log(foreignKey);
+			const id = (criteria as any)[foreignKey.referencedColumnNames[0]];
+			if (!id) throw new Error("id missing in criteria options");
+
+			if (x.relationType === "many-to-many" || x.relationType === "one-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") {
+				return getConnection()
+					.createQueryBuilder()
+					.from(x.inverseEntityMetadata, "user")
+					.of(id)
+					.remove({ [foreignKey.name]: id });
+			}
+		});
+		await Promise.all(promises);
+		return super.delete(criteria, options);
+	}
 }
 
 export class BaseClass extends BaseClassWithoutId {
diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts
index b1f75f33..74611eea 100644
--- a/util/src/entities/Channel.ts
+++ b/util/src/entities/Channel.ts
@@ -1,14 +1,4 @@
-import {
-	Column,
-	Entity,
-	FindConditions,
-	JoinColumn,
-	ManyToOne,
-	ObjectID,
-	OneToMany,
-	RelationId,
-	RemoveOptions,
-} from "typeorm";
+import { Column, Entity, JoinColumn, ManyToOne, OneToMany, RelationId } from "typeorm";
 import { BaseClass } from "./BaseClass";
 import { Guild } from "./Guild";
 import { PublicUserProjection, User } from "./User";
diff --git a/util/src/util/Database.ts b/util/src/util/Database.ts
index d3844cd9..c22d8abd 100644
--- a/util/src/util/Database.ts
+++ b/util/src/util/Database.ts
@@ -21,7 +21,7 @@ export function initDatabase() {
 		//
 		entities: Object.values(Models).filter((x) => x.constructor.name !== "Object"),
 		synchronize: true,
-		logging: false,
+		logging: true,
 		cache: {
 			duration: 1000 * 3, // cache all find queries for 3 seconds
 		},