-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-setup.js
More file actions
191 lines (159 loc) ยท 6.97 KB
/
database-setup.js
File metadata and controls
191 lines (159 loc) ยท 6.97 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/env node
const { execSync } = require('child_process');
const fs = require('fs');
const path = require('path');
const readline = require('readline');
console.log('๐ Newsletter Application Database Setup\n');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const question = (query) => new Promise(resolve => rl.question(query, resolve));
// Function to run commands
const runCommand = (command, cwd = process.cwd()) => {
try {
console.log(`Running: ${command}`);
execSync(command, { cwd, stdio: 'inherit' });
return true;
} catch (error) {
console.error(`Failed to execute: ${command}`);
return false;
}
};
// Function to create .env file
const createEnvFile = (databaseType, config) => {
const envPath = path.join(process.cwd(), 'backend', '.env');
let envContent = `# Server Configuration
PORT=5050
NODE_ENV=development
CLIENT_URL=http://localhost:3030
# Database Configuration
DATABASE_TYPE=${databaseType}
`;
if (databaseType === 'mongodb') {
envContent += `# MongoDB Configuration
MONGODB_URI=${config.mongoUri || 'mongodb://localhost:27017/newsletter'}
`;
} else if (databaseType === 'postgresql') {
envContent += `# PostgreSQL Configuration
POSTGRES_HOST=${config.host || 'localhost'}
POSTGRES_PORT=${config.port || '5432'}
POSTGRES_DB=${config.database || 'newsletter'}
POSTGRES_USER=${config.username || 'newsletter_user'}
POSTGRES_PASSWORD=${config.password || 'newsletter_password'}
`;
}
envContent += `# JWT Secret
JWT_SECRET=your-super-secret-jwt-key-change-this-in-production
# Email Configuration (Optional - for sending newsletters)
SMTP_HOST=smtp.gmail.com
SMTP_PORT=587
SMTP_USER=your-email@gmail.com
SMTP_PASS=your-app-password
FROM_EMAIL=your-email@gmail.com`;
fs.writeFileSync(envPath, envContent);
console.log('โ
Created .env file');
};
// Main setup function
const setup = async () => {
try {
console.log('๐ฆ Installing dependencies...\n');
// Install root dependencies
if (!runCommand('npm install')) {
console.error('โ Failed to install root dependencies');
return;
}
// Install backend dependencies
console.log('\n๐ฆ Installing backend dependencies...');
if (!runCommand('npm install', path.join(process.cwd(), 'backend'))) {
console.error('โ Failed to install backend dependencies');
return;
}
// Install frontend dependencies
console.log('\n๐ฆ Installing frontend dependencies...');
if (!runCommand('npm install', path.join(process.cwd(), 'frontend'))) {
console.error('โ Failed to install frontend dependencies');
return;
}
console.log('\n๐๏ธ Database Configuration\n');
console.log('Choose your preferred database:');
console.log('1. MongoDB (NoSQL Document Database)');
console.log('2. PostgreSQL (SQL Relational Database)');
const dbChoice = await question('\nEnter your choice (1 or 2): ');
let databaseType, config = {};
if (dbChoice === '1') {
databaseType = 'mongodb';
console.log('\n๐ MongoDB Configuration');
console.log('1. Local MongoDB (mongodb://localhost:27017/newsletter)');
console.log('2. MongoDB Atlas (cloud)');
console.log('3. Custom connection string');
const mongoChoice = await question('\nChoose MongoDB option (1, 2, or 3): ');
if (mongoChoice === '1') {
config.mongoUri = 'mongodb://localhost:27017/newsletter';
console.log('\nโ ๏ธ Make sure MongoDB is installed and running locally.');
console.log(' Install MongoDB: https://www.mongodb.com/try/download/community');
} else if (mongoChoice === '2') {
console.log('\n๐ MongoDB Atlas Setup:');
console.log('1. Go to https://www.mongodb.com/atlas');
console.log('2. Create a free account and cluster');
console.log('3. Create a database user');
console.log('4. Get your connection string');
config.mongoUri = await question('\nEnter your MongoDB Atlas connection string: ');
} else if (mongoChoice === '3') {
config.mongoUri = await question('Enter custom MongoDB connection string: ');
}
} else if (dbChoice === '2') {
databaseType = 'postgresql';
console.log('\n๐ PostgreSQL Configuration');
console.log('1. Local PostgreSQL');
console.log('2. Cloud PostgreSQL (ElephantSQL, Heroku, etc.)');
const pgChoice = await question('\nChoose PostgreSQL option (1 or 2): ');
if (pgChoice === '1') {
console.log('\nโ ๏ธ Make sure PostgreSQL is installed and running locally.');
console.log(' Install PostgreSQL: https://www.postgresql.org/download/');
config.host = 'localhost';
config.port = '5432';
config.database = await question('Database name (default: newsletter): ') || 'newsletter';
config.username = await question('Username (default: newsletter_user): ') || 'newsletter_user';
config.password = await question('Password (default: newsletter_password): ') || 'newsletter_password';
} else if (pgChoice === '2') {
console.log('\n๐ Cloud PostgreSQL Setup:');
config.host = await question('Host: ');
config.port = await question('Port (default: 5432): ') || '5432';
config.database = await question('Database name: ');
config.username = await question('Username: ');
config.password = await question('Password: ');
}
} else {
console.log('โ Invalid choice. Defaulting to MongoDB local.');
databaseType = 'mongodb';
config.mongoUri = 'mongodb://localhost:27017/newsletter';
}
// Create .env file
console.log('\nโ๏ธ Creating environment configuration...');
createEnvFile(databaseType, config);
console.log('\nโ
Setup completed successfully!');
console.log('\n๐ Next steps:');
console.log(`1. Make sure your ${databaseType.toUpperCase()} database is running`);
if (databaseType === 'postgresql') {
console.log('2. Create the database and user if using local PostgreSQL:');
console.log(` CREATE DATABASE ${config.database || 'newsletter'};`);
console.log(` CREATE USER ${config.username || 'newsletter_user'} WITH PASSWORD '${config.password || 'newsletter_password'}';`);
console.log(` GRANT ALL PRIVILEGES ON DATABASE ${config.database || 'newsletter'} TO ${config.username || 'newsletter_user'};`);
}
console.log('3. Run "npm run seed" to add sample data (optional)');
console.log('4. Run "npm run dev" to start the application');
console.log('\n๐ฑ The app will be available at:');
console.log(' Frontend: http://localhost:3030');
console.log(' Backend: http://localhost:5050');
console.log('\n๐ค Default admin credentials (after seeding):');
console.log(' Email: admin@example.com');
console.log(' Password: password123');
} catch (error) {
console.error('โ Setup failed:', error.message);
} finally {
rl.close();
}
};
// Run setup
setup();