99 lines
3.2 KiB
JavaScript
99 lines
3.2 KiB
JavaScript
#!/usr/bin/env node
|
||
|
||
// Production database seeder script
|
||
// This script seeds the production CouchDB with default admin user
|
||
|
||
import { readFileSync } from 'fs';
|
||
import { resolve, dirname } from 'path';
|
||
import { fileURLToPath } from 'url';
|
||
|
||
const __filename = fileURLToPath(import.meta.url);
|
||
const __dirname = dirname(__filename);
|
||
const projectDir = resolve(__dirname, '..');
|
||
|
||
console.warn('🌱 Starting production database seeding...');
|
||
|
||
// Load environment variables from .env file if it exists
|
||
try {
|
||
const envFile = resolve(projectDir, '.env');
|
||
const envContent = readFileSync(envFile, 'utf8');
|
||
|
||
console.warn('📄 Loading environment variables from .env file...');
|
||
|
||
envContent.split('\n').forEach(line => {
|
||
const trimmed = line.trim();
|
||
if (trimmed && !trimmed.startsWith('#') && trimmed.includes('=')) {
|
||
const [key, ...valueParts] = trimmed.split('=');
|
||
const value = valueParts.join('=');
|
||
if (!process.env[key]) {
|
||
process.env[key] = value;
|
||
}
|
||
}
|
||
});
|
||
} catch (_error) {
|
||
console.warn(
|
||
'ℹ️ No .env file found, using environment variables or defaults'
|
||
);
|
||
}
|
||
|
||
// Use environment variables with fallbacks
|
||
const COUCHDB_URL =
|
||
process.env.VITE_COUCHDB_URL ||
|
||
process.env.COUCHDB_URL ||
|
||
'http://localhost:5984';
|
||
const COUCHDB_USER =
|
||
process.env.VITE_COUCHDB_USER || process.env.COUCHDB_USER || 'admin';
|
||
const COUCHDB_PASSWORD =
|
||
process.env.VITE_COUCHDB_PASSWORD ||
|
||
process.env.COUCHDB_PASSWORD ||
|
||
'change-this-secure-password';
|
||
|
||
// Set environment variables for the seeder to use
|
||
process.env.VITE_COUCHDB_URL = COUCHDB_URL;
|
||
process.env.VITE_COUCHDB_USER = COUCHDB_USER;
|
||
process.env.VITE_COUCHDB_PASSWORD = COUCHDB_PASSWORD;
|
||
|
||
console.warn('🔗 CouchDB Configuration:');
|
||
console.warn(` URL: ${COUCHDB_URL}`);
|
||
console.warn(` User: ${COUCHDB_USER}`);
|
||
console.warn(` Password: ${'*'.repeat(COUCHDB_PASSWORD.length)}`);
|
||
|
||
// Validate required environment variables
|
||
if (!COUCHDB_URL || !COUCHDB_USER || !COUCHDB_PASSWORD) {
|
||
console.error('❌ Missing required environment variables:');
|
||
console.error(' VITE_COUCHDB_URL or COUCHDB_URL');
|
||
console.error(' VITE_COUCHDB_USER or COUCHDB_USER');
|
||
console.error(' VITE_COUCHDB_PASSWORD or COUCHDB_PASSWORD');
|
||
console.error('');
|
||
console.error('💡 Set these in your .env file or as environment variables');
|
||
process.exit(1);
|
||
}
|
||
|
||
async function seedDatabase() {
|
||
try {
|
||
// Import the seeder (this will use the production CouchDB due to env vars)
|
||
const { DatabaseSeeder } = await import('../services/database.seeder.ts');
|
||
|
||
// Wait a bit for databases to be initialized
|
||
console.warn('⏳ Waiting for databases to initialize...');
|
||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||
|
||
const seeder = new DatabaseSeeder();
|
||
|
||
console.warn('📊 Seeding admin user...');
|
||
await seeder.seedDefaultAdmin();
|
||
|
||
console.warn('🎉 Production database seeding completed successfully!');
|
||
console.warn('🔐 You can now login with:');
|
||
console.warn(' Email: admin@localhost');
|
||
console.warn(' Password: change-this-secure-password');
|
||
|
||
process.exit(0);
|
||
} catch (error) {
|
||
console.error('❌ Seeding failed:', error);
|
||
process.exit(1);
|
||
}
|
||
}
|
||
|
||
seedDatabase();
|