Add entity

This commit is contained in:
Ethan Lane 2022-05-12 18:28:47 +01:00
parent 6522bee37b
commit a44c6424e3
Signed by: Vylpes
GPG key ID: EED233CC06D12504
4 changed files with 43 additions and 2 deletions

View file

@ -7,7 +7,9 @@
"scripts": {
"build": "tsc",
"start": "node ./dist/vylbot",
"test": "jest"
"test": "jest",
"db:up": "typeorm migration:run",
"db:down": "typeorm migration:revert"
},
"repository": {
"type": "git",

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

@ -0,0 +1,18 @@
import { Column, Entity, 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;
}

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);
}
}

View file

@ -0,0 +1,13 @@
import { MigrationInterface, QueryRunner } from "typeorm"
export class migration1652375907691 implements MigrationInterface {
public async up(queryRunner: QueryRunner): Promise<void> {
queryRunner.query(`CREATE TABLE role (Id varchar(255), WhenCreated datetime, WhenUpdated datetime, RoleId varchar(255), serverId varchar(255), PRIMARY KEY (Id), FOREIGN KEY (serverId) REFERENCES server(Id))`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
queryRunner.query(`DROP TABLE role`);
}
}