Skip to content
Merged

test #125

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions front/src/lib/mongodb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ const globalForMongo = globalThis as typeof globalThis & {
//crea el cliente solo si no existe ya (singleton)
async function getClient(): Promise<MongoClient> {
if (!globalForMongo._mongoClient) {
console.log("[mongodb] Connecting to MongoDB...", connectionString?.replace(/\/\/.*@/, "//***@"));
globalForMongo._mongoClient = new MongoClient(connectionString);
await globalForMongo._mongoClient.connect();
console.log("[mongodb] Connected successfully");
}

return globalForMongo._mongoClient;
Expand Down
7 changes: 5 additions & 2 deletions front/src/pods/embalse-search/embalse-search.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Embalse } from "./api/api.model";

export async function getEmbalsesFromDb(): Promise<Embalse[]> {
try {
console.log("[embalse-search] getEmbalsesFromDb: querying MongoDB...");
const db = await getDb();
const docs = await db
.collection("embalses")
Expand All @@ -21,14 +22,16 @@ export async function getEmbalsesFromDb(): Promise<Embalse[]> {
)
.toArray();

console.log(`[embalse-search] getEmbalsesFromDb: found ${docs.length} documents`);
return docs.map((doc) => ({
_id: doc.slug ?? createSlug(doc.nombre ?? ""),
nombre: doc.nombre ?? "",
provincia: doc.provincia ?? "",
}));
} catch {
} catch (error) {
console.warn(
"getEmbalsesFromDb: MongoDB not available (build time?), returning empty array"
"getEmbalsesFromDb: MongoDB not available (build time?), returning empty array.",
"Error:", error instanceof Error ? error.message : error
);
return [];
}
Expand Down