summary refs log tree commit diff
path: root/src/util/entities/BaseClass.ts
blob: aecc2465f6afa489085d4a6761fc530d7753c107 (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, ObjectIdColumn, PrimaryColumn, 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);
	}
}