Spaces:
Sleeping
Sleeping
Upload db.js
Browse files- server/config/db.js +73 -2
server/config/db.js
CHANGED
|
@@ -17,12 +17,83 @@ const pool = new Pool({
|
|
| 17 |
// Test the connection
|
| 18 |
pool.query('SELECT NOW()', (err, res) => {
|
| 19 |
if (err) {
|
| 20 |
-
console.
|
|
|
|
| 21 |
} else {
|
| 22 |
console.log('Database connected successfully');
|
| 23 |
}
|
| 24 |
});
|
| 25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
module.exports = {
|
| 27 |
-
query: (text, params) =>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
};
|
|
|
|
| 17 |
// Test the connection
|
| 18 |
pool.query('SELECT NOW()', (err, res) => {
|
| 19 |
if (err) {
|
| 20 |
+
console.warn('Database connection warning:', err.stack);
|
| 21 |
+
console.warn('Continuing without database connection - using mock data only');
|
| 22 |
} else {
|
| 23 |
console.log('Database connected successfully');
|
| 24 |
}
|
| 25 |
});
|
| 26 |
|
| 27 |
+
// Mock data for when database is not available
|
| 28 |
+
const mockNews = [
|
| 29 |
+
{
|
| 30 |
+
id: 1,
|
| 31 |
+
title: 'New Cold Chain Initiative Launched in Southeast Asia',
|
| 32 |
+
excerpt: 'Regional partnership aims to reduce post-harvest losses by 30% through solar-powered refrigeration.',
|
| 33 |
+
date: '2025-10-15',
|
| 34 |
+
category: 'Policy'
|
| 35 |
+
},
|
| 36 |
+
{
|
| 37 |
+
id: 2,
|
| 38 |
+
title: 'Innovative Edible Coatings Show Promise in Lab Trials',
|
| 39 |
+
excerpt: 'New biodegradable coatings extend shelf life of fruits by up to 2 weeks.',
|
| 40 |
+
date: '2025-10-10',
|
| 41 |
+
category: 'Technology'
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
id: 3,
|
| 45 |
+
title: 'Global Fund Announces $50M for FLW Reduction Projects',
|
| 46 |
+
excerpt: 'Funding opportunity for pilot projects connecting smallholders to processing facilities.',
|
| 47 |
+
date: '2025-10-05',
|
| 48 |
+
category: 'Finance'
|
| 49 |
+
}
|
| 50 |
+
];
|
| 51 |
+
|
| 52 |
+
const mockDatasets = [
|
| 53 |
+
{
|
| 54 |
+
id: 1,
|
| 55 |
+
title: 'Cold Chain Infrastructure Map',
|
| 56 |
+
description: 'Geospatial data on refrigeration facilities across Sub-Saharan Africa',
|
| 57 |
+
category: 'Infrastructure',
|
| 58 |
+
size: '2.4 GB',
|
| 59 |
+
downloads: 1240
|
| 60 |
+
},
|
| 61 |
+
{
|
| 62 |
+
id: 2,
|
| 63 |
+
title: 'Post-Harvest Loss Hotspots',
|
| 64 |
+
description: 'Identified areas with highest food loss rates in South Asia',
|
| 65 |
+
category: 'Analytics',
|
| 66 |
+
size: '890 MB',
|
| 67 |
+
downloads: 890
|
| 68 |
+
},
|
| 69 |
+
{
|
| 70 |
+
id: 3,
|
| 71 |
+
title: 'Processing Capacity Database',
|
| 72 |
+
description: 'List of food processing facilities with available capacity',
|
| 73 |
+
category: 'Industry',
|
| 74 |
+
size: '1.1 GB',
|
| 75 |
+
downloads: 1560
|
| 76 |
+
}
|
| 77 |
+
];
|
| 78 |
+
|
| 79 |
module.exports = {
|
| 80 |
+
query: (text, params) => {
|
| 81 |
+
// If we're in a Hugging Face Spaces environment, return mock data
|
| 82 |
+
if (process.env.HF_SPACES === 'true') {
|
| 83 |
+
console.log('Using mock data for Hugging Face Spaces environment');
|
| 84 |
+
return {
|
| 85 |
+
rows: text.includes('news') ? mockNews : mockDatasets
|
| 86 |
+
};
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
// Otherwise try to connect to the database
|
| 90 |
+
return pool.query(text, params).catch(err => {
|
| 91 |
+
console.warn('Database query failed, using mock data:', err.message);
|
| 92 |
+
return {
|
| 93 |
+
rows: text.includes('news') ? mockNews : mockDatasets
|
| 94 |
+
};
|
| 95 |
+
});
|
| 96 |
+
},
|
| 97 |
+
mockNews,
|
| 98 |
+
mockDatasets
|
| 99 |
};
|