Local Development Setup Guide
Local Development Setup Guide
Section titled “Local Development Setup Guide”This guide will help you set up a complete local development environment for the Edge MySQL Gateway project.
Prerequisites
Section titled “Prerequisites”Before starting, ensure you have the following installed:
- Node.js (v18 or later) with npm
- Git for version control
- Wrangler CLI for Cloudflare Workers development
- Python 3.8+ (for SDK development and security scanning)
Install Required Tools
Section titled “Install Required Tools”# Install Node.js (if not already installed)# Visit https://nodejs.org/ or use a version manager like nvm
# Install Wrangler CLI globallynpm install -g wrangler
# Verify installationsnode --versionnpm --versionwrangler --version
Project Setup
Section titled “Project Setup”1. Clone and Install Dependencies
Section titled “1. Clone and Install Dependencies”# Clone the repositorygit clone https://github.com/healthfees-org/workersql.gitcd workersql
# Install Node.js dependenciesnpm install
# Set up Python virtual environment (for development tools)python -m venv .venv
# Activate virtual environment# On Windows:.venv\Scripts\activate# On macOS/Linux:source .venv/bin/activate
# Install Python dependenciespip install -r requirements-dev.txt
2. Configure Wrangler
Section titled “2. Configure Wrangler”# Authenticate with Cloudflare (required for deployment)wrangler auth login
# Copy the wrangler configuration templatecp wrangler.toml.template wrangler.toml
3. Set Up Cloudflare Resources
Section titled “3. Set Up Cloudflare Resources”Create KV Namespace for Caching
Section titled “Create KV Namespace for Caching”# Create the main KV namespacewrangler kv:namespace create "APP_CACHE"
# Create a preview namespace for developmentwrangler kv:namespace create "APP_CACHE" --preview
# Note the namespace IDs and update wrangler.toml
Create Queue for Event Processing
Section titled “Create Queue for Event Processing”# Create the main event queuewrangler queues create db-events
# Create dead letter queue for failed eventswrangler queues create db-events-dlq
Create D1 Database (Optional)
Section titled “Create D1 Database (Optional)”# Create D1 database for portable data mirrorwrangler d1 create portable-mirror
# Note the database ID and update wrangler.toml
4. Update Configuration
Section titled “4. Update Configuration”Edit wrangler.toml
with the IDs from the previous step:
[[kv_namespaces]]binding = "APP_CACHE"id = "YOUR_KV_NAMESPACE_ID"preview_id = "YOUR_PREVIEW_NAMESPACE_ID"
[[d1_databases]]binding = "PORTABLE_DB"database_name = "portable-mirror"database_id = "YOUR_D1_DATABASE_ID"
5. Set Development Secrets
Section titled “5. Set Development Secrets”# Set JWT secret for authenticationecho "dev-jwt-secret-$(date +%s)" | wrangler secret put JWT_SECRET
# Set database encryption keyecho "dev-encryption-key-$(date +%s)" | wrangler secret put DATABASE_ENCRYPTION_KEY
# Set admin API keyecho "dev-admin-key-$(date +%s)" | wrangler secret put ADMIN_API_KEY
Development Workflow
Section titled “Development Workflow”1. Build the Project
Section titled “1. Build the Project”# TypeScript compilationnpm run build
# Verify the build succeededls -la dist/
2. Run Tests
Section titled “2. Run Tests”# Run all testsnpm test
# Run tests with coveragenpm run test:coverage
# Run specific test suitesnpm run test:unitnpm run test:integration
# Watch mode for developmentnpm run test:watch
3. Code Quality Checks
Section titled “3. Code Quality Checks”# Run lintingnpm run lint
# Fix linting issues automaticallynpm run lint:fix
# Check code formattingnpm run format:check
# Format code automaticallynpm run format
# Run all quality checksnpm run workflow:check
4. Local Development Server
Section titled “4. Local Development Server”# Start local development servernpm run dev
# This will start Wrangler in development mode with hot reload# The server will be available at http://localhost:8787
5. Pre-commit Hooks
Section titled “5. Pre-commit Hooks”The project uses pre-commit hooks to ensure code quality:
# Install pre-commit hooks (run once)npm run prepare
# Test pre-commit hooks manuallypre-commit run --all-files
Testing Your Setup
Section titled “Testing Your Setup”1. Health Check
Section titled “1. Health Check”Test that your local server is working:
# Start the development servernpm run dev
# In another terminal, test the health endpointcurl http://localhost:8787/health
2. SQL Query Test
Section titled “2. SQL Query Test”Test a simple SQL query:
curl -X POST http://localhost:8787/sql \ -H "Content-Type: application/json" \ -H "Authorization: Bearer test-token-12345" \ -d '{ "sql": "SELECT 1 as test", "params": [] }'
3. Cache Test
Section titled “3. Cache Test”Test the caching functionality:
# First request (should miss cache)curl -X POST http://localhost:8787/sql \ -H "Content-Type: application/json" \ -H "Authorization: Bearer test-token-12345" \ -d '{ "sql": "SELECT * FROM users LIMIT 10", "params": [] }'
# Second request (should hit cache)curl -X POST http://localhost:8787/sql \ -H "Content-Type: application/json" \ -H "Authorization: Bearer test-token-12345" \ -d '{ "sql": "SELECT * FROM users LIMIT 10", "params": [] }'
Environment Variables
Section titled “Environment Variables”For local development, you can create a .env.local
file:
# Create local environment filecat > .env.local << EOFENVIRONMENT=developmentLOG_LEVEL=debugMAX_SHARD_SIZE_GB=1CACHE_TTL_MS=5000CACHE_SWR_MS=10000SHARD_COUNT=4EOF
Debugging
Section titled “Debugging”1. Enable Debug Logging
Section titled “1. Enable Debug Logging”Set LOG_LEVEL=debug
in your wrangler.toml
:
[env.development.vars]LOG_LEVEL = "debug"
2. Inspect KV Store
Section titled “2. Inspect KV Store”# List all keys in your KV namespacewrangler kv:key list --binding APP_CACHE
# Get a specific key valuewrangler kv:key get "cache-key" --binding APP_CACHE
# Delete a keywrangler kv:key delete "cache-key" --binding APP_CACHE
3. Monitor Queue Messages
Section titled “3. Monitor Queue Messages”# View queue consumer logswrangler tail --format pretty
# Send test message to queuewrangler queues producer send db-events '{"type":"test","data":"hello"}'
4. D1 Database Operations
Section titled “4. D1 Database Operations”# Execute SQL on D1 databasewrangler d1 execute portable-mirror --command "SELECT 1"
# Import SQL filewrangler d1 execute portable-mirror --file schema.sql
# Export databasewrangler d1 export portable-mirror --output backup.sql
IDE Setup
Section titled “IDE Setup”VS Code Configuration
Section titled “VS Code Configuration”Create .vscode/settings.json
:
{ "typescript.preferences.importModuleSpecifier": "relative", "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, "eslint.workingDirectories": ["src", "tests"], "files.exclude": { "**/node_modules": true, "**/dist": true, "**/.git": true }}
Recommended Extensions
Section titled “Recommended Extensions”- TypeScript and JavaScript Language Features
- ESLint
- Prettier - Code formatter
- Cloudflare Workers
- Thunder Client (for API testing)
Troubleshooting
Section titled “Troubleshooting”Common Issues
Section titled “Common Issues”-
“wrangler not found”
Terminal window npm install -g wrangler -
“Authentication required”
Terminal window wrangler auth login -
“KV namespace not found”
- Check that you’ve created the KV namespace
- Verify the namespace ID in
wrangler.toml
-
“Build failed”
Terminal window # Clean and rebuildrm -rf dist node_modulesnpm installnpm run build -
Tests failing
Terminal window # Check if Miniflare is properly configurednpm run test:unit -- --verbose
Getting Help
Section titled “Getting Help”- Check the Cloudflare Workers documentation
- Review the Wrangler CLI documentation
- Look at existing tests in the
tests/
directory for examples - Check the project’s GitHub issues for known problems
Next Steps
Section titled “Next Steps”Once your local environment is set up:
- Explore the Architecture: Review
docs/architecture.md
- Run the Test Suite: Ensure all tests pass with
npm test
- Make Changes: Edit code in the
src/
directory - Test Changes: Use
npm run dev
and test your changes - Submit PRs: Follow the contribution guidelines
Production Deployment
Section titled “Production Deployment”When ready to deploy:
# Deploy to stagingwrangler deploy --env staging
# Deploy to productionwrangler deploy --env production
Remember to set production secrets and update configuration for production workloads.