server initial commit

This commit is contained in:
Ari Yeger
2025-07-15 13:34:30 -04:00
parent cf3f4270f9
commit 5fc2a1eb24
11 changed files with 1694 additions and 0 deletions

31
server/models/db.js Normal file
View File

@ -0,0 +1,31 @@
import dotenv from 'dotenv';
import {Pool} from 'pg';
dotenv.config();
const pool = new Pool(process.env.DATABASE_URL ? {connectionString: process.env.DATABASE_URL} : {
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_NAME,
password: process.env.DB_PASSWORD,
port: parseInt(process.env.DB_PORT || '5432', 10),
});
/**
* Generic query function for PostgreSQL database.
* @param {string} text
* @param {any[]} [params]
* @return {Promise<any[]>}
*/
async function query(text, params= []) {
const start = Date.now();
const res = await pool.query(text, params).then(qr => qr);
const duration = Date.now() - start;
if (params) for (let i = 0; i < params.length; i++) text = text.replace("$".concat(String(i + 1)), params[i]);
console.log('executed query', {text, duration, rows: res.rowCount});
return res.rows;
}
export default {
pool,
query
};

23
server/models/users.js Normal file
View File

@ -0,0 +1,23 @@
import db from "./db.js";
/** @typedef User
* @property {number} id - The unique identifier for the user.
* @property {string} username - The username of the user.
* @property {string} password - The hashed password of the user.
*/
/**
* Fetches all users from the database.
* @return {Promise<User[]>}
*/
const getAllUsers = async () => {
try {
return await db.query('SELECT * FROM users').then((users) => users);
} catch (error) {
console.error('Error fetching users:', error);
throw error;
}
}
export default {
getAllUsers
}