File size: 2,861 Bytes
1fe073f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4538202
 
1fe073f
 
 
 
 
4538202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fe073f
4538202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fe073f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// 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
};