feat: Agregar modulo de Sistema (mongosh, logs, export/import)
This commit is contained in:
@@ -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]];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user