summary refs log tree commit diff
path: root/util/src/entities/BaseClass.ts
blob: c872e7f181c8c23295d04907826b11b1d26e3403 (plain) (blame)
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
import "reflect-metadata";
import { BaseEntity, EntityMetadata, ObjectIdColumn, PrimaryColumn, FindOptionsWhere, Generated, SaveOptions } from "typeorm";
import { Snowflake } from "../util/Snowflake";

export class BaseClassWithoutId extends BaseEntity {
	constructor() {
		super();
	}
}

export const PrimaryIdColumn = process.env.DATABASE?.startsWith("mongodb") ? ObjectIdColumn : PrimaryColumn;

export class BaseClass extends BaseClassWithoutId {
	@PrimaryIdColumn()
	id: string;

	constructor() {
		super();
		if (!this.id) this.id = Snowflake.generate();
	}

	save(options?: SaveOptions | undefined): Promise<this> {
		if (!this.id) this.id = Snowflake.generate();
		return super.save(options);
	}
}