Skip to content

Commit 05d4430

Browse files
Copilothotlong
andcommitted
Add comprehensive documentation for sync command
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent 2120fd3 commit 05d4430

2 files changed

Lines changed: 219 additions & 0 deletions

File tree

packages/tools/cli/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,79 @@ objectql migrate status --config ./config/objectql.config.ts
356356
- `-c, --config <path>` - Path to objectql.config.ts/js
357357
- `-d, --dir <path>` - Migrations directory [default: "./migrations"]
358358

359+
#### `sync`
360+
361+
Introspect an existing SQL database and generate ObjectQL `.object.yml` files from the database schema. This is useful for:
362+
- Connecting to an existing/legacy database
363+
- Reverse-engineering database schema to ObjectQL metadata
364+
- Bootstrapping a new ObjectQL project from an existing database
365+
366+
```bash
367+
# Sync all tables from the database
368+
objectql sync
369+
370+
# Sync specific tables only
371+
objectql sync --tables users posts comments
372+
373+
# Custom output directory
374+
objectql sync --output ./src/metadata/objects
375+
376+
# Overwrite existing files
377+
objectql sync --force
378+
379+
# With custom config file
380+
objectql sync --config ./config/objectql.config.ts
381+
```
382+
383+
**Options:**
384+
- `-c, --config <path>` - Path to objectql.config.ts/js
385+
- `-o, --output <path>` - Output directory for .object.yml files [default: "./src/objects"]
386+
- `-t, --tables <tables...>` - Specific tables to sync (default: all tables)
387+
- `-f, --force` - Overwrite existing .object.yml files
388+
389+
**Features:**
390+
- Automatically detects table structure (columns, data types, constraints)
391+
- Maps SQL types to appropriate ObjectQL field types
392+
- Identifies foreign keys and converts them to `lookup` relationships
393+
- Generates human-readable labels from table/column names
394+
- Preserves field constraints (required, unique, maxLength)
395+
- Skips system fields (id, created_at, updated_at) as they're automatic in ObjectQL
396+
397+
**Example:**
398+
399+
Given a database with this table structure:
400+
```sql
401+
CREATE TABLE users (
402+
id VARCHAR PRIMARY KEY,
403+
username VARCHAR UNIQUE NOT NULL,
404+
email VARCHAR NOT NULL,
405+
is_active BOOLEAN DEFAULT true,
406+
created_at TIMESTAMP,
407+
updated_at TIMESTAMP
408+
);
409+
```
410+
411+
Running `objectql sync` generates:
412+
```yaml
413+
# users.object.yml
414+
name: users
415+
label: Users
416+
fields:
417+
username:
418+
type: text
419+
label: Username
420+
required: true
421+
unique: true
422+
email:
423+
type: text
424+
label: Email
425+
required: true
426+
is_active:
427+
type: boolean
428+
label: Is Active
429+
defaultValue: true
430+
```
431+
359432
---
360433
361434
### Development Tools

packages/tools/cli/USAGE_EXAMPLES.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,152 @@ export async function up(app: ObjectQL) {
517517
}
518518
```
519519

520+
### Syncing from Existing Database
521+
522+
The `sync` command introspects an existing SQL database and generates ObjectQL `.object.yml` files.
523+
524+
#### Basic Sync
525+
526+
```bash
527+
# Sync all tables from the database
528+
objectql sync
529+
530+
# Output: src/objects/users.object.yml, src/objects/posts.object.yml, etc.
531+
```
532+
533+
#### Selective Table Sync
534+
535+
```bash
536+
# Sync only specific tables
537+
objectql sync --tables users posts comments
538+
539+
# Custom output directory
540+
objectql sync --output ./src/metadata/objects
541+
```
542+
543+
#### Overwriting Existing Files
544+
545+
```bash
546+
# Force overwrite of existing .object.yml files
547+
objectql sync --force
548+
```
549+
550+
#### Example Workflow: Connecting to Legacy Database
551+
552+
```bash
553+
# 1. Create config file pointing to existing database
554+
cat > objectql.config.ts << 'EOF'
555+
import { ObjectQL } from '@objectql/core';
556+
import { SqlDriver } from '@objectql/driver-sql';
557+
558+
const driver = new SqlDriver({
559+
client: 'postgresql',
560+
connection: {
561+
host: 'localhost',
562+
database: 'legacy_db',
563+
user: 'postgres',
564+
password: 'password'
565+
}
566+
});
567+
568+
export default new ObjectQL({
569+
datasources: { default: driver }
570+
});
571+
EOF
572+
573+
# 2. Introspect and generate object definitions
574+
objectql sync --output ./src/objects
575+
576+
# 3. Review generated files
577+
ls -la ./src/objects/
578+
579+
# Output:
580+
# users.object.yml
581+
# products.object.yml
582+
# orders.object.yml
583+
# order_items.object.yml
584+
585+
# 4. Inspect a generated file
586+
cat ./src/objects/users.object.yml
587+
```
588+
589+
**Generated Output Example:**
590+
591+
```yaml
592+
# users.object.yml
593+
name: users
594+
label: Users
595+
fields:
596+
username:
597+
type: text
598+
label: Username
599+
required: true
600+
unique: true
601+
email:
602+
type: text
603+
label: Email
604+
required: true
605+
first_name:
606+
type: text
607+
label: First Name
608+
last_name:
609+
type: text
610+
label: Last Name
611+
is_active:
612+
type: boolean
613+
label: Is Active
614+
defaultValue: true
615+
role_id:
616+
type: lookup
617+
label: Role Id
618+
reference_to: roles
619+
```
620+
621+
**Type Mapping:**
622+
623+
The sync command automatically maps SQL types to ObjectQL field types:
624+
625+
| SQL Type | ObjectQL Type |
626+
|----------|---------------|
627+
| INT, INTEGER, BIGINT | integer |
628+
| FLOAT, DOUBLE, DECIMAL, NUMERIC | number |
629+
| BOOLEAN, BIT | boolean |
630+
| VARCHAR, CHAR, TEXT | text |
631+
| TEXT, CLOB | textarea |
632+
| TIMESTAMP, DATETIME | datetime |
633+
| DATE | date |
634+
| TIME | time |
635+
| JSON, JSONB | json |
636+
637+
**Foreign Key Detection:**
638+
639+
Foreign keys are automatically detected and converted to `lookup` fields:
640+
641+
```sql
642+
-- Database Schema
643+
CREATE TABLE posts (
644+
id VARCHAR PRIMARY KEY,
645+
title VARCHAR NOT NULL,
646+
author_id VARCHAR REFERENCES users(id),
647+
...
648+
);
649+
```
650+
651+
```yaml
652+
# Generated posts.object.yml
653+
name: posts
654+
label: Posts
655+
fields:
656+
title:
657+
type: text
658+
label: Title
659+
required: true
660+
author_id:
661+
type: lookup
662+
label: Author Id
663+
reference_to: users
664+
```
665+
520666
---
521667

522668
## Development Tools

0 commit comments

Comments
 (0)