191 lines
5.2 KiB
JavaScript
191 lines
5.2 KiB
JavaScript
|
|
#!/usr/bin/env node
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Script de migración: TypeScript → PostgreSQL
|
||
|
|
*
|
||
|
|
* FASE 1: Infraestructura Base
|
||
|
|
*
|
||
|
|
* Este script lee el contenido de src/data/*.ts y lo migra a PostgreSQL
|
||
|
|
*
|
||
|
|
* Uso: node scripts/migrate-content.js
|
||
|
|
*
|
||
|
|
* IMPORTANTE: Ejecutar después de crear el esquema de BD
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { readFile } from 'fs/promises';
|
||
|
|
import { join, dirname } from 'path';
|
||
|
|
import { fileURLToPath } from 'url';
|
||
|
|
import { pool } from '../config/database.js';
|
||
|
|
import dotenv from 'dotenv';
|
||
|
|
|
||
|
|
dotenv.config();
|
||
|
|
|
||
|
|
const __filename = fileURLToPath(import.meta.url);
|
||
|
|
const __dirname = dirname(__filename);
|
||
|
|
const projectRoot = join(__dirname, '..', '..');
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Migrar protocolos desde procedures.ts
|
||
|
|
*/
|
||
|
|
async function migrateProtocols() {
|
||
|
|
try {
|
||
|
|
// Leer procedures.ts dinámicamente
|
||
|
|
const proceduresPath = join(projectRoot, 'src', 'data', 'procedures.ts');
|
||
|
|
const proceduresContent = await readFile(proceduresPath, 'utf-8');
|
||
|
|
|
||
|
|
// Extraer array de procedures (parsing básico)
|
||
|
|
// NOTA: En producción usar parser TypeScript real
|
||
|
|
const proceduresMatch = proceduresContent.match(/export const procedures[^=]*=\s*(\[[\s\S]*?\]);/);
|
||
|
|
|
||
|
|
if (!proceduresMatch) {
|
||
|
|
console.log('⚠️ No se encontraron procedures en procedures.ts');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Evaluar el array (cuidado: solo en desarrollo, en producción usar parser)
|
||
|
|
const procedures = eval(proceduresMatch[1]);
|
||
|
|
|
||
|
|
console.log(`\n📋 Migrando ${procedures.length} protocolos...\n`);
|
||
|
|
|
||
|
|
for (const proc of procedures) {
|
||
|
|
const content = {
|
||
|
|
steps: proc.steps || [],
|
||
|
|
warnings: proc.warnings || [],
|
||
|
|
keyPoints: proc.keyPoints || [],
|
||
|
|
equipment: proc.equipment || [],
|
||
|
|
drugs: proc.drugs || [],
|
||
|
|
};
|
||
|
|
|
||
|
|
await pool.query(
|
||
|
|
`INSERT INTO emerges_content.content_items (
|
||
|
|
id, type, level, title, short_title, content,
|
||
|
|
category, subcategory, priority, age_group,
|
||
|
|
version, status, created_by, updated_by
|
||
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
|
||
|
|
ON CONFLICT (id) DO NOTHING`,
|
||
|
|
[
|
||
|
|
proc.id,
|
||
|
|
'protocol',
|
||
|
|
'operativo',
|
||
|
|
proc.title,
|
||
|
|
proc.shortTitle,
|
||
|
|
JSON.stringify(content),
|
||
|
|
proc.category,
|
||
|
|
proc.subcategory || null,
|
||
|
|
proc.priority,
|
||
|
|
proc.ageGroup,
|
||
|
|
1,
|
||
|
|
'validated', // Asumir validado para contenido migrado
|
||
|
|
'migration-script',
|
||
|
|
'migration-script'
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log(` ✅ ${proc.id}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log(`\n✅ ${procedures.length} protocolos migrados\n`);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ Error migrando protocolos:', error.message);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Migrar fármacos desde drugs.ts
|
||
|
|
*/
|
||
|
|
async function migrateDrugs() {
|
||
|
|
try {
|
||
|
|
const drugsPath = join(projectRoot, 'src', 'data', 'drugs.ts');
|
||
|
|
const drugsContent = await readFile(drugsPath, 'utf-8');
|
||
|
|
|
||
|
|
const drugsMatch = drugsContent.match(/export const drugs[^=]*=\s*(\[[\s\S]*?\]);/);
|
||
|
|
|
||
|
|
if (!drugsMatch) {
|
||
|
|
console.log('⚠️ No se encontraron drugs en drugs.ts');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const drugs = eval(drugsMatch[1]);
|
||
|
|
|
||
|
|
console.log(`\n💊 Migrando ${drugs.length} fármacos...\n`);
|
||
|
|
|
||
|
|
for (const drug of drugs) {
|
||
|
|
const content = {
|
||
|
|
genericName: drug.genericName,
|
||
|
|
tradeName: drug.tradeName,
|
||
|
|
presentation: drug.presentation || '',
|
||
|
|
adultDose: drug.adultDose,
|
||
|
|
pediatricDose: drug.pediatricDose || null,
|
||
|
|
routes: drug.routes || [],
|
||
|
|
dilution: drug.dilution || null,
|
||
|
|
indications: drug.indications || [],
|
||
|
|
contraindications: drug.contraindications || [],
|
||
|
|
sideEffects: drug.sideEffects || [],
|
||
|
|
antidote: drug.antidote || null,
|
||
|
|
notes: drug.notes || [],
|
||
|
|
criticalPoints: drug.criticalPoints || [],
|
||
|
|
source: drug.source || null,
|
||
|
|
};
|
||
|
|
|
||
|
|
await pool.query(
|
||
|
|
`INSERT INTO emerges_content.content_items (
|
||
|
|
id, type, level, title, short_title, content,
|
||
|
|
category, version, status, created_by, updated_by
|
||
|
|
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
||
|
|
ON CONFLICT (id) DO NOTHING`,
|
||
|
|
[
|
||
|
|
drug.id,
|
||
|
|
'drug',
|
||
|
|
'operativo',
|
||
|
|
drug.genericName,
|
||
|
|
drug.tradeName,
|
||
|
|
JSON.stringify(content),
|
||
|
|
drug.category,
|
||
|
|
1,
|
||
|
|
'validated',
|
||
|
|
'migration-script',
|
||
|
|
'migration-script'
|
||
|
|
]
|
||
|
|
);
|
||
|
|
|
||
|
|
console.log(` ✅ ${drug.id}`);
|
||
|
|
}
|
||
|
|
|
||
|
|
console.log(`\n✅ ${drugs.length} fármacos migrados\n`);
|
||
|
|
} catch (error) {
|
||
|
|
console.error('❌ Error migrando fármacos:', error.message);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Función principal
|
||
|
|
*/
|
||
|
|
async function main() {
|
||
|
|
try {
|
||
|
|
console.log('\n🚀 Iniciando migración de contenido...\n');
|
||
|
|
|
||
|
|
// Test de conexión
|
||
|
|
await pool.query('SELECT 1');
|
||
|
|
console.log('✅ Conexión a PostgreSQL establecida\n');
|
||
|
|
|
||
|
|
// Migrar protocolos
|
||
|
|
await migrateProtocols();
|
||
|
|
|
||
|
|
// Migrar fármacos
|
||
|
|
await migrateDrugs();
|
||
|
|
|
||
|
|
console.log('🎉 Migración completada exitosamente\n');
|
||
|
|
|
||
|
|
await pool.end();
|
||
|
|
} catch (error) {
|
||
|
|
console.error('\n❌ Error en migración:', error.message);
|
||
|
|
console.error(error);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
main();
|
||
|
|
|