- Crear server/db-mariadb.js con conexión a MariaDB usando los datos proporcionados - Crear server/api-mariadb.js con endpoints individuales para todas las entidades - Crear server/migrate-to-mariadb.js para migrar datos de SQLite a MariaDB - Todas las operaciones CRUD disponibles via API REST
87 lines
2.6 KiB
JavaScript
87 lines
2.6 KiB
JavaScript
import Database from 'better-sqlite3';
|
|
import { fileURLToPath } from 'url';
|
|
import { dirname, join } from 'path';
|
|
import mariadb from 'mariadb';
|
|
|
|
const __dirname = dirname(fileURLToPath(import.meta.url));
|
|
const SQLITE_PATH = join(__dirname, 'data', 'hospital.db');
|
|
|
|
// MariaDB connection config
|
|
const mariaPool = mariadb.createPool({
|
|
host: process.env.DB_HOST || '10.9.8.135',
|
|
port: parseInt(process.env.DB_PORT || '3306'),
|
|
user: process.env.DB_USER || 'snlavaise',
|
|
password: process.env.DB_PASSWORD || '1123581321Rtaylor%_+',
|
|
database: process.env.DB_NAME || 'santojanni',
|
|
connectionLimit: 5,
|
|
});
|
|
|
|
async function migrate() {
|
|
console.log('Starting migration from SQLite to MariaDB...');
|
|
|
|
// Open SQLite database
|
|
const sqliteDb = new Database(SQLITE_PATH);
|
|
console.log('Connected to SQLite database');
|
|
|
|
const mariaConn = await mariaPool.getConnection();
|
|
console.log('Connected to MariaDB');
|
|
|
|
try {
|
|
// Migrate each table
|
|
const tables = [
|
|
'usuarios', 'pacientes', 'areas', 'camas', 'internaciones',
|
|
'evoluciones', 'laboratorios', 'acidosbase', 'cultivos',
|
|
'estudiosComplementarios', 'interconsultas', 'atb',
|
|
'indicaciones', 'movimientos_indicaciones', 'kv'
|
|
];
|
|
|
|
for (const table of tables) {
|
|
console.log(`Migrating ${table}...`);
|
|
|
|
// Get all data from SQLite
|
|
const rows = sqliteDb.prepare(`SELECT * FROM ${table}`).all();
|
|
console.log(` Found ${rows.length} rows in ${table}`);
|
|
|
|
if (rows.length === 0) continue;
|
|
|
|
// Get column names from first row
|
|
const columns = Object.keys(rows[0]);
|
|
const placeholders = columns.map(() => '?').join(', ');
|
|
const columnNames = columns.map(c => `\`${c}\``).join(', ');
|
|
|
|
// Insert into MariaDB
|
|
for (const row of rows) {
|
|
const values = columns.map(col => {
|
|
const val = row[col];
|
|
// Handle JSON strings or special cases
|
|
if (typeof val === 'object' && val !== null) {
|
|
return JSON.stringify(val);
|
|
}
|
|
return val;
|
|
});
|
|
|
|
try {
|
|
await mariaConn.query(
|
|
`INSERT IGNORE INTO ${table} (${columnNames}) VALUES (${placeholders})`,
|
|
values
|
|
);
|
|
} catch (err) {
|
|
console.error(` Error inserting row in ${table}:`, err.message);
|
|
}
|
|
}
|
|
|
|
console.log(` Migrated ${rows.length} rows to ${table}`);
|
|
}
|
|
|
|
console.log('Migration completed successfully!');
|
|
} catch (err) {
|
|
console.error('Migration failed:', err);
|
|
} finally {
|
|
sqliteDb.close();
|
|
mariaConn.release();
|
|
mariaPool.end();
|
|
}
|
|
}
|
|
|
|
migrate();
|