Skip to content

Commit

Permalink
Switch to TypeORM's DataSource API (#299)
Browse files Browse the repository at this point in the history
- Switch to TypeORM's DataSource API, rather than using the now deprecated ormconfig.json
- This will fix stage deployment not knowing how to deploy the database migrations

#297

> **NOTE:** This change requires the deployment scripts to be updated, please update them on the server before merging

Co-authored-by: Ethan Lane <ethan@vylpes.com>
Reviewed-on: https://gitea.vylpes.xyz/RabbitLabs/vylbot-app/pulls/299
  • Loading branch information
Vylpes committed May 26, 2023
1 parent c241838 commit e6c845e
Show file tree
Hide file tree
Showing 37 changed files with 128 additions and 164 deletions.
8 changes: 8 additions & 0 deletions .dev.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions .prod.env
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions .stage.env
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ BOT_CLIENTID=1016767908740857949

ABOUT_FUNDING=https://ko-fi.com/vylpes
ABOUT_REPO=https://gitea.vylpes.xyz/RabbitLabs/vylbot-app

DB_HOST=127.0.0.1
DB_PORT=3111
DB_NAME=vylbot
DB_AUTH_USER=stage
DB_AUTH_PASS=stage
DB_SYNC=false
DB_LOGGING=false
24 changes: 0 additions & 24 deletions ormconfig.dev.json

This file was deleted.

24 changes: 0 additions & 24 deletions ormconfig.prod.json

This file was deleted.

24 changes: 0 additions & 24 deletions ormconfig.stage.json

This file was deleted.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"build": "tsc",
"start": "node ./dist/vylbot",
"test": "jest",
"db:up": "typeorm migration:run",
"db:down": "typeorm migration:revert"
"db:up": "typeorm migration:run -d dist/database/dataSources/appDataSource.js",
"db:down": "typeorm migration:revert -d dist/database/dataSources/appDataSource.js"
},
"repository": {
"type": "git",
Expand Down
1 change: 0 additions & 1 deletion scripts/deploy_prod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ cd ~/apps/vylbot/vylbot_prod \
&& (pm2 stop vylbot_prod || true) \
&& (pm2 delete vylbot_prod || true) \
&& cp .prod.env .env \
&& cp ormconfig.prod.json ormconfig.json \
&& yarn clean \
&& yarn install --frozen-lockfile \
&& yarn build \
Expand Down
1 change: 0 additions & 1 deletion scripts/deploy_stage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ cd ~/apps/vylbot/vylbot_stage \
&& (pm2 stop vylbot_stage || true) \
&& (pm2 delete vylbot_stage || true) \
&& cp .stage.env .env \
&& cp ormconfig.stage.json ormconfig.json \
&& yarn clean \
&& yarn install --frozen-lockfile \
&& yarn build \
Expand Down
10 changes: 5 additions & 5 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { Command } from "../type/command";

import { Events } from "./events";
import { Util } from "./util";
import AppDataSource from "../database/dataSources/appDataSource";

export class CoreClient extends Client {
private static _commandItems: ICommandItem[];
private static _eventItems: IEventItem[];

private _events: Events;
private _util: Util;

Expand Down Expand Up @@ -41,10 +42,9 @@ export class CoreClient extends Client {
return;
}

await createConnection().catch(e => {
console.error(e);
return;
});
await 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);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/501231711271780357/Lobby/add.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import { Command } from "../../../type/command";
import { default as eLobby } from "../../../entity/501231711271780357/Lobby";
import { default as eLobby } from "../../../database/entities/501231711271780357/Lobby";

export default class AddRole extends Command {
constructor() {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/501231711271780357/Lobby/list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CacheType, CommandInteraction, EmbedBuilder, GuildBasedChannel, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import { Command } from "../../../type/command";
import { default as eLobby } from "../../../entity/501231711271780357/Lobby";
import { default as eLobby } from "../../../database/entities/501231711271780357/Lobby";
import EmbedColours from "../../../constants/EmbedColours";

export default class ListLobby extends Command {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/501231711271780357/Lobby/lobby.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandInteraction, SlashCommandBuilder } from "discord.js";
import { Command } from "../../../type/command";
import { default as eLobby } from "../../../entity/501231711271780357/Lobby";
import { default as eLobby } from "../../../database/entities/501231711271780357/Lobby";

export default class Lobby extends Command {
constructor() {
Expand All @@ -13,7 +13,7 @@ export default class Lobby extends Command {

public override async execute(interaction: CommandInteraction) {
if (!interaction.channelId) return;

const lobby = await eLobby.FetchOneByChannelId(interaction.channelId);

if (!lobby) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/501231711271780357/Lobby/remove.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import { Command } from "../../../type/command";
import { default as eLobby } from "../../../entity/501231711271780357/Lobby";
import { default as eLobby } from "../../../database/entities/501231711271780357/Lobby";
import BaseEntity from "../../../contracts/BaseEntity";

export default class RemoveLobby extends Command {
Expand Down Expand Up @@ -32,7 +32,7 @@ export default class RemoveLobby extends Command {
await interaction.reply('Channel not found.');
return;
}

await BaseEntity.Remove<eLobby>(eLobby, entity);

await interaction.reply(`Removed <#${channel.channel.id}> from the list of lobby channels`);
Expand Down
8 changes: 4 additions & 4 deletions src/commands/Role/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import { Command } from "../../type/command";
import { default as eRole } from "../../entity/Role";
import Server from "../../entity/Server";
import { default as eRole } from "../../database/entities/Role";
import Server from "../../database/entities/Server";

export default class ConfigRole extends Command {
constructor() {
Expand Down Expand Up @@ -33,7 +33,7 @@ export default class ConfigRole extends Command {

if (existingRole) {
await eRole.Remove(eRole, existingRole);

await interaction.reply('Removed role from configuration.');
} else {
const server = await Server.FetchOneById(Server, interaction.guildId);
Expand All @@ -45,7 +45,7 @@ export default class ConfigRole extends Command {

const newRole = new eRole(role.role.id);
newRole.SetServer(server);

await newRole.Save(eRole, newRole);

await interaction.reply('Added role to configuration.');
Expand Down
4 changes: 2 additions & 2 deletions src/commands/Role/role.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandInteraction, EmbedBuilder, GuildMemberRoleManager, SlashCommandBuilder } from "discord.js";
import { Command } from "../../type/command";
import { default as eRole } from "../../entity/Role";
import { default as eRole } from "../../database/entities/Role";
import EmbedColours from "../../constants/EmbedColours";

export default class Role extends Command {
Expand Down Expand Up @@ -47,7 +47,7 @@ export default class Role extends Command {
.setColor(EmbedColours.Ok)
.setTitle("Roles")
.setDescription(`Roles: ${roles.length}\n\n${roles.join("\n")}`);

await interaction.reply({ embeds: [ embed ]});
}

Expand Down
12 changes: 6 additions & 6 deletions src/commands/audits.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Audit from "../entity/Audit";
import Audit from "../database/entities/Audit";
import AuditTools from "../helpers/AuditTools";
import { Command } from "../type/command";
import { CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuilder } from "discord.js";
Expand All @@ -13,7 +13,7 @@ export default class Audits extends Command {
.setName("audits")
.setDescription("View audits of a particular user in the server")
.setDefaultMemberPermissions(PermissionsBitField.Flags.ModerateMembers)
.addSubcommand(subcommand =>
.addSubcommand(subcommand =>
subcommand
.setName('user')
.setDescription('View all audits done against a user')
Expand Down Expand Up @@ -66,7 +66,7 @@ export default class Audits extends Command {
option
.setName('reason')
.setDescription('The reason')));

}

public override async execute(interaction: CommandInteraction) {
Expand Down Expand Up @@ -188,13 +188,13 @@ export default class Audits extends Command {
await interaction.reply("Audit cleared.");
}

private async AddAudit(interaction: CommandInteraction) {
private async AddAudit(interaction: CommandInteraction) {
if (!interaction.guildId) return;

const user = interaction.options.getUser('target');
const auditType = interaction.options.get('type');
const reasonInput = interaction.options.get('reason');

if (!user || !auditType || !auditType.value) {
await interaction.reply("Invalid input.");
return;
Expand All @@ -206,7 +206,7 @@ export default class Audits extends Command {
const audit = new Audit(user.id, type, reason, interaction.user.id, interaction.guildId);

await audit.Save(Audit, audit);

await interaction.reply(`Created new audit with ID \`${audit.AuditId}\``);
}
}
2 changes: 1 addition & 1 deletion src/commands/ban.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from "../type/command";
import Audit from "../entity/Audit";
import Audit from "../database/entities/Audit";
import { AuditType } from "../constants/AuditType";
import { CommandInteraction, EmbedBuilder, GuildMember, PermissionsBitField, SlashCommandBuilder, TextChannel } from "discord.js";
import EmbedColours from "../constants/EmbedColours";
Expand Down
6 changes: 3 additions & 3 deletions src/commands/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { CommandInteraction, EmbedBuilder, PermissionsBitField, SlashCommandBuil
import { readFileSync } from "fs";
import DefaultValues from "../constants/DefaultValues";
import EmbedColours from "../constants/EmbedColours";
import Server from "../entity/Server";
import Setting from "../entity/Setting";
import Server from "../database/entities/Server";
import Setting from "../database/entities/Setting";
import { Command } from "../type/command";

export default class Config extends Command {
Expand Down Expand Up @@ -124,7 +124,7 @@ export default class Config extends Command {

private async ResetValue(interaction: CommandInteraction) {
if (!interaction.guildId) return;

const key = interaction.options.get('key');

if (!key || !key.value) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/ignore.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import IgnoredChannel from "../entity/IgnoredChannel";
import IgnoredChannel from "../database/entities/IgnoredChannel";
import { Command } from "../type/command";

export default class Ignore extends Command {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/kick.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Command } from "../type/command";
import Audit from "../entity/Audit";
import Audit from "../database/entities/Audit";
import { AuditType } from "../constants/AuditType";
import { CommandInteraction, EmbedBuilder, GuildMember, PermissionsBitField, SlashCommandBuilder, TextChannel } from "discord.js";
import EmbedColours from "../constants/EmbedColours";
Expand Down Expand Up @@ -54,7 +54,7 @@ export default class Kick extends Command {
value: reason,
},
]);

if (!member.kickable) {
await interaction.reply('Insufficient permissions. Please contact a moderator.');
return;
Expand Down
2 changes: 1 addition & 1 deletion src/commands/mute.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommandInteraction, EmbedBuilder, GuildMember, PermissionsBitField, SlashCommandBuilder, TextChannel } from "discord.js";
import { AuditType } from "../constants/AuditType";
import EmbedColours from "../constants/EmbedColours";
import Audit from "../entity/Audit";
import Audit from "../database/entities/Audit";
import SettingsHelper from "../helpers/SettingsHelper";
import { Command } from "../type/command";

Expand Down
2 changes: 1 addition & 1 deletion src/commands/setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandInteraction, PermissionsBitField, SlashCommandBuilder } from "discord.js";
import Server from "../entity/Server";
import Server from "../database/entities/Server";
import { Command } from "../type/command";

export default class Setup extends Command {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/warn.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { CommandInteraction, EmbedBuilder, GuildMember, PermissionsBitField, SlashCommandBuilder, TextChannel } from "discord.js";
import { AuditType } from "../constants/AuditType";
import EmbedColours from "../constants/EmbedColours";
import Audit from "../entity/Audit";
import Audit from "../database/entities/Audit";
import SettingsHelper from "../helpers/SettingsHelper";
import { Command } from "../type/command";

Expand Down
Loading

0 comments on commit e6c845e

Please sign in to comment.