From 9ebc8a8991410731399d910af7d733aaba35e895 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Mon, 15 May 2023 19:28:22 +0100 Subject: [PATCH] Switch to DataSource --- .dev.env | 8 ++++++++ .prod.env | 8 ++++++++ .stage.env | 16 ---------------- ormconfig.dev.json | 24 ------------------------ ormconfig.prod.json | 24 ------------------------ ormconfig.stage.json | 24 ------------------------ src/client/client.ts | 10 +++++----- src/contracts/BaseEntity.ts | 23 +++++++---------------- src/database/appDataSource.ts | 26 ++++++++++++++++++++++++++ src/entity/501231711271780357/Lobby.ts | 7 +++---- src/entity/Audit.ts | 11 ++++------- src/entity/IgnoredChannel.ts | 7 +++---- src/entity/Role.ts | 11 ++++------- src/entity/Setting.ts | 7 +++---- src/vylbot.ts | 6 ++++++ 15 files changed, 77 insertions(+), 135 deletions(-) delete mode 100644 .stage.env delete mode 100644 ormconfig.dev.json delete mode 100644 ormconfig.prod.json delete mode 100644 ormconfig.stage.json create mode 100644 src/database/appDataSource.ts diff --git a/.dev.env b/.dev.env index 964667c..bb123f5 100644 --- a/.dev.env +++ b/.dev.env @@ -15,3 +15,11 @@ BOT_CLIENTID=682942374040961060 ABOUT_FUNDING=https://ko-fi.com/vylpes ABOUT_REPO=https://gitea.vylpes.xyz/RabbitLabs/vylbot-app + +DB_HOST=127.0.0.1 +DB_PORT=3101 +DB_NAME=vylbot +DB_AUTH_USER=dev +DB_AUTH_PASS=dev +DB_SYNC=true +DB_LOGGING=true \ No newline at end of file diff --git a/.prod.env b/.prod.env index f0c75d5..25cac3d 100644 --- a/.prod.env +++ b/.prod.env @@ -14,3 +14,11 @@ BOT_CLIENTID=680083120896081954 ABOUT_FUNDING=https://ko-fi.com/vylpes ABOUT_REPO=https://gitea.vylpes.xyz/RabbitLabs/vylbot-app + +DB_HOST=127.0.0.1 +DB_PORT=3121 +DB_NAME=vylbot +DB_AUTH_USER=prod +DB_AUTH_PASS=prod +DB_SYNC=false +DB_LOGGING=false \ No newline at end of file diff --git a/.stage.env b/.stage.env deleted file mode 100644 index 9b90831..0000000 --- a/.stage.env +++ /dev/null @@ -1,16 +0,0 @@ -# Security Warning! Do not commit this file to any VCS! -# This is a local file to speed up development process, -# so you don't have to change your environment variables. -# -# This is not applied to `.env.template`! -# Template files must be committed to the VCS, but must not contain -# any secret values. - -BOT_TOKEN= -BOT_VER=3.1 -BOT_AUTHOR=Vylpes -BOT_OWNERID=147392775707426816 -BOT_CLIENTID=1016767908740857949 - -ABOUT_FUNDING=https://ko-fi.com/vylpes -ABOUT_REPO=https://gitea.vylpes.xyz/RabbitLabs/vylbot-app diff --git a/ormconfig.dev.json b/ormconfig.dev.json deleted file mode 100644 index f6f80c1..0000000 --- a/ormconfig.dev.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "type": "mysql", - "host": "localhost", - "port": 3101, - "username": "dev", - "password": "dev", - "database": "vylbot", - "synchronize": false, - "logging": false, - "entities": [ - "dist/entity/**/*.js" - ], - "migrations": [ - "dist/migration/**/*.js" - ], - "subscribers": [ - "dist/subscriber/**/*.js" - ], - "cli": { - "entitiesDir": "dist/entity", - "migrationsDir": "dist/migration", - "subscribersDir": "dist/subscriber" - } -} \ No newline at end of file diff --git a/ormconfig.prod.json b/ormconfig.prod.json deleted file mode 100644 index 300cfe2..0000000 --- a/ormconfig.prod.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "type": "mysql", - "host": "localhost", - "port": 3121, - "username": "prod", - "password": "prod", - "database": "vylbot", - "synchronize": false, - "logging": false, - "entities": [ - "dist/entity/**/*.js" - ], - "migrations": [ - "dist/migration/**/*.js" - ], - "subscribers": [ - "dist/subscriber/**/*.js" - ], - "cli": { - "entitiesDir": "dist/entity", - "migrationsDir": "dist/migration", - "subscribersDir": "dist/subscriber" - } -} \ No newline at end of file diff --git a/ormconfig.stage.json b/ormconfig.stage.json deleted file mode 100644 index a038f64..0000000 --- a/ormconfig.stage.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "type": "mysql", - "host": "localhost", - "port": 3111, - "username": "stage", - "password": "stage", - "database": "vylbot", - "synchronize": false, - "logging": false, - "entities": [ - "dist/entity/**/*.js" - ], - "migrations": [ - "dist/migration/**/*.js" - ], - "subscribers": [ - "dist/subscriber/**/*.js" - ], - "cli": { - "entitiesDir": "dist/entity", - "migrationsDir": "dist/migration", - "subscribersDir": "dist/subscriber" - } -} \ No newline at end of file diff --git a/src/client/client.ts b/src/client/client.ts index 75e06fb..f9ffc89 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -8,11 +8,12 @@ import { Command } from "../type/command"; import { Events } from "./events"; import { Util } from "./util"; +import AppDataSource from "../database/appDataSource"; export class CoreClient extends Client { private static _commandItems: ICommandItem[]; private static _eventItems: IEventItem[]; - + private _events: Events; private _util: Util; @@ -41,10 +42,9 @@ export class CoreClient extends Client { return; } - await createConnection().catch(e => { - console.error(e); - return; - }); + AppDataSource.initialize() + .then(() => console.log("Data Source Initialized")) + .catch((err) => console.error("Error Initialising Data Source", err)); super.on("interactionCreate", this._events.onInteractionCreate); super.on("ready", this._events.onReady); diff --git a/src/contracts/BaseEntity.ts b/src/contracts/BaseEntity.ts index bb4d550..dfed28c 100644 --- a/src/contracts/BaseEntity.ts +++ b/src/contracts/BaseEntity.ts @@ -1,5 +1,6 @@ -import { Column, DeepPartial, EntityTarget, getConnection, PrimaryColumn, ObjectLiteral, FindOptionsWhere } from "typeorm"; +import { Column, DeepPartial, EntityTarget, PrimaryColumn, ObjectLiteral, FindOptionsWhere } from "typeorm"; import { v4 } from "uuid"; +import AppDataSource from "../database/appDataSource"; export default class BaseEntity { constructor() { @@ -21,25 +22,19 @@ export default class BaseEntity { public async Save(target: EntityTarget, entity: DeepPartial): Promise { this.WhenUpdated = new Date(); - const connection = getConnection(); - - const repository = connection.getRepository(target); + const repository = AppDataSource.getRepository(target); await repository.save(entity); } public static async Remove(target: EntityTarget, entity: T): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(target); + const repository = AppDataSource.getRepository(target); await repository.remove(entity); } public static async FetchAll(target: EntityTarget, relations?: string[]): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(target); + const repository = AppDataSource.getRepository(target); const all = await repository.find({ relations: relations || [] }); @@ -47,9 +42,7 @@ export default class BaseEntity { } public static async FetchOneById(target: EntityTarget, id: string, relations?: string[]): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(target); + const repository = AppDataSource.getRepository(target); const single = await repository.findOne({ where: ({ Id: id } as FindOptionsWhere), relations: relations || {} }); @@ -57,9 +50,7 @@ export default class BaseEntity { } public static async Any(target: EntityTarget): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(target); + const repository = AppDataSource.getRepository(target); const any = await repository.find(); diff --git a/src/database/appDataSource.ts b/src/database/appDataSource.ts new file mode 100644 index 0000000..2f9fef2 --- /dev/null +++ b/src/database/appDataSource.ts @@ -0,0 +1,26 @@ +import { DataSource } from "typeorm"; +import * as dotenv from "dotenv"; + +dotenv.config(); + +const AppDataSource = new DataSource({ + type: "mysql", + host: process.env.DB_HOST, + port: Number(process.env.DB_PORT), + username: process.env.DB_AUTH_USER, + password: process.env.DB_AUTH_PASS, + database: process.env.DB_NAME, + synchronize: process.env.DB_SYNC == "true", + logging: process.env.DB_LOGGING == "true", + entities: [ + "dist/entity/**/*.js", + ], + migrations: [ + "dist/mirgration/**/*.js", + ], + subscribers: [ + "dist/subscriber/**/*.js", + ], +}); + +export default AppDataSource; \ No newline at end of file diff --git a/src/entity/501231711271780357/Lobby.ts b/src/entity/501231711271780357/Lobby.ts index b9f588b..d0ca25c 100644 --- a/src/entity/501231711271780357/Lobby.ts +++ b/src/entity/501231711271780357/Lobby.ts @@ -1,5 +1,6 @@ -import { Column, Entity, getConnection } from "typeorm"; +import { Column, Entity } from "typeorm"; import BaseEntity from "../../contracts/BaseEntity"; +import AppDataSource from "../../database/appDataSource"; @Entity() export default class Lobby extends BaseEntity { @@ -34,9 +35,7 @@ export default class Lobby extends BaseEntity { } public static async FetchOneByChannelId(channelId: string, relations?: string[]): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(Lobby); + const repository = AppDataSource.getRepository(Lobby); const single = await repository.findOne({ where: { ChannelId: channelId }, relations: relations || [] }); diff --git a/src/entity/Audit.ts b/src/entity/Audit.ts index 8577f0f..dddcc8e 100644 --- a/src/entity/Audit.ts +++ b/src/entity/Audit.ts @@ -1,7 +1,8 @@ -import { Column, Entity, getConnection } from "typeorm"; +import { Column, Entity } from "typeorm"; import { AuditType } from "../constants/AuditType"; import BaseEntity from "../contracts/BaseEntity"; import StringTools from "../helpers/StringTools"; +import AppDataSource from "../database/appDataSource"; @Entity() export default class Audit extends BaseEntity { @@ -35,9 +36,7 @@ export default class Audit extends BaseEntity { ServerId: string; public static async FetchAuditsByUserId(userId: string, serverId: string): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(Audit); + const repository = AppDataSource.getRepository(Audit); const all = await repository.find({ where: { UserId: userId, ServerId: serverId } }); @@ -45,9 +44,7 @@ export default class Audit extends BaseEntity { } public static async FetchAuditByAuditId(auditId: string, serverId: string): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(Audit); + const repository = AppDataSource.getRepository(Audit); const single = await repository.findOne({ where: { AuditId: auditId, ServerId: serverId } }); diff --git a/src/entity/IgnoredChannel.ts b/src/entity/IgnoredChannel.ts index ccb573c..dae34ad 100644 --- a/src/entity/IgnoredChannel.ts +++ b/src/entity/IgnoredChannel.ts @@ -1,5 +1,6 @@ -import { Entity, getConnection } from "typeorm"; +import { Entity } from "typeorm"; import BaseEntity from "../contracts/BaseEntity"; +import AppDataSource from "../database/appDataSource"; @Entity() export default class IgnoredChannel extends BaseEntity { @@ -10,9 +11,7 @@ export default class IgnoredChannel extends BaseEntity { } public static async IsChannelIgnored(channelId: string): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(IgnoredChannel); + const repository = AppDataSource.getRepository(IgnoredChannel); const single = await repository.findOne({ where: { Id: channelId } }); diff --git a/src/entity/Role.ts b/src/entity/Role.ts index 8bbd0d2..4c51372 100644 --- a/src/entity/Role.ts +++ b/src/entity/Role.ts @@ -1,6 +1,7 @@ -import { Column, Entity, getConnection, ManyToOne } from "typeorm"; +import { Column, Entity, ManyToOne } from "typeorm"; import BaseEntity from "../contracts/BaseEntity" import Server from "./Server"; +import AppDataSource from "../database/appDataSource"; @Entity() export default class Role extends BaseEntity { @@ -21,9 +22,7 @@ export default class Role extends BaseEntity { } public static async FetchOneByRoleId(roleId: string, relations?: string[]): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(Role); + const repository = AppDataSource.getRepository(Role); const single = await repository.findOne({ where: { RoleId: roleId }, relations: relations || []}); @@ -31,9 +30,7 @@ export default class Role extends BaseEntity { } public static async FetchAllByServerId(serverId: string): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(Server); + const repository = AppDataSource.getRepository(Server); const all = await repository.findOne({ where: { Id: serverId }, relations: [ "Roles", diff --git a/src/entity/Setting.ts b/src/entity/Setting.ts index 813accf..fe43ec6 100644 --- a/src/entity/Setting.ts +++ b/src/entity/Setting.ts @@ -1,6 +1,7 @@ -import { Column, Entity, getConnection, ManyToOne } from "typeorm"; +import { Column, Entity, ManyToOne } from "typeorm"; import BaseEntity from "../contracts/BaseEntity"; import Server from "./Server"; +import AppDataSource from "../database/appDataSource"; @Entity() export default class Setting extends BaseEntity { @@ -26,9 +27,7 @@ export default class Setting extends BaseEntity { } public static async FetchOneByKey(key: string, relations?: string[]): Promise { - const connection = getConnection(); - - const repository = connection.getRepository(Setting); + const repository = AppDataSource.getRepository(Setting); const single = await repository.findOne({ where: { Key: key }, relations: relations || {} }); diff --git a/src/vylbot.ts b/src/vylbot.ts index 16589c2..c1d2997 100644 --- a/src/vylbot.ts +++ b/src/vylbot.ts @@ -11,6 +11,12 @@ const requiredConfigs: string[] = [ "BOT_AUTHOR", "BOT_OWNERID", "BOT_CLIENTID", + "DB_HOST", + "DB_PORT", + "DB_AUTH_USER", + "DB_AUTH_PASS", + "DB_SYNC", + "DB_LOGGING", ]; requiredConfigs.forEach(config => {