-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
148 lines (123 loc) · 4.36 KB
/
main.ts
File metadata and controls
148 lines (123 loc) · 4.36 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import fs from "fs";
import path from "path";
import {pathToFileURL} from "url";
import {
showBufferedEntities,
showEntitiesData,
showEntitiesDataBuff, showNormalizedEntities,
transformBufferedToValidatedData, EntitiesData, parsedEntities
} from "./entity-decorator";
import mysql, {Connection} from "mysql2/promise";
import {BufferedEntities, checkIfEntity} from "./entity-decorator";
import {Property} from "./Property";
import "reflect-metadata";
import {startupQuery} from "./Querys";
import {BeeRepository} from "./RepoUtils/PosibleWays";
import {Gato, Perro} from "./Entities/user";
export async function loadEntities(directory: string) {
const entitiesPath = path.resolve(directory); // Resolviendo ruta absoluta
if (!fs.existsSync(entitiesPath)) {
throw new Error(`❌ Error: La carpeta de entidades no existe -> ${entitiesPath}`);
}
const files = fs.readdirSync(entitiesPath);
for (const file of files) {
if (file.endsWith(".ts") || file.endsWith(".js")) {
const filePath = pathToFileURL(path.join(entitiesPath, file)).href; // Convertir a URL
await import(filePath); // Importar dinámicamente
}
}
console.log(`✅ Entidades cargadas desde: ${entitiesPath}`);
showEntitiesData();
showBufferedEntities();
showEntitiesDataBuff();
transformBufferedToValidatedData();
showEntitiesData();
showNormalizedEntities()
}
async function connection(conn: { host: string; user: string; password: string; database: string;}){
return mysql.createConnection({
host: conn.host,
user: conn.user,
password: conn.password,
database: conn.database
});
}
// Función para cerrar la conexión
async function closeConnection(connection: Connection) {
if (connection) {
await connection.end();
console.log("✅ Conexión cerrada.");
} else {
console.error("❌ No hay conexión para cerrar.");
}
}
export class BeeORM{
static dbConnection: Connection;
static async init(route: string){
await loadEntities(route);
}
static async connection(conn: { host: string; user: string; password: string; database: string;}){
this.dbConnection = await connection(conn);
}
static async closeConnection(){
await closeConnection(this.dbConnection);
}
static async test() {
if (!this.dbConnection) {
console.error("❌ No hay conexión a la base de datos.");
return;
}
try {
const [rows, fields] = await this.dbConnection.execute(
"CREATE TABLE IF NOT EXISTS usersPrueba (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), name2 VARCHAR(255))"
);
console.log("✅ Tabla creada o ya existe:", rows);
} catch (err) {
console.error("❌ Error al ejecutar la consulta:", err);
}
}
static async query(query: string) {
if (!this.dbConnection) {
console.error("❌ No hay conexión a la base de datos.");
return;
}
try {
const [rows, fields] = await this.dbConnection.execute(query);
console.log("row ",rows);
console.log("fields ",fields);
return rows;
} catch (e) {
console.error("Error executing query ❌")
}
}
static async BeeFactory(T) {
return new BeeRepository(T)
}
static async StartupQuery() {
await startupQuery(EntitiesData);
}
}
//await BeeORM.init("./Entities");
//await BeeORM.connection({
// host: "localhost",
// user: "root",
// password: "kgyspy10230",
// database: "albertBank"
//});
//await BeeORM.test();
//await BeeORM.query("CREATE TABLE IF NOT EXISTS usersPrueba2 (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), surname VARCHAR(255))")
//await BeeORM.query("INSERT INTO usersPrueba2 (name) VALUES ('Alberto')");
//await BeeORM.query("SELECT * FROM usersPrueba");
//await BeeORM.StartupQuery();
//const catsRepo = await BeeORM.BeeFactory(Gato);
//catsRepo.getNameTable();
//catsRepo.getAll();
//console.log(catsRepo.getPrimaryKey());
//const gato = new Gato();
//gato.raza = "Mishi";
//gato.gato_id = 80;
//gato.nombre = "Mishi";
//const data = await catsRepo.getById(24);
//console.log("CAT:",data);
//const entities: Array<Gato> = await catsRepo.getAll();
//console.log(entities);