-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.js
More file actions
51 lines (45 loc) · 1.35 KB
/
database.js
File metadata and controls
51 lines (45 loc) · 1.35 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const { Pool } = require('pg');
// Database connection
const connectionString = process.env.DATABASE_URL || 'postgresql://localhost/studymatcher_dev';
const pool = new Pool({
connectionString: connectionString,
ssl: process.env.DATABASE_URL ? { rejectUnauthorized: false } : false
});
// Test connection and create tables
pool.query('SELECT NOW()', (err, res) => {
if (err) {
console.error('Error connecting to database:', err);
} else {
console.log('Connected to PostgreSQL database');
createTables();
}
});
// Create tables if they don't exist
async function createTables() {
try {
await pool.query(`
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
major TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`);
console.log('Users table ready');
await pool.query(`
CREATE TABLE IF NOT EXISTS user_courses (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL,
course_name TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
UNIQUE(user_id, course_name)
)
`);
console.log('User_courses table ready');
} catch (err) {
console.error('Error creating tables:', err);
}
}
module.exports = pool;