Skip to content

Commit

Permalink
Add an action to query teams from a database (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
mia-pi-git authored Sep 25, 2023
1 parent 2810ce6 commit d380afd
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 1 deletion.
5 changes: 5 additions & 0 deletions config/config-example.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,8 @@ exports.standings = {
"30": "Permaban",
"100": "Disabled",
};

/**
* @type {import('pg').PoolConfig | null}
*/
exports.postgres = null;
145 changes: 145 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
},
"dependencies": {
"@types/node": "^15.12.4",
"@types/pg": "^8.10.3",
"bcrypt": "^5.0.1",
"eslint-plugin-import": "^2.24.2",
"google-auth-library": "^3.1.2",
Expand Down
16 changes: 16 additions & 0 deletions src/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,22 @@ export const actions: {[k: string]: QueryHandler} = {
await tables.oauthTokens.deleteAll()`WHERE client = ${client.id} and owner = ${this.user.id}`;
return {success: true};
},

async getteams(params) {
if (!this.user.loggedIn || this.user.id === 'guest') {
return {teams: []}; // don't wanna nag people with popups if they aren't logged in
}
let teams = [];
try {
teams = await tables.pgdb.query(
'SELECT teamid, team, format, title as name FROM teams WHERE ownerid = $1', [this.user.id]
) ?? [];
} catch (e) {
Server.crashlog(e, 'a teams database query', params);
throw new ActionError('The server could not load your teams. Please try again later.');
}
return {teams};
},
};

if (Config.actions) {
Expand Down
14 changes: 14 additions & 0 deletions src/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import * as mysql from 'mysql2';
import * as pg from 'pg';

export type BasicSQLValue = string | number | null;
export type SQLRow = {[k: string]: BasicSQLValue};
Expand Down Expand Up @@ -290,3 +291,16 @@ export class DatabaseTable<Row> {
return this.updateAll(data)`WHERE \`${this.primaryKeyName}\` = ${primaryKey} LIMIT 1`;
}
}

export class PGDatabase {
database: pg.Pool | null;
constructor(config: pg.PoolConfig | null) {
this.database = config ? new pg.Pool(config) : null;
}
async query<O = any>(query: string, values: BasicSQLValue[]) {
if (!this.database) return null;
const result = await this.database.query(query, values);
return result.rows as O[];
}
}

3 changes: 2 additions & 1 deletion src/tables.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* Login server database tables
*/
import {Database, DatabaseTable} from './database';
import {Database, DatabaseTable, PGDatabase} from './database';
import {Config} from './config-loader';

import type {LadderEntry} from './ladder';
import type {ReplayData} from './replays';

// direct access
export const psdb = new Database(Config.mysql);
export const pgdb = new PGDatabase(Config.postgres);
export const replaysDB = Config.replaysdb ? new Database(Config.replaysdb!) : psdb;
export const ladderDB = Config.ladderdb ? new Database(Config.ladderdb!) : psdb;

Expand Down

0 comments on commit d380afd

Please sign in to comment.