Package Exports
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (d1-sync-local) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
d1-sync-local
Sync Cloudflare D1 remote databases to your local development environment with ease.
Features
- đ Auto-detection: Automatically reads
wrangler.tomlto find all D1 databases - đ Environment support: Sync from any environment (dev, staging, production)
- đĄī¸ Safe operation: Confirms before overwriting local data
- đ Validation: Shows synced tables and record counts
- đ¨ Beautiful CLI: Colored output with progress indicators
- đ Multi-language: Auto-detects language (English, įŽäŊ䏿, įšéĢ䏿, æĨæŦčĒ)
Installation
npm install -g d1-sync-localOr use directly with npx:
npx d1-sync-localUsage
Navigate to your Cloudflare Workers project directory (where wrangler.toml is located) and run:
d1-sync-localOr use the shorter alias:
d1slThe tool will:
- Read your
wrangler.tomlconfiguration - Show all available D1 databases
- Let you select which remote database to sync from
- Sync the data to your local development database
- Validate and show the results
Requirements
- Node.js >= 14.0.0
- Wrangler CLI installed (
npm install -g wrangler) - A Cloudflare Workers project with D1 database configuration
How it works
- Reads wrangler.toml: Automatically detects all D1 database configurations
- Exports remote data: Uses
wrangler d1 exportto get remote database data - Fixes SQL format: Handles D1 export format issues (missing quotes, etc.)
- Cleans local database: Removes existing local database files
- Imports data: Creates fresh local database with remote data
- Validates: Shows imported tables and record counts
Example
$ d1-sync-local
đ D1 Sync to Local
â Found wrangler.toml
Local database: my-app-db
? Select source database to sync from:
⯠Local/Default (my-app-db)
Staging (my-app-db-staging)
Production (my-app-db-production)
â ī¸ This will sync my-app-db-staging to local database
? All local data will be replaced. Continue? (y/N)
â Export completed
â Cleaned local database (3 files)
â Export file processed
â Import completed
đ Synced 6 tables:
â users: 150 records
â products: 89 records
â orders: 1203 records
â categories: 12 records
â sessions: 567 records
â logs: 4521 records
⨠Sync completed successfully!Configuration
The tool reads your wrangler.toml file to find D1 databases:
# Default/Local database
[[d1_databases]]
binding = "DB"
database_name = "my-app-db"
database_id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
# Environment-specific databases
[env.staging]
[[env.staging.d1_databases]]
binding = "DB"
database_name = "my-app-db-staging"
database_id = "yyyyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy"
[env.production]
[[env.production.d1_databases]]
binding = "DB"
database_name = "my-app-db-production"
database_id = "zzzzzzzz-zzzz-zzzz-zzzz-zzzzzzzzzzzz"Language Support
The tool automatically detects your system language and displays messages in:
- English (default)
- įŽäŊ䏿 (Simplified Chinese)
- įšéĢ䏿 (Traditional Chinese)
- æĨæŦčĒ (Japanese)
Manual Language Selection
You can override the auto-detected language using the D1_SYNC_LANG environment variable:
# Use Simplified Chinese
D1_SYNC_LANG=zh-cn d1-sync-local
# Use Traditional Chinese
D1_SYNC_LANG=zh-tw d1-sync-local
# Use Japanese
D1_SYNC_LANG=ja d1-sync-local
# Use English
D1_SYNC_LANG=en d1-sync-localTroubleshooting
"wrangler.toml not found"
Make sure you run the command in your Cloudflare Workers project directory.
"No D1 databases found"
Check that your wrangler.toml has D1 database configurations.
Permission errors
Make sure you have the necessary permissions to access the remote databases.
License
MIT