Switch to TypeORM's DataSource API #299

Merged
Vylpes merged 5 commits from feature/297-stage-deployment into develop 2023-05-26 17:59:22 +01:00
15 changed files with 77 additions and 135 deletions
Showing only changes of commit 9ebc8a8991 - Show all commits

View file

@ -15,3 +15,11 @@ BOT_CLIENTID=682942374040961060
ABOUT_FUNDING=https://ko-fi.com/vylpes ABOUT_FUNDING=https://ko-fi.com/vylpes
ABOUT_REPO=https://gitea.vylpes.xyz/RabbitLabs/vylbot-app 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

View file

@ -14,3 +14,11 @@ BOT_CLIENTID=680083120896081954
ABOUT_FUNDING=https://ko-fi.com/vylpes ABOUT_FUNDING=https://ko-fi.com/vylpes
ABOUT_REPO=https://gitea.vylpes.xyz/RabbitLabs/vylbot-app 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

View file

@ -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

View file

@ -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"
}
}

View file

@ -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"
}
}

View file

@ -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"
}
}

View file

@ -8,6 +8,7 @@ import { Command } from "../type/command";
import { Events } from "./events"; import { Events } from "./events";
import { Util } from "./util"; import { Util } from "./util";
import AppDataSource from "../database/appDataSource";
export class CoreClient extends Client { export class CoreClient extends Client {
private static _commandItems: ICommandItem[]; private static _commandItems: ICommandItem[];
@ -41,10 +42,9 @@ export class CoreClient extends Client {
return; return;
} }
await createConnection().catch(e => { AppDataSource.initialize()
Vylpes marked this conversation as resolved Outdated

I think this should be awaited, or at least put the rest of the function in the .then()

I think this should be awaited, or at least put the rest of the function in the .then()
console.error(e); .then(() => console.log("Data Source Initialized"))
return; .catch((err) => console.error("Error Initialising Data Source", err));
});
super.on("interactionCreate", this._events.onInteractionCreate); super.on("interactionCreate", this._events.onInteractionCreate);
super.on("ready", this._events.onReady); super.on("ready", this._events.onReady);

View file

@ -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 { v4 } from "uuid";
import AppDataSource from "../database/appDataSource";
export default class BaseEntity { export default class BaseEntity {
constructor() { constructor() {
@ -21,25 +22,19 @@ export default class BaseEntity {
public async Save<T extends BaseEntity>(target: EntityTarget<T>, entity: DeepPartial<T>): Promise<void> { public async Save<T extends BaseEntity>(target: EntityTarget<T>, entity: DeepPartial<T>): Promise<void> {
this.WhenUpdated = new Date(); this.WhenUpdated = new Date();
const connection = getConnection(); const repository = AppDataSource.getRepository<T>(target);
const repository = connection.getRepository<T>(target);
await repository.save(entity); await repository.save(entity);
} }
public static async Remove<T extends BaseEntity>(target: EntityTarget<T>, entity: T): Promise<void> { public static async Remove<T extends BaseEntity>(target: EntityTarget<T>, entity: T): Promise<void> {
const connection = getConnection(); const repository = AppDataSource.getRepository<T>(target);
const repository = connection.getRepository<T>(target);
await repository.remove(entity); await repository.remove(entity);
} }
public static async FetchAll<T extends BaseEntity>(target: EntityTarget<T>, relations?: string[]): Promise<T[]> { public static async FetchAll<T extends BaseEntity>(target: EntityTarget<T>, relations?: string[]): Promise<T[]> {
const connection = getConnection(); const repository = AppDataSource.getRepository<T>(target);
const repository = connection.getRepository<T>(target);
const all = await repository.find({ relations: relations || [] }); const all = await repository.find({ relations: relations || [] });
@ -47,9 +42,7 @@ export default class BaseEntity {
} }
public static async FetchOneById<T extends BaseEntity>(target: EntityTarget<T>, id: string, relations?: string[]): Promise<T | null> { public static async FetchOneById<T extends BaseEntity>(target: EntityTarget<T>, id: string, relations?: string[]): Promise<T | null> {
const connection = getConnection(); const repository = AppDataSource.getRepository<T>(target);
const repository = connection.getRepository<T>(target);
const single = await repository.findOne({ where: ({ Id: id } as FindOptionsWhere<T>), relations: relations || {} }); const single = await repository.findOne({ where: ({ Id: id } as FindOptionsWhere<T>), relations: relations || {} });
@ -57,9 +50,7 @@ export default class BaseEntity {
} }
public static async Any<T extends ObjectLiteral>(target: EntityTarget<T>): Promise<boolean> { public static async Any<T extends ObjectLiteral>(target: EntityTarget<T>): Promise<boolean> {
const connection = getConnection(); const repository = AppDataSource.getRepository<T>(target);
const repository = connection.getRepository<T>(target);
const any = await repository.find(); const any = await repository.find();

View file

@ -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;

View file

@ -1,5 +1,6 @@
import { Column, Entity, getConnection } from "typeorm"; import { Column, Entity } from "typeorm";
import BaseEntity from "../../contracts/BaseEntity"; import BaseEntity from "../../contracts/BaseEntity";
import AppDataSource from "../../database/appDataSource";
@Entity() @Entity()
export default class Lobby extends BaseEntity { 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<Lobby | null> { public static async FetchOneByChannelId(channelId: string, relations?: string[]): Promise<Lobby | null> {
const connection = getConnection(); const repository = AppDataSource.getRepository(Lobby);
const repository = connection.getRepository(Lobby);
const single = await repository.findOne({ where: { ChannelId: channelId }, relations: relations || [] }); const single = await repository.findOne({ where: { ChannelId: channelId }, relations: relations || [] });

View file

@ -1,7 +1,8 @@
import { Column, Entity, getConnection } from "typeorm"; import { Column, Entity } from "typeorm";
import { AuditType } from "../constants/AuditType"; import { AuditType } from "../constants/AuditType";
import BaseEntity from "../contracts/BaseEntity"; import BaseEntity from "../contracts/BaseEntity";
import StringTools from "../helpers/StringTools"; import StringTools from "../helpers/StringTools";
import AppDataSource from "../database/appDataSource";
@Entity() @Entity()
export default class Audit extends BaseEntity { export default class Audit extends BaseEntity {
@ -35,9 +36,7 @@ export default class Audit extends BaseEntity {
ServerId: string; ServerId: string;
public static async FetchAuditsByUserId(userId: string, serverId: string): Promise<Audit[] | null> { public static async FetchAuditsByUserId(userId: string, serverId: string): Promise<Audit[] | null> {
const connection = getConnection(); const repository = AppDataSource.getRepository(Audit);
const repository = connection.getRepository(Audit);
const all = await repository.find({ where: { UserId: userId, ServerId: serverId } }); 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<Audit | null> { public static async FetchAuditByAuditId(auditId: string, serverId: string): Promise<Audit | null> {
const connection = getConnection(); const repository = AppDataSource.getRepository(Audit);
const repository = connection.getRepository(Audit);
const single = await repository.findOne({ where: { AuditId: auditId, ServerId: serverId } }); const single = await repository.findOne({ where: { AuditId: auditId, ServerId: serverId } });

View file

@ -1,5 +1,6 @@
import { Entity, getConnection } from "typeorm"; import { Entity } from "typeorm";
import BaseEntity from "../contracts/BaseEntity"; import BaseEntity from "../contracts/BaseEntity";
import AppDataSource from "../database/appDataSource";
@Entity() @Entity()
export default class IgnoredChannel extends BaseEntity { export default class IgnoredChannel extends BaseEntity {
@ -10,9 +11,7 @@ export default class IgnoredChannel extends BaseEntity {
} }
public static async IsChannelIgnored(channelId: string): Promise<boolean> { public static async IsChannelIgnored(channelId: string): Promise<boolean> {
const connection = getConnection(); const repository = AppDataSource.getRepository(IgnoredChannel);
const repository = connection.getRepository(IgnoredChannel);
const single = await repository.findOne({ where: { Id: channelId } }); const single = await repository.findOne({ where: { Id: channelId } });

View file

@ -1,6 +1,7 @@
import { Column, Entity, getConnection, ManyToOne } from "typeorm"; import { Column, Entity, ManyToOne } from "typeorm";
import BaseEntity from "../contracts/BaseEntity" import BaseEntity from "../contracts/BaseEntity"
import Server from "./Server"; import Server from "./Server";
import AppDataSource from "../database/appDataSource";
@Entity() @Entity()
export default class Role extends BaseEntity { 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<Role | null> { public static async FetchOneByRoleId(roleId: string, relations?: string[]): Promise<Role | null> {
const connection = getConnection(); const repository = AppDataSource.getRepository(Role);
const repository = connection.getRepository(Role);
const single = await repository.findOne({ where: { RoleId: roleId }, relations: relations || []}); 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<Role[]> { public static async FetchAllByServerId(serverId: string): Promise<Role[]> {
const connection = getConnection(); const repository = AppDataSource.getRepository(Server);
const repository = connection.getRepository(Server);
const all = await repository.findOne({ where: { Id: serverId }, relations: [ const all = await repository.findOne({ where: { Id: serverId }, relations: [
"Roles", "Roles",

View file

@ -1,6 +1,7 @@
import { Column, Entity, getConnection, ManyToOne } from "typeorm"; import { Column, Entity, ManyToOne } from "typeorm";
import BaseEntity from "../contracts/BaseEntity"; import BaseEntity from "../contracts/BaseEntity";
import Server from "./Server"; import Server from "./Server";
import AppDataSource from "../database/appDataSource";
@Entity() @Entity()
export default class Setting extends BaseEntity { 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<Setting | null> { public static async FetchOneByKey(key: string, relations?: string[]): Promise<Setting | null> {
const connection = getConnection(); const repository = AppDataSource.getRepository(Setting);
const repository = connection.getRepository(Setting);
const single = await repository.findOne({ where: { Key: key }, relations: relations || {} }); const single = await repository.findOne({ where: { Key: key }, relations: relations || {} });

View file

@ -11,6 +11,12 @@ const requiredConfigs: string[] = [
"BOT_AUTHOR", "BOT_AUTHOR",
"BOT_OWNERID", "BOT_OWNERID",
"BOT_CLIENTID", "BOT_CLIENTID",
"DB_HOST",
"DB_PORT",
"DB_AUTH_USER",
"DB_AUTH_PASS",
"DB_SYNC",
"DB_LOGGING",
]; ];
requiredConfigs.forEach(config => { requiredConfigs.forEach(config => {