-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_db.js
More file actions
88 lines (76 loc) · 2.28 KB
/
setup_db.js
File metadata and controls
88 lines (76 loc) · 2.28 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const { Pool } = require('pg');
// Create a new instance of the Pool class
const pool = new Pool({
user: process.env.DB_USER,
host: process.env.DB_HOST,
database: process.env.DB_DB,
password: process.env.DB_PASSWORD,
port: process.env.DB_PORT,
});
// Function to create the family_members table
async function createFamilyMembersTable() {
try {
const query = `
CREATE TABLE IF NOT EXISTS family_members (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
age INTEGER,
gender VARCHAR(10),
created_at TIMESTAMP DEFAULT NOW()
)`;
await pool.query(query);
console.log('family_members table created');
} catch (error) {
console.error('Error creating family_members table:', error);
}
}
// Function to create the monthly_expenses table
async function createMonthlyExpensesTable() {
try {
const query = `
CREATE TABLE IF NOT EXISTS monthly_expenses (
id SERIAL PRIMARY KEY,
member_id INTEGER REFERENCES family_members(id),
expense_name VARCHAR(255) NOT NULL,
amount NUMERIC(10, 2) NOT NULL,
category VARCHAR(255),
recurring_day INTEGER NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
)`;
await pool.query(query);
console.log('monthly_expenses table created');
} catch (error) {
console.error('Error creating monthly_expenses table:', error);
}
}
async function createIncomeTransactionsTable() {
try {
const query = `
CREATE TABLE IF NOT EXISTS income_transactions (
id SERIAL PRIMARY KEY,
member_id INTEGER REFERENCES family_members(id),
transaction_date DATE,
description VARCHAR(255),
amount NUMERIC
);`;
await pool.query(query);
console.log('Income transactions table created successfully');
} catch (error) {
console.error('Error creating income transactions table:', error);
}
}
// Call the functions to create the tables
async function createTables() {
try {
await createFamilyMembersTable();
await createMonthlyExpensesTable();
await createIncomeTransactionsTable();
} finally {
pool.end(); // Close the database connection
}
}
// Execute the createTables function
createTables();