@objectql/cli 1.8.2 → 1.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +27 -0
  2. package/README.md +229 -0
  3. package/USAGE_EXAMPLES.md +147 -0
  4. package/__tests__/commands.test.ts +274 -1
  5. package/dist/commands/ai.js +4 -3
  6. package/dist/commands/ai.js.map +1 -1
  7. package/dist/commands/build.d.ts +12 -0
  8. package/dist/commands/build.js +119 -0
  9. package/dist/commands/build.js.map +1 -0
  10. package/dist/commands/dev.d.ts +9 -0
  11. package/dist/commands/dev.js +23 -0
  12. package/dist/commands/dev.js.map +1 -0
  13. package/dist/commands/format.d.ts +9 -0
  14. package/dist/commands/format.js +137 -0
  15. package/dist/commands/format.js.map +1 -0
  16. package/dist/commands/lint.d.ts +9 -0
  17. package/dist/commands/lint.js +120 -0
  18. package/dist/commands/lint.js.map +1 -0
  19. package/dist/commands/new.js +5 -52
  20. package/dist/commands/new.js.map +1 -1
  21. package/dist/commands/start.d.ts +11 -0
  22. package/dist/commands/start.js +119 -0
  23. package/dist/commands/start.js.map +1 -0
  24. package/dist/commands/sync.d.ts +14 -0
  25. package/dist/commands/sync.js +314 -0
  26. package/dist/commands/sync.js.map +1 -0
  27. package/dist/commands/test.d.ts +10 -0
  28. package/dist/commands/test.js +120 -0
  29. package/dist/commands/test.js.map +1 -0
  30. package/dist/index.js +109 -14
  31. package/dist/index.js.map +1 -1
  32. package/jest.config.js +19 -0
  33. package/package.json +7 -7
  34. package/src/commands/ai.ts +4 -3
  35. package/src/commands/build.ts +98 -0
  36. package/src/commands/dev.ts +23 -0
  37. package/src/commands/format.ts +110 -0
  38. package/src/commands/lint.ts +98 -0
  39. package/src/commands/new.ts +5 -52
  40. package/src/commands/start.ts +100 -0
  41. package/src/commands/sync.ts +328 -0
  42. package/src/commands/test.ts +98 -0
  43. package/src/index.ts +114 -14
  44. package/tsconfig.tsbuildinfo +1 -1
  45. package/dist/commands/studio.d.ts +0 -5
  46. package/dist/commands/studio.js +0 -364
  47. package/dist/commands/studio.js.map +0 -1
  48. package/src/commands/studio.ts +0 -354
package/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  # @objectql/cli
2
2
 
3
+ ## 1.8.4
4
+
5
+ ### Patch Changes
6
+
7
+ - Release version 1.8.4 with latest improvements and bug fixes
8
+ - Updated dependencies
9
+ - @objectql/types@1.8.4
10
+ - @objectql/core@1.8.4
11
+ - @objectql/platform-node@1.8.4
12
+ - @objectql/driver-sql@1.8.4
13
+ - @objectql/server@1.8.4
14
+
15
+ ## 1.8.3
16
+
17
+ ### Patch Changes
18
+
19
+ - Release patch version 1.8.3
20
+
21
+ Small version update with latest improvements and bug fixes.
22
+
23
+ - Updated dependencies
24
+ - @objectql/core@1.8.3
25
+ - @objectql/driver-sql@1.8.3
26
+ - @objectql/server@1.8.3
27
+ - @objectql/types@1.8.3
28
+ - @objectql/platform-node@1.8.3
29
+
3
30
  ## 1.8.2
4
31
 
5
32
  ### Patch Changes
package/README.md CHANGED
@@ -356,12 +356,241 @@ objectql migrate status --config ./config/objectql.config.ts
356
356
  - `-c, --config <path>` - Path to objectql.config.ts/js
357
357
  - `-d, --dir <path>` - Migrations directory [default: "./migrations"]
358
358
 
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
+
359
432
  ---
360
433
 
361
434
  ### Development Tools
362
435
 
436
+ #### `dev` (alias: `d`)
437
+
438
+ Start development server with hot reload support. This is the recommended command for local development.
439
+
440
+ ```bash
441
+ # Start development server (port 3000)
442
+ objectql dev
443
+
444
+ # Specify options
445
+ objectql dev --dir ./src --port 8080
446
+
447
+ # Disable file watching
448
+ objectql dev --no-watch
449
+ ```
450
+
451
+ The development server provides:
452
+ - **Swagger UI**: `http://localhost:<port>/swagger` - Interactive API documentation
453
+ - **API Endpoint**: `http://localhost:<port>/` - Main API endpoint
454
+ - **OpenAPI Spec**: `http://localhost:<port>/openapi.json` - Machine-readable API spec
455
+
456
+ **Options:**
457
+ - `-p, --port <number>` - Port to listen on [default: "3000"]
458
+ - `-d, --dir <path>` - Directory containing schema [default: "."]
459
+ - `--no-watch` - Disable file watching (future feature)
460
+
461
+ #### `start`
462
+
463
+ Start production server. Loads configuration from `objectql.config.ts/js` if available.
464
+
465
+ ```bash
466
+ # Start production server
467
+ objectql start
468
+
469
+ # Specify options
470
+ objectql start --port 8080 --dir ./dist
471
+
472
+ # Use custom config file
473
+ objectql start --config ./config/production.config.ts
474
+ ```
475
+
476
+ **Options:**
477
+ - `-p, --port <number>` - Port to listen on [default: "3000"]
478
+ - `-d, --dir <path>` - Directory containing schema [default: "."]
479
+ - `-c, --config <path>` - Path to objectql.config.ts/js
480
+
481
+ **Environment Variables:**
482
+ - `DATABASE_FILE` - Path to SQLite database file (default: "./objectql.db")
483
+
484
+ #### `build` (alias: `b`)
485
+
486
+ Build project and prepare for production deployment. Validates metadata, generates TypeScript types, and copies files to dist folder.
487
+
488
+ ```bash
489
+ # Build project
490
+ objectql build
491
+
492
+ # Build with custom output directory
493
+ objectql build --output ./build
494
+
495
+ # Build without type generation
496
+ objectql build --no-types
497
+
498
+ # Build without validation
499
+ objectql build --no-validate
500
+ ```
501
+
502
+ **Options:**
503
+ - `-d, --dir <path>` - Source directory [default: "."]
504
+ - `-o, --output <path>` - Output directory [default: "./dist"]
505
+ - `--no-types` - Skip TypeScript type generation
506
+ - `--no-validate` - Skip metadata validation
507
+
508
+ **Build Steps:**
509
+ 1. Validates all metadata files
510
+ 2. Generates TypeScript type definitions (if enabled)
511
+ 3. Copies all metadata files (.yml) to dist folder
512
+
513
+ #### `test` (alias: `t`)
514
+
515
+ Run tests for the ObjectQL project. Automatically detects and runs Jest tests if configured.
516
+
517
+ ```bash
518
+ # Run all tests
519
+ objectql test
520
+
521
+ # Run tests in watch mode
522
+ objectql test --watch
523
+
524
+ # Run tests with coverage report
525
+ objectql test --coverage
526
+
527
+ # Specify project directory
528
+ objectql test --dir ./src
529
+ ```
530
+
531
+ **Options:**
532
+ - `-d, --dir <path>` - Project directory [default: "."]
533
+ - `-w, --watch` - Watch mode (re-run tests on file changes)
534
+ - `--coverage` - Generate coverage report
535
+
536
+ **Requirements:**
537
+ - Jest must be installed and configured in package.json
538
+ - Falls back to `npm test` if Jest is not detected
539
+
540
+ #### `lint` (alias: `l`)
541
+
542
+ Validate metadata files for correctness and best practices.
543
+
544
+ ```bash
545
+ # Lint all metadata files
546
+ objectql lint
547
+
548
+ # Lint specific directory
549
+ objectql lint --dir ./src/objects
550
+
551
+ # Auto-fix issues (future feature)
552
+ objectql lint --fix
553
+ ```
554
+
555
+ **Options:**
556
+ - `-d, --dir <path>` - Directory to lint [default: "."]
557
+ - `--fix` - Automatically fix issues (future feature)
558
+
559
+ **Validation Rules:**
560
+ - Object and field names must be lowercase with underscores
561
+ - All objects should have labels
562
+ - All fields should have labels
563
+ - No empty objects (objects must have at least one field)
564
+
565
+ #### `format` (alias: `fmt`)
566
+
567
+ Format metadata files using Prettier for consistent styling.
568
+
569
+ ```bash
570
+ # Format all YAML files
571
+ objectql format
572
+
573
+ # Format specific directory
574
+ objectql format --dir ./src
575
+
576
+ # Check formatting without modifying files
577
+ objectql format --check
578
+ ```
579
+
580
+ **Options:**
581
+ - `-d, --dir <path>` - Directory to format [default: "."]
582
+ - `--check` - Check if files are formatted without modifying them
583
+
584
+ **Formatting Rules:**
585
+ - Uses Prettier with YAML parser
586
+ - Print width: 80 characters
587
+ - Tab width: 2 spaces
588
+ - Single quotes for strings
589
+
363
590
  #### `serve` (alias: `s`)
364
591
 
592
+ *Note: This is an alias for the `dev` command, kept for backwards compatibility. Use `objectql dev` for new projects.*
593
+
365
594
  Start a lightweight development server with an in-memory database. Perfect for rapid prototyping without setting up a backend project.
366
595
 
367
596
  ```bash
package/USAGE_EXAMPLES.md CHANGED
@@ -517,6 +517,153 @@ export async function up(app: ObjectQL) {
517
517
  }
518
518
  ```
519
519
 
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, SERIAL | number |
628
+ | FLOAT, DOUBLE, DECIMAL, NUMERIC | number |
629
+ | BOOLEAN, BIT | boolean |
630
+ | VARCHAR, CHAR | text |
631
+ | TEXT, CLOB, LONGTEXT | textarea |
632
+ | TIMESTAMP, DATETIME | datetime |
633
+ | DATE | date |
634
+ | TIME | time |
635
+ | JSON, JSONB | object |
636
+ | BLOB, BINARY, BYTEA | file |
637
+
638
+ **Foreign Key Detection:**
639
+
640
+ Foreign keys are automatically detected and converted to `lookup` fields:
641
+
642
+ ```sql
643
+ -- Database Schema
644
+ CREATE TABLE posts (
645
+ id VARCHAR PRIMARY KEY,
646
+ title VARCHAR NOT NULL,
647
+ author_id VARCHAR REFERENCES users(id),
648
+ ...
649
+ );
650
+ ```
651
+
652
+ ```yaml
653
+ # Generated posts.object.yml
654
+ name: posts
655
+ label: Posts
656
+ fields:
657
+ title:
658
+ type: text
659
+ label: Title
660
+ required: true
661
+ author_id:
662
+ type: lookup
663
+ label: Author Id
664
+ reference_to: users
665
+ ```
666
+
520
667
  ---
521
668
 
522
669
  ## Development Tools