gaialive's picture
Upload db.js
4538202 verified
// Database configuration
const { Pool } = require('pg');
const dotenv = require('dotenv');
dotenv.config();
// For development, we'll use a simple configuration
// In production, you would use environment variables
const pool = new Pool({
user: process.env.DB_USER || 'postgres',
host: process.env.DB_HOST || 'localhost',
database: process.env.DB_NAME || 'bionexus_hub',
password: process.env.DB_PASSWORD || 'postgres',
port: process.env.DB_PORT || 5432,
});
// Test the connection
pool.query('SELECT NOW()', (err, res) => {
if (err) {
console.warn('Database connection warning:', err.stack);
console.warn('Continuing without database connection - using mock data only');
} else {
console.log('Database connected successfully');
}
});
// Mock data for when database is not available
const mockNews = [
{
id: 1,
title: 'New Cold Chain Initiative Launched in Southeast Asia',
excerpt: 'Regional partnership aims to reduce post-harvest losses by 30% through solar-powered refrigeration.',
date: '2025-10-15',
category: 'Policy'
},
{
id: 2,
title: 'Innovative Edible Coatings Show Promise in Lab Trials',
excerpt: 'New biodegradable coatings extend shelf life of fruits by up to 2 weeks.',
date: '2025-10-10',
category: 'Technology'
},
{
id: 3,
title: 'Global Fund Announces $50M for FLW Reduction Projects',
excerpt: 'Funding opportunity for pilot projects connecting smallholders to processing facilities.',
date: '2025-10-05',
category: 'Finance'
}
];
const mockDatasets = [
{
id: 1,
title: 'Cold Chain Infrastructure Map',
description: 'Geospatial data on refrigeration facilities across Sub-Saharan Africa',
category: 'Infrastructure',
size: '2.4 GB',
downloads: 1240
},
{
id: 2,
title: 'Post-Harvest Loss Hotspots',
description: 'Identified areas with highest food loss rates in South Asia',
category: 'Analytics',
size: '890 MB',
downloads: 890
},
{
id: 3,
title: 'Processing Capacity Database',
description: 'List of food processing facilities with available capacity',
category: 'Industry',
size: '1.1 GB',
downloads: 1560
}
];
module.exports = {
query: (text, params) => {
// If we're in a Hugging Face Spaces environment, return mock data
if (process.env.HF_SPACES === 'true') {
console.log('Using mock data for Hugging Face Spaces environment');
return {
rows: text.includes('news') ? mockNews : mockDatasets
};
}
// Otherwise try to connect to the database
return pool.query(text, params).catch(err => {
console.warn('Database query failed, using mock data:', err.message);
return {
rows: text.includes('news') ? mockNews : mockDatasets
};
});
},
mockNews,
mockDatasets
};