-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
26 lines (22 loc) · 725 Bytes
/
db.js
File metadata and controls
26 lines (22 loc) · 725 Bytes
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
import mysql from 'mysql2/promise';
import fs from 'fs/promises';
import dotenv from 'dotenv';
dotenv.config();
const db = await mysql.createConnection({
host: 'localhost',
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: 'syncode_db'
});
console.log("Conectado a MySQL");
// Importar database.sql si no existen las tabla "users"
const [tables] = await db.query("SHOW TABLES LIKE 'users'");
if (tables.length === 0) {
console.log("Inicializando base de datos...");
const sql = await fs.readFile('./database.sql', 'utf-8');
await db.query(sql);
console.log("Base de datos inicializada");
} else {
console.log("Base de datos ya inicializada");
}
export default db;