feat: Agregar modulo de Sistema (mongosh, logs, export/import)

This commit is contained in:
2026-08-12 03:17:28 +00:00
parent e038c25e9e
commit 205c8f1c7c
6 changed files with 450 additions and 3 deletions
+65
View File
@@ -993,3 +993,68 @@ export async function setValue(key, value) {
memStore.kv[key] = value;
}
}
export function getDb() {
return db;
}
export async function exportAllData() {
const collections = [
'usuarios', 'pacientes', 'areas', 'camas', 'internaciones',
'evoluciones', 'laboratorios', 'glucemias', 'acidosbase',
'cultivos', 'estudiosComplementarios', 'interconsultas', 'atb',
'indicaciones', 'movimientos_indicaciones', 'pendientes', 'kv'
];
const dump = {};
if (db) {
for (const name of collections) {
const docs = await db.collection(name).find().toArray();
dump[name] = docs;
}
} else {
for (const name of collections) {
dump[name] = [...(memStore[name] || [])];
}
}
return dump;
}
export async function importAllData(dump) {
const collections = [
'usuarios', 'pacientes', 'areas', 'camas', 'internaciones',
'evoluciones', 'laboratorios', 'glucemias', 'acidosbase',
'cultivos', 'estudiosComplementarios', 'interconsultas', 'atb',
'indicaciones', 'movimientos_indicaciones', 'pendientes', 'kv'
];
if (db) {
for (const name of collections) {
if (Array.isArray(dump[name])) {
try {
await db.collection(name).deleteMany({});
} catch (e) {
console.warn(`Could not clear collection ${name}:`, e);
}
if (dump[name].length > 0) {
const cleaned = dump[name].map(doc => {
const copy = { ...doc };
if (copy._id) {
if (ObjectId.isValid(copy._id)) {
copy._id = new ObjectId(copy._id);
} else {
delete copy._id;
}
}
return copy;
});
await db.collection(name).insertMany(cleaned);
}
}
}
} else {
for (const name of collections) {
if (Array.isArray(dump[name])) {
memStore[name] = [...dump[name]];
}
}
}
}