server for my chess game
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import pg from "pg";
const { Pool } = pg;

const pool = new Pool({
  connectionString: process.env.DATABASE_URL,
  ssl: { rejectUnauthorized: false },
});

pool.on("error", (err) => {
  console.error("database: err:", err);
});

/**
 * Runs a SQL command on the database
 *
 * @param {String|Object} query The query to run
 * @returns {Promise<(undefined|Object)>} result
 */
export async function command(query) {
  let client;
  try {
    client = await pool.connect();
    const res = await client.query(query);
    return res;
  } catch (err) {
    console.error("database: err:", err.stack);
  } finally {
    if (client) client.release();
  }
}