#!/bin/bash # # Dataflow Setup Script # Quick setup for development # set -e echo "🚀 Dataflow Setup" echo "=================" echo "" # Load .env if it exists if [ -f .env ]; then export $(cat .env | grep -v '^#' | xargs) fi # Prompt for admin credentials echo "📋 PostgreSQL Admin Credentials" echo " (Used to create user and database)" echo "" read -p "Admin username [postgres]: " ADMIN_USER ADMIN_USER=${ADMIN_USER:-postgres} read -s -p "Admin password: " ADMIN_PASS echo "" echo "" # Prompt for app user and database echo "📋 Application Credentials" echo "" read -p "App username [dataflow]: " APP_USER APP_USER=${APP_USER:-dataflow} read -s -p "App password: " APP_PASS echo "" read -p "Database name [dataflow]: " DB_NAME DB_NAME=${DB_NAME:-dataflow} read -p "Database host [localhost]: " DB_HOST DB_HOST=${DB_HOST:-localhost} read -p "Database port [5432]: " DB_PORT DB_PORT=${DB_PORT:-5432} echo "" # Prompt for API configuration echo "📋 API Configuration" echo "" read -p "API port [3000]: " API_PORT API_PORT=${API_PORT:-3000} read -p "Node environment [development]: " NODE_ENV NODE_ENV=${NODE_ENV:-development} echo "" # Install dependencies echo "" echo "📦 Installing Node.js dependencies..." npm install echo "" # Test admin connection echo "🔍 Testing PostgreSQL admin connection..." export PGPASSWORD="$ADMIN_PASS" if psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c '\q' 2>/dev/null; then echo "✓ PostgreSQL connection successful" else echo "✗ Cannot connect to PostgreSQL" echo " Please check your admin credentials" exit 1 fi # Create user if it doesn't exist echo "" echo "👤 Creating PostgreSQL user..." if psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -tAc "SELECT rolname FROM pg_roles WHERE rolname='$APP_USER'" | grep -q "^$APP_USER$"; then echo "✓ User '$APP_USER' already exists" else psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c "CREATE USER $APP_USER WITH PASSWORD '$APP_PASS';" echo "✓ User '$APP_USER' created" fi # Create database if it doesn't exist echo "" echo "🗄️ Creating database..." if psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -lqt | cut -d \| -f 1 | grep -qw "$DB_NAME"; then echo "✓ Database '$DB_NAME' exists" else psql -U "$ADMIN_USER" -h "$DB_HOST" -p "$DB_PORT" -d postgres -c "CREATE DATABASE $DB_NAME OWNER $APP_USER;" echo "✓ Database '$DB_NAME' created" fi unset PGPASSWORD # Set password for app user export PGPASSWORD="$APP_PASS" # Save credentials to .env echo "" echo "💾 Saving credentials to .env..." cat > .env << EOF # Database Configuration DB_HOST=$DB_HOST DB_PORT=$DB_PORT DB_NAME=$DB_NAME DB_USER=$APP_USER DB_PASSWORD=$APP_PASS # API Configuration API_PORT=$API_PORT NODE_ENV=$NODE_ENV EOF echo "✓ Credentials saved" # Deploy schema echo "" echo "📋 Deploying database schema..." psql -U "$APP_USER" -h "$DB_HOST" -p "$DB_PORT" -d "$DB_NAME" -f database/schema.sql echo "✓ Schema deployed" echo "" echo "⚙️ Deploying database functions..." psql -U "$APP_USER" -h "$DB_HOST" -p "$DB_PORT" -d "$DB_NAME" -f database/functions.sql echo "✓ Functions deployed" echo "" echo "✅ Setup complete!" echo "" echo "Next steps:" echo " 1. Start the server: npm start" echo " 2. Test the API: curl http://localhost:$API_PORT/health" echo " 3. Follow the guide: examples/GETTING_STARTED.md" echo ""