Skip to content

Commit

Permalink
added two databases for plotsystem and network
Browse files Browse the repository at this point in the history
  • Loading branch information
MineFact committed Sep 18, 2023
1 parent 22d98aa commit ad47347
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 30 deletions.
11 changes: 10 additions & 1 deletion _config.json5
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@
port: 8080,
debug: false,

database: {
plotsystem_database: {
host: '127.0.01',
port: 3306,
username: 'root',
password: 'password',
db: 'MyDatabase',
max_conn: 5
},

network_database: {
host: '127.0.01',
port: 3306,
username: 'root',
Expand Down
25 changes: 12 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import Database from "./struct/database.js";
import Joi from "joi";
import { NestFactory } from "@nestjs/core";
import PlotSystem from "./struct/core/plotsystem.js";
import Settings from "./struct/settings.js";
import bodyParser from "body-parser";
import cors from "cors";
import express from "express";
import helmet from "helmet";
Expand All @@ -14,52 +12,53 @@ const router = express.Router();
// Plot System Imports

const config = new Settings();
const db = new Database(config);
const plotsystem_database = new Database(config, config.plotsystem_database);
const network_database = new Database(config, config.network_database);

const plotSystem = new PlotSystem(db);
const plotSystem = new PlotSystem(plotsystem_database);

// Init GET Routes for the API
(await import("./routes/builders/GET.js")).initRoutes(router, Joi, plotSystem);
(await import("./routes/difficulties/GET.js")).initRoutes(
(await import("./routes/plotsystem/builders/GET.js")).initRoutes(router, Joi, plotSystem);
(await import("./routes/plotsystem/difficulties/GET.js")).initRoutes(
router,
Joi,
plotSystem
);
(await import("./routes/teams/cities/GET.js")).initRoutes(
(await import("./routes/plotsystem/teams/cities/GET.js")).initRoutes(
router,
Joi,
plotSystem
);
(await import("./routes/teams/countries/GET.js")).initRoutes(
(await import("./routes/plotsystem/teams/countries/GET.js")).initRoutes(
router,
Joi,
plotSystem
);
(await import("./routes/teams/plots/GET.js")).initRoutes(
(await import("./routes/plotsystem/teams/plots/GET.js")).initRoutes(
router,
Joi,
plotSystem
);
(await import("./routes/teams/reviews/GET.js")).initRoutes(
(await import("./routes/plotsystem/teams/reviews/GET.js")).initRoutes(
router,
Joi,
plotSystem
);
(await import("./routes/teams/servers/GET.js")).initRoutes(
(await import("./routes/plotsystem/teams/servers/GET.js")).initRoutes(
router,
Joi,
plotSystem
);
// Init POST Routes for the API

(await import("./routes/teams/plots/POST.js")).initRoutes(
(await import("./routes/plotsystem/teams/plots/POST.js")).initRoutes(
router,
Joi,
plotSystem
);

// Init PUT Routes for the API
(await import("./routes/teams/plots/PUT.js")).initRoutes(
(await import("./routes/plotsystem/teams/plots/PUT.js")).initRoutes(
router,
Joi,
plotSystem
Expand Down
14 changes: 7 additions & 7 deletions src/struct/core/plotsystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import express from "express";
import joi from "joi";

export default class PlotSystem {
private database: DatabaseHandler;
private plotsystem_database: DatabaseHandler;
private builders: any[] | null = null;
private difficulties: any[] | null = null;
private api_keys: any[] | null = null;
private buildTeams = new Map();

constructor(database: DatabaseHandler) {
this.database = database;
constructor(plotsystem_database: DatabaseHandler) {
this.plotsystem_database = plotsystem_database;
}

async updateCache(isStarting: boolean = false) {
Expand Down Expand Up @@ -83,7 +83,7 @@ export default class PlotSystem {
if (this.buildTeams.has(api_key)) return this.buildTeams.get(api_key);

// Create a new build team and add it to the cache
const buildTeam = new BuildTeam(api_key, this.database);
const buildTeam = new BuildTeam(api_key, this.plotsystem_database);
this.buildTeams.set(api_key, buildTeam);

return buildTeam;
Expand Down Expand Up @@ -119,17 +119,17 @@ export default class PlotSystem {

async getBuildersFromDatabase() {
const SQL = "SELECT COUNT(uuid) FROM plotsystem_builders";
return await this.database.query(SQL);
return await this.plotsystem_database.query(SQL);
}

async getDifficultiesFromDatabase() {
const SQL = "SELECT * FROM plotsystem_difficulties";
return await this.database.query(SQL);
return await this.plotsystem_database.query(SQL);
}

async getAPIKeysFromDatabase() {
const SQL = "SELECT * FROM plotsystem_api_keys";
const result = await this.database.query(SQL); // result: [{"id":1,"api_key":"super_cool_api_key","created_at":"2022-06-23T18:00:09.000Z"}]
const result = await this.plotsystem_database.query(SQL); // result: [{"id":1,"api_key":"super_cool_api_key","created_at":"2022-06-23T18:00:09.000Z"}]
return result.map((row: { api_key: string }) => row.api_key); // result: ["super_cool_api_key"]
}
}
18 changes: 10 additions & 8 deletions src/struct/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,24 @@ export interface Database {
}

export default class DatabaseHandler {
public settings!: Settings;
public database_settings!: Database;
public settings: Settings;
private pool!: mariadb.Pool;

constructor(settings: Settings) {
constructor(settings: Settings, database_settings: Database) {
this.database_settings = database_settings;
this.settings = settings;

console.log(
`Using Database at ${settings.database.host}:${settings.database.port}/${settings.database.db}`
`Using Database at ${database_settings.host}:${database_settings.port}/${database_settings.db}`
);

this.pool = mariadb.createPool({
host: settings.database.host,
user: settings.database.username,
password: settings.database.password,
connectionLimit: settings.database.max_conn,
database: settings.database.db,
host: database_settings.host,
user: database_settings.username,
password: database_settings.password,
connectionLimit: database_settings.max_conn,
database: database_settings.db,
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/struct/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default class Settings {
public port!: number;
public debug!: boolean;

public database!: Database;
public plotsystem_database!: Database;
public network_database!: Database;

constructor(file?: string) {
if (!file) file = path.join(__dirname, "../../config.json5");
Expand Down

0 comments on commit ad47347

Please sign in to comment.