db/PostgreSQL.js

"use strict";
// PostgreSQL.ts - PostgreSQL client (noud02)
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const child_process_1 = require("child_process");
const pg = require("pg");
/**
 * PostgreSQL client class
 *
 * @export
 * @class PostgreSQL
 */
class PostgreSQL {
    constructor(options) {
        this.options = options;
        /**
         * `pg` client
         * @see https://node-postgres.com/
         *
         * @type {pg.Client}
         */
        this.client = new pg.Client(this.options);
    }
    /**
     * Connects the client to the database
     *
     * @returns {Promise<void>}
     */
    connect() {
        return new Promise((resolve, reject) => {
            this.client.connect((err) => {
                if (err) {
                    return reject(err);
                }
                return resolve();
            });
        });
    }
    /**
     * Disconnects the client from the database
     *
     * @returns {Promise<void>}
     */
    disconnect() {
        return new Promise((resolve, reject) => {
            this.client.end((err) => {
                if (err) {
                    return reject(err);
                }
                return resolve();
            });
        });
    }
    /**
     * Execute a raw query (command)
     *
     * @param {string} query Query, eg: `SELECT * FROM guilds;`
     * @returns {Promise<string>}
     */
    rawQuery(query) {
        return new Promise((resolve, reject) => {
            child_process_1.exec(`psql ${this.options.database} -c '${query}'`, (err, stdout) => {
                if (err) {
                    return reject(err);
                }
                return resolve(stdout);
            });
        });
    }
    /**
     * Insert data into a table
     *
     * @param {string} table Table to query
     * @param {*} data Data to insert
     * @returns {Promise<pg.QueryResult>}
     */
    insert(table, data) {
        const vals = [];
        const keys = Object.keys(data);
        for (const key of keys) {
            vals.push(data[key]);
        }
        return this.client.query(`INSERT INTO ${table} (${keys.join(", ")}) VALUES (${keys.map((_key, i) => `$${i + 1}`).join(", ")});`, vals);
    }
    /**
     * Update data in a table
     *
     * @example PostgreSQL.update('nya', 'id = "awoo"', { reee: true });
     *
     * @param {string} table Table to query
     * @param {string} expression Expression, eg: `id = 123456, name = "awoo"`
     * @param {*} data New data
     * @returns {Promise<pg.QueryResult>}
     */
    update(table, expression, data) {
        const vals = [];
        const keys = Object.keys(data);
        const changes = [];
        for (const key of keys) {
            vals.push(data[key]);
            changes.push(`${key} = $${keys.indexOf(key) + 1}`);
        }
        return this.client.query(`UPDATE ${table} SET ${changes.join(", ")} WHERE ${expression};`, vals);
    }
    /**
     * Select data from database
     *
     * @example PostgreSQL.select('nyan', 'id = "awoo"')
     *
     * @param {string} table Table to query
     * @param {string} expression Expression, eg: `id = 123456, name = "awoo"`
     * @returns {Promise<pg.QueryResult>}
     */
    select(table, expression) {
        return this.client.query(`SELECT * FROM ${table} WHERE ${expression};`);
    }
    /**
     * Adds a guild to the database if it isn't in there already
     *
     * @param {Eris.Guild} guild
     * @returns {Promise<pg.QueryResult>}
     */
    addGuild(guild) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const data = {
                id: guild.id,
                name: guild.name,
                prefixes: [],
            };
            const query = yield this.select("guilds", `id = '${guild.id}'`);
            if (query.rows.length > 0) {
                return query;
            }
            else {
                return this.insert("guilds", data);
            }
        });
    }
}
exports.PostgreSQL = PostgreSQL;