-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-db.js
More file actions
55 lines (46 loc) · 1.93 KB
/
init-db.js
File metadata and controls
55 lines (46 loc) · 1.93 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
const mysql = require('mysql2/promise');
const dotenv = require('dotenv');
dotenv.config();
async function initDB() {
try {
// Connect without database selected to create it
const connection = await mysql.createConnection({
host: process.env.DB_HOST || 'localhost',
user: process.env.DB_USER || 'root',
password: process.env.DB_PASSWORD || ''
});
console.log('Connected to MySQL server.');
await connection.query(`CREATE DATABASE IF NOT EXISTS ${process.env.DB_NAME || 'cryptocardiac'}`);
console.log(`Database '${process.env.DB_NAME || 'cryptocardiac'}' created or already exists.`);
await connection.changeUser({ database: process.env.DB_NAME || 'cryptocardiac' });
const createUsersTable = `
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
email VARCHAR(255) NOT NULL UNIQUE,
password_hash VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
`;
await connection.query(createUsersTable);
console.log('Table "users" created or checked.');
const createVotesTable = `
CREATE TABLE IF NOT EXISTS votes (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
coin_id VARCHAR(255) NOT NULL,
coin_name VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id)
)
`;
await connection.query(createVotesTable);
console.log('Table "votes" created or checked.');
await connection.end();
console.log('Database initialization complete.');
process.exit(0);
} catch (error) {
console.error('Database initialization failed:', error);
process.exit(1);
}
}
initDB();