Skip to content

Commit c38b789

Browse files
committed
feat: update backend configuration and database models for Supabase integration
1 parent 3218b17 commit c38b789

File tree

8 files changed

+347
-149
lines changed

8 files changed

+347
-149
lines changed

backend/package-lock.json

Lines changed: 153 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@
55
"main": "src/server.js",
66
"scripts": {
77
"start": "node src/server.js",
8-
"dev": "nodemon src/server.js"
8+
"dev": "nodemon src/server.js",
9+
"init-db": "node init-db.js",
10+
"clean-db": "node clean-database.js"
911
},
1012
"dependencies": {
13+
"@supabase/supabase-js": "^2.39.7",
1114
"axios": "^1.6.2",
1215
"cors": "^2.8.5",
1316
"dotenv": "^16.3.1",
14-
"express": "^4.18.2",
15-
"mysql2": "^3.11.5",
16-
"sequelize": "^6.37.5"
17+
"express": "^4.18.2"
1718
},
1819
"devDependencies": {
1920
"nodemon": "^3.0.2"

backend/src/config/database.js

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,29 @@
1-
const { Sequelize } = require('sequelize');
1+
const { createClient } = require('@supabase/supabase-js');
22
require('dotenv').config();
33

4-
let sequelize;
4+
const SUPABASE_URL = process.env.SUPABASE_URL;
5+
const SUPABASE_KEY = process.env.SUPABASE_KEY;
56

6-
if (process.env.DATABASE_URL) {
7-
// Production configuration for Railway
8-
sequelize = new Sequelize(process.env.DATABASE_URL, {
9-
dialect: 'mysql',
10-
dialectOptions: {
11-
ssl: {
12-
require: true,
13-
rejectUnauthorized: false
14-
}
15-
},
16-
logging: false
17-
});
18-
} else {
19-
// Local configuration
20-
sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, {
21-
host: process.env.DB_HOST,
22-
dialect: 'mysql',
23-
logging: console.log,
24-
pool: {
25-
max: 5,
26-
min: 0,
27-
acquire: 30000,
28-
idle: 10000
29-
}
30-
});
7+
if (!SUPABASE_URL || !SUPABASE_KEY) {
8+
console.warn('SUPABASE_URL or SUPABASE_KEY environment variables are not set. Some features will be limited.');
319
}
3210

11+
const supabase = createClient(
12+
SUPABASE_URL || 'https://example.supabase.co',
13+
SUPABASE_KEY || 'demo-key'
14+
);
15+
3316
// Test the connection
34-
sequelize.authenticate()
35-
.then(() => {
36-
console.log('Database connection has been established successfully.');
17+
supabase.from('stocks').select('count', { count: 'exact', head: true })
18+
.then(({ count, error }) => {
19+
if (error) {
20+
console.error('Unable to connect to Supabase:', error);
21+
} else {
22+
console.log('Supabase connection has been established successfully.');
23+
}
3724
})
3825
.catch(err => {
39-
console.error('Unable to connect to the database:', err);
26+
console.error('Unable to connect to Supabase:', err);
4027
});
4128

42-
module.exports = sequelize;
29+
module.exports = supabase;

backend/src/config/finnhub.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ require('dotenv').config();
33
const FINNHUB_API_KEY = process.env.FINNHUB_API_KEY;
44

55
if (!FINNHUB_API_KEY) {
6-
console.error('FINNHUB_API_KEY environment variable is not set');
7-
process.exit(1);
6+
console.warn('FINNHUB_API_KEY environment variable is not set. Some features will be limited.');
7+
// We'll continue running but with limited functionality
88
}
99

1010
module.exports = {
11-
API_KEY: FINNHUB_API_KEY,
11+
API_KEY: FINNHUB_API_KEY || 'demo', // Use 'demo' as a fallback
1212
BASE_URL: 'https://finnhub.io/api/v1'
1313
};

backend/src/migrations/20240115_create_watchlist.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
// This migration file is for Sequelize and is not used with Supabase
2+
// For Supabase, tables are created through the Supabase dashboard or SQL migrations
3+
// See supabase-schema.sql for the actual table creation
4+
5+
/*
16
const { DataTypes } = require('sequelize');
27
38
module.exports = {
@@ -44,4 +49,15 @@ module.exports = {
4449
down: async (queryInterface, Sequelize) => {
4550
await queryInterface.dropTable('watchlists');
4651
}
52+
};
53+
*/
54+
55+
// Supabase migration placeholder
56+
module.exports = {
57+
up: async () => {
58+
console.log('Migration not needed for Supabase - tables are managed separately');
59+
},
60+
down: async () => {
61+
console.log('Migration not needed for Supabase - tables are managed separately');
62+
}
4763
};

backend/src/migrations/init.js

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,13 @@
1-
const Stock = require('../models/Stock');
2-
const sequelize = require('../config/database');
1+
const supabase = require('../config/supabase');
32

43
async function initializeDatabase() {
54
try {
6-
// Sync all models with the database
7-
await sequelize.sync({ force: true });
8-
console.log('Database tables created successfully');
9-
10-
// Create some initial stocks for testing
11-
await Stock.bulkCreate([
12-
{
13-
name: 'Apple Inc.',
14-
ticker: 'AAPL',
15-
shares: 10,
16-
buy_price: 150.00,
17-
current_price: 155.00,
18-
target_price: 180.00,
19-
is_in_watchlist: false
20-
},
21-
{
22-
name: 'Microsoft Corporation',
23-
ticker: 'MSFT',
24-
shares: 5,
25-
buy_price: 280.00,
26-
current_price: 290.00,
27-
target_price: 320.00,
28-
is_in_watchlist: false
29-
}
30-
]);
31-
console.log('Initial stocks created successfully');
5+
console.log('Database initialization not needed for Supabase - tables are managed separately');
6+
7+
// Note: For Supabase, tables are created through the Supabase dashboard or SQL migrations
8+
// This function is kept for compatibility but doesn't need to do anything
9+
10+
console.log('Database initialization completed');
3211

3312
} catch (error) {
3413
console.error('Error initializing database:', error);

0 commit comments

Comments
 (0)