d1-sync-local
Version:
Sync Cloudflare D1 remote databases to local development environment
160 lines (115 loc) âĸ 3.89 kB
Markdown
# d1-sync-local
Sync Cloudflare D1 remote databases to your local development environment with ease.
## Features
- đ **Auto-detection**: Automatically reads `wrangler.toml` to 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
```bash
npm install -g d1-sync-local
```
Or use directly with npx:
```bash
npx d1-sync-local
```
## Usage
Navigate to your Cloudflare Workers project directory (where `wrangler.toml` is located) and run:
```bash
d1-sync-local
```
Or use the shorter alias:
```bash
d1sl
```
The tool will:
1. Read your `wrangler.toml` configuration
2. Show all available D1 databases
3. Let you select which remote database to sync from
4. Sync the data to your local development database
5. 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
1. **Reads wrangler.toml**: Automatically detects all D1 database configurations
2. **Exports remote data**: Uses `wrangler d1 export` to get remote database data
3. **Fixes SQL format**: Handles D1 export format issues (missing quotes, etc.)
4. **Cleans local database**: Removes existing local database files
5. **Imports data**: Creates fresh local database with remote data
6. **Validates**: Shows imported tables and record counts
## Example
```bash
$ 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:
```toml
# 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:
```bash
# 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-local
```
## Troubleshooting
### "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