150 assignable roles should be its own table to prevent limitations on length (#158)

* Add entity

* Update role config command to use new entity

* Update role command to use new entity

* Remove legacy code from config command
This commit is contained in:
Vylpes 2022-05-14 14:59:32 +01:00 committed by GitHub
parent 6522bee37b
commit 514be692fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 126 additions and 37 deletions

44
src/entity/Role.ts Normal file
View file

@ -0,0 +1,44 @@
import { Column, Entity, EntityTarget, getConnection, ManyToOne } from "typeorm";
import BaseEntity from "../contracts/BaseEntity"
import Server from "./Server";
@Entity()
export default class Role extends BaseEntity {
constructor(roleId: string) {
super();
this.RoleId = roleId;
}
@Column()
RoleId: string;
@ManyToOne(() => Server, x => x.Roles)
Server: Server;
public static async FetchOneByRoleId(roleId: string, relations?: string[]): Promise<Role | undefined> {
const connection = getConnection();
const repository = connection.getRepository(Role);
const single = await repository.findOne({ RoleId: roleId}, { relations: relations || [] });
return single;
}
public static async FetchAllByServerId(serverId: string): Promise<Role[]> {
const connection = getConnection();
const repository = connection.getRepository(Server);
const all = await repository.findOne(serverId, { relations: [
"Roles",
]});
if (!all) {
return [];
}
return all.Roles;
}
}

View file

@ -1,5 +1,6 @@
import { Column, Entity, getConnection, OneToMany } from "typeorm";
import { Entity, OneToMany } from "typeorm";
import BaseEntity from "../contracts/BaseEntity";
import Role from "./Role";
import Setting from "./Setting";
@Entity()
@ -13,7 +14,14 @@ export default class Server extends BaseEntity {
@OneToMany(() => Setting, x => x.Server)
Settings: Setting[];
@OneToMany(() => Role, x => x.Server)
Roles: Role[];
public AddSettingToServer(setting: Setting) {
this.Settings.push(setting);
}
public AddRoleToServer(role: Role) {
this.Roles.push(role);
}
}