-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitDB.js
130 lines (117 loc) · 4.27 KB
/
initDB.js
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const sqlite3 = require('sqlite3').verbose();
const bcrypt = require('bcrypt');
const saltRounds = 10;
const db = new sqlite3.Database('./db/cafeteria.db', (err) => {
if (err) {
return console.error(err.message);
}
console.log('Conectado a la base de datos.');
});
db.serialize(() => {
db.run(`CREATE TABLE IF NOT EXISTS Users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
role TEXT NOT NULL
)`);
// Crear o actualizar el usuario administrador
const username = 'admin';
const password = 'SecurePass123!'; // Nueva contraseña segura y sencilla
bcrypt.hash(password, saltRounds, (err, hash) => {
if (err) {
console.error('Error hashing password:', err);
} else {
db.run(`INSERT INTO Users (username, password, role) VALUES (?, ?, ?)
ON CONFLICT(username) DO UPDATE SET password=excluded.password, role=excluded.role`,
[username, hash, 'admin'], (err) => {
if (err) {
console.error('Error inserting/updating user:', err);
} else {
console.log('Admin user created/updated successfully');
}
// Cerrar la base de datos después de la operación
db.close((err) => {
if (err) {
return console.error(err.message);
}
console.log('Base de datos cerrada.');
});
});
}
});
db.run(`CREATE TABLE IF NOT EXISTS Campus (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
state TEXT NOT NULL
)`);
db.run(`CREATE TABLE IF NOT EXISTS Cafeteria (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
campus_id INTEGER NOT NULL,
encargado TEXT NOT NULL,
state TEXT NOT NULL,
FOREIGN KEY (campus_id) REFERENCES Campus(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS TiposUsuarios (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
state TEXT NOT NULL
)`);
db.run(`CREATE TABLE IF NOT EXISTS Marcas (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
state TEXT NOT NULL
)`);
db.run(`CREATE TABLE IF NOT EXISTS Proveedores (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nombre_comercial TEXT NOT NULL,
rnc TEXT NOT NULL,
fecha_registro TEXT NOT NULL,
state TEXT NOT NULL
)`);
db.run(`CREATE TABLE IF NOT EXISTS Usuarios (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nombre TEXT NOT NULL,
cedula TEXT NOT NULL,
tipo_usuario_id INTEGER NOT NULL,
limite_credito REAL NOT NULL,
fecha_registro TEXT NOT NULL,
state TEXT NOT NULL,
FOREIGN KEY (tipo_usuario_id) REFERENCES TiposUsuarios(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS Empleados (
id INTEGER PRIMARY KEY AUTOINCREMENT,
nombre TEXT NOT NULL,
cedula TEXT NOT NULL,
tanda_labor TEXT NOT NULL,
porciento_comision REAL NOT NULL,
fecha_ingreso TEXT NOT NULL,
state TEXT NOT NULL
)`);
db.run(`CREATE TABLE IF NOT EXISTS Articulos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
description TEXT NOT NULL,
marca_id INTEGER NOT NULL,
costo REAL NOT NULL,
proveedor_id INTEGER NOT NULL,
existencia INTEGER NOT NULL,
state TEXT NOT NULL,
FOREIGN KEY (marca_id) REFERENCES Marcas(id),
FOREIGN KEY (proveedor_id) REFERENCES Proveedores(id)
)`);
db.run(`CREATE TABLE IF NOT EXISTS Ventas (
id INTEGER PRIMARY KEY AUTOINCREMENT,
no_factura TEXT NOT NULL,
empleado_id INTEGER NOT NULL,
articulo_id INTEGER NOT NULL,
usuario_id INTEGER NOT NULL,
fecha_venta DATE NOT NULL,
monto REAL NOT NULL,
unidades_vendidas INTEGER NOT NULL,
comentario TEXT,
estado TEXT NOT NULL,
FOREIGN KEY (empleado_id) REFERENCES Empleados(id),
FOREIGN KEY (articulo_id) REFERENCES Articulos(id),
FOREIGN KEY (usuario_id) REFERENCES Usuarios(id)
)`);
});