Skip to content

Local Development Setup Guide

This guide will help you set up a complete local development environment for the Edge MySQL Gateway project.

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)
Terminal window
# Install Node.js (if not already installed)
# Visit https://nodejs.org/ or use a version manager like nvm
# Install Wrangler CLI globally
npm install -g wrangler
# Verify installations
node --version
npm --version
wrangler --version
Terminal window
# Clone the repository
git clone https://github.com/healthfees-org/workersql.git
cd workersql
# Install Node.js dependencies
npm 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 dependencies
pip install -r requirements-dev.txt
Terminal window
# Authenticate with Cloudflare (required for deployment)
wrangler auth login
# Copy the wrangler configuration template
cp wrangler.toml.template wrangler.toml
Terminal window
# Create the main KV namespace
wrangler kv:namespace create "APP_CACHE"
# Create a preview namespace for development
wrangler kv:namespace create "APP_CACHE" --preview
# Note the namespace IDs and update wrangler.toml
Terminal window
# Create the main event queue
wrangler queues create db-events
# Create dead letter queue for failed events
wrangler queues create db-events-dlq
Terminal window
# Create D1 database for portable data mirror
wrangler d1 create portable-mirror
# Note the database ID and update wrangler.toml

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"
Terminal window
# Set JWT secret for authentication
echo "dev-jwt-secret-$(date +%s)" | wrangler secret put JWT_SECRET
# Set database encryption key
echo "dev-encryption-key-$(date +%s)" | wrangler secret put DATABASE_ENCRYPTION_KEY
# Set admin API key
echo "dev-admin-key-$(date +%s)" | wrangler secret put ADMIN_API_KEY
Terminal window
# TypeScript compilation
npm run build
# Verify the build succeeded
ls -la dist/
Terminal window
# Run all tests
npm test
# Run tests with coverage
npm run test:coverage
# Run specific test suites
npm run test:unit
npm run test:integration
# Watch mode for development
npm run test:watch
Terminal window
# Run linting
npm run lint
# Fix linting issues automatically
npm run lint:fix
# Check code formatting
npm run format:check
# Format code automatically
npm run format
# Run all quality checks
npm run workflow:check
Terminal window
# Start local development server
npm run dev
# This will start Wrangler in development mode with hot reload
# The server will be available at http://localhost:8787

The project uses pre-commit hooks to ensure code quality:

Terminal window
# Install pre-commit hooks (run once)
npm run prepare
# Test pre-commit hooks manually
pre-commit run --all-files

Test that your local server is working:

Terminal window
# Start the development server
npm run dev
# In another terminal, test the health endpoint
curl http://localhost:8787/health

Test a simple SQL query:

Terminal window
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": []
}'

Test the caching functionality:

Terminal window
# 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": []
}'

For local development, you can create a .env.local file:

Terminal window
# Create local environment file
cat > .env.local << EOF
ENVIRONMENT=development
LOG_LEVEL=debug
MAX_SHARD_SIZE_GB=1
CACHE_TTL_MS=5000
CACHE_SWR_MS=10000
SHARD_COUNT=4
EOF

Set LOG_LEVEL=debug in your wrangler.toml:

[env.development.vars]
LOG_LEVEL = "debug"
Terminal window
# List all keys in your KV namespace
wrangler kv:key list --binding APP_CACHE
# Get a specific key value
wrangler kv:key get "cache-key" --binding APP_CACHE
# Delete a key
wrangler kv:key delete "cache-key" --binding APP_CACHE
Terminal window
# View queue consumer logs
wrangler tail --format pretty
# Send test message to queue
wrangler queues producer send db-events '{"type":"test","data":"hello"}'
Terminal window
# Execute SQL on D1 database
wrangler d1 execute portable-mirror --command "SELECT 1"
# Import SQL file
wrangler d1 execute portable-mirror --file schema.sql
# Export database
wrangler d1 export portable-mirror --output backup.sql

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
}
}
  • TypeScript and JavaScript Language Features
  • ESLint
  • Prettier - Code formatter
  • Cloudflare Workers
  • Thunder Client (for API testing)
  1. “wrangler not found”

    Terminal window
    npm install -g wrangler
  2. “Authentication required”

    Terminal window
    wrangler auth login
  3. “KV namespace not found”

    • Check that you’ve created the KV namespace
    • Verify the namespace ID in wrangler.toml
  4. “Build failed”

    Terminal window
    # Clean and rebuild
    rm -rf dist node_modules
    npm install
    npm run build
  5. Tests failing

    Terminal window
    # Check if Miniflare is properly configured
    npm run test:unit -- --verbose

Once your local environment is set up:

  1. Explore the Architecture: Review docs/architecture.md
  2. Run the Test Suite: Ensure all tests pass with npm test
  3. Make Changes: Edit code in the src/ directory
  4. Test Changes: Use npm run dev and test your changes
  5. Submit PRs: Follow the contribution guidelines

When ready to deploy:

Terminal window
# Deploy to staging
wrangler deploy --env staging
# Deploy to production
wrangler deploy --env production

Remember to set production secrets and update configuration for production workloads.