1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
|
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { BaseEntity, BeforeInsert, BeforeUpdate, Column, ColumnOptions, FindOptionsWhere, ObjectIdColumn, PrimaryColumn } from "typeorm";
import { Snowflake, getDatabase } from "../util";
import { OrmUtils } from "../imports";
import { annotationsKey } from "../util/Decorators";
export class BaseClassWithoutId extends BaseEntity {
private get construct() {
return this.constructor;
}
// stores custom annotations we may stick on the properties
[annotationsKey]: { [p: string]: string[] };
// retrieves the custom annotations as its not super straight forward
get_annotations() {
return Object.getPrototypeOf(this)[annotationsKey];
}
// Loops through all the keys and compares it to annotations. If the RemoveEmpty is there it sets the value to undefined if null
clean_data() {
const annotations = this.get_annotations();
if (annotations == undefined || annotations.length > 0)
//prevent errors if there are no annotations on an object
return;
for (const key in this) {
if (
key in this && // This object has this property, should never fail but better to be safe
key in annotations && // If this property has an annotation
annotations[key].indexOf("JsonRemoveEmpty") > -1 && // if one of the annotations is JsonRemoveEmpty
(this[key] == null || // If this property is null
(typeof this[key] == "object" && Object.keys(this[key]).length == 0))
) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
this[key] = undefined; // set to undefined to remove
}
if (
key in this && // This object has this property, should never fail but better to be safe
key in annotations && // If this property has an annotation
annotations[key].indexOf("JsonNumber") > -1 && // if one of the annotations is JsonRemoveEmpty
typeof this[key] == "string" // and its a String
) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
this[key] = Number(this[key]); // convert string back to number
}
}
return this;
}
private get metadata() {
return getDatabase()?.getMetadata(this.construct);
}
assign(props: object) {
OrmUtils.mergeDeep(this, props);
return this;
}
// TODO: fix eslint
// eslint-disable-next-line @typescript-eslint/no-explicit-any
toJSON(): any {
this.clean_data();
return Object.fromEntries(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
this.metadata!.columns // @ts-ignore
.map((x) => [x.propertyName, this[x.propertyName]])
.concat(
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this.metadata.relations.map((x) => [
x.propertyName,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
this[x.propertyName],
]),
),
);
}
static increment<T extends BaseClass>(conditions: FindOptionsWhere<T>, propertyPath: string, value: number | string) {
const repository = this.getRepository();
return repository.increment(conditions, propertyPath, value);
}
static decrement<T extends BaseClass>(conditions: FindOptionsWhere<T>, propertyPath: string, value: number | string) {
const repository = this.getRepository();
return repository.decrement(conditions, propertyPath, value);
}
public async insert(): Promise<this> {
await getDatabase()!.getRepository(this.construct).insert(this);
return this;
}
}
export const PrimaryIdColumn = process.env.DATABASE?.startsWith("mongodb") ? ObjectIdColumn : PrimaryColumn;
export class BaseClass extends BaseClassWithoutId {
@PrimaryIdColumn()
id: string = Snowflake.generate();
@BeforeUpdate()
@BeforeInsert()
_do_validate() {
if (!this.id) this.id = Snowflake.generate();
}
}
export const ArrayColumn = (opts: ColumnOptions) => (process.env.DATABASE?.startsWith("postgres") ? Column({ ...opts, array: true }) : Column({ ...opts, type: "simple-array" }));
|