@dreamtree-org/korm-js 1.0.40 → 1.0.41
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.
- package/README.md +204 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -500,6 +500,210 @@ POST /api/Users/crud
|
|
|
500
500
|
}
|
|
501
501
|
```
|
|
502
502
|
|
|
503
|
+
## Relational Support with hasRelations
|
|
504
|
+
|
|
505
|
+
### Understanding hasRelations Structure
|
|
506
|
+
|
|
507
|
+
The `hasRelations` object in your schema defines relationships between models. Based on your actual schema file (`../borebell.in/schema/sync.json`), relationships use the following structure:
|
|
508
|
+
|
|
509
|
+
#### One-to-One or Belongs-To Relationship (`type: "one"`)
|
|
510
|
+
|
|
511
|
+
```json
|
|
512
|
+
{
|
|
513
|
+
"UserDetail": {
|
|
514
|
+
"hasRelations": {
|
|
515
|
+
"User": {
|
|
516
|
+
"type": "one",
|
|
517
|
+
"table": "users",
|
|
518
|
+
"localKey": "user_id",
|
|
519
|
+
"foreignKey": "id"
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
```
|
|
525
|
+
|
|
526
|
+
#### One-to-Many Relationship (`type: "many"`)
|
|
527
|
+
|
|
528
|
+
```json
|
|
529
|
+
{
|
|
530
|
+
"User": {
|
|
531
|
+
"hasRelations": {
|
|
532
|
+
"Post": {
|
|
533
|
+
"type": "many",
|
|
534
|
+
"table": "posts",
|
|
535
|
+
"localKey": "id",
|
|
536
|
+
"foreignKey": "user_id"
|
|
537
|
+
},
|
|
538
|
+
"Comment": {
|
|
539
|
+
"type": "many",
|
|
540
|
+
"table": "comments",
|
|
541
|
+
"localKey": "id",
|
|
542
|
+
"foreignKey": "user_id"
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
```
|
|
548
|
+
|
|
549
|
+
#### Many-to-Many Relationship (using `through`)
|
|
550
|
+
|
|
551
|
+
For many-to-many relationships, use the `through` key to specify the join/pivot table:
|
|
552
|
+
|
|
553
|
+
```json
|
|
554
|
+
{
|
|
555
|
+
"User": {
|
|
556
|
+
"hasRelations": {
|
|
557
|
+
"Role": {
|
|
558
|
+
"type": "many",
|
|
559
|
+
"table": "roles",
|
|
560
|
+
"localKey": "id",
|
|
561
|
+
"foreignKey": "id",
|
|
562
|
+
"through": "user_roles",
|
|
563
|
+
"throughLocalKey": "user_id",
|
|
564
|
+
"throughForeignKey": "role_id"
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
```
|
|
570
|
+
|
|
571
|
+
**Many-to-Many Structure:**
|
|
572
|
+
- `type`: `"many"` (always use "many" for many-to-many)
|
|
573
|
+
- `table`: The related table name (e.g., `"roles"`)
|
|
574
|
+
- `localKey`: The primary key in the current model (e.g., `"id"`)
|
|
575
|
+
- `foreignKey`: The primary key in the related table (e.g., `"id"`)
|
|
576
|
+
- `through`: **Required** - The join/pivot table name (e.g., `"user_roles"`)
|
|
577
|
+
- `throughLocalKey`: The foreign key in the join table pointing to the current model (e.g., `"user_id"`)
|
|
578
|
+
- `throughForeignKey`: The foreign key in the join table pointing to the related model (e.g., `"role_id"`)
|
|
579
|
+
|
|
580
|
+
### Using Relationships in Queries
|
|
581
|
+
|
|
582
|
+
#### Eager Loading with `with`
|
|
583
|
+
|
|
584
|
+
```javascript
|
|
585
|
+
// Load User with related Posts and Comments
|
|
586
|
+
POST /api/User/crud
|
|
587
|
+
{
|
|
588
|
+
"action": "list",
|
|
589
|
+
"where": { "id": 1 },
|
|
590
|
+
"with": ["Post", "Comment"]
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// Nested relationships
|
|
594
|
+
POST /api/User/crud
|
|
595
|
+
{
|
|
596
|
+
"action": "list",
|
|
597
|
+
"where": { "id": 1 },
|
|
598
|
+
"with": ["Post.Comment", "Post.Like"]
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
// Multiple relationships
|
|
602
|
+
POST /api/User/crud
|
|
603
|
+
{
|
|
604
|
+
"action": "list",
|
|
605
|
+
"where": { "id": 1 },
|
|
606
|
+
"with": ["UserDetail", "Post", "Comment", "Like"]
|
|
607
|
+
}
|
|
608
|
+
```
|
|
609
|
+
|
|
610
|
+
#### Filtering Related Data with Nested Where
|
|
611
|
+
|
|
612
|
+
```javascript
|
|
613
|
+
// Get users who have posts with specific hashtag
|
|
614
|
+
POST /api/User/crud
|
|
615
|
+
{
|
|
616
|
+
"action": "list",
|
|
617
|
+
"where": {
|
|
618
|
+
"Post.hashtag": "#OrganicFarming"
|
|
619
|
+
},
|
|
620
|
+
"with": ["Post"]
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// Get posts with comments from specific user
|
|
624
|
+
POST /api/Post/crud
|
|
625
|
+
{
|
|
626
|
+
"action": "list",
|
|
627
|
+
"where": {
|
|
628
|
+
"Comment.user_id": 1
|
|
629
|
+
},
|
|
630
|
+
"with": ["Comment"]
|
|
631
|
+
}
|
|
632
|
+
```
|
|
633
|
+
|
|
634
|
+
### Example: Complete Relationship Structure
|
|
635
|
+
|
|
636
|
+
Based on your actual schema (`../borebell.in/schema/sync.json`):
|
|
637
|
+
|
|
638
|
+
```json
|
|
639
|
+
{
|
|
640
|
+
"User": {
|
|
641
|
+
"table": "users",
|
|
642
|
+
"hasRelations": {
|
|
643
|
+
"UserDetail": {
|
|
644
|
+
"type": "one",
|
|
645
|
+
"table": "user_details",
|
|
646
|
+
"localKey": "id",
|
|
647
|
+
"foreignKey": "user_id"
|
|
648
|
+
},
|
|
649
|
+
"Post": {
|
|
650
|
+
"type": "many",
|
|
651
|
+
"table": "posts",
|
|
652
|
+
"localKey": "id",
|
|
653
|
+
"foreignKey": "user_id"
|
|
654
|
+
},
|
|
655
|
+
"Comment": {
|
|
656
|
+
"type": "many",
|
|
657
|
+
"table": "comments",
|
|
658
|
+
"localKey": "id",
|
|
659
|
+
"foreignKey": "user_id"
|
|
660
|
+
},
|
|
661
|
+
"Like": {
|
|
662
|
+
"type": "many",
|
|
663
|
+
"table": "likes",
|
|
664
|
+
"localKey": "id",
|
|
665
|
+
"foreignKey": "user_id"
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
},
|
|
669
|
+
"Post": {
|
|
670
|
+
"table": "posts",
|
|
671
|
+
"hasRelations": {
|
|
672
|
+
"User": {
|
|
673
|
+
"type": "one",
|
|
674
|
+
"table": "users",
|
|
675
|
+
"localKey": "user_id",
|
|
676
|
+
"foreignKey": "id"
|
|
677
|
+
},
|
|
678
|
+
"Comment": {
|
|
679
|
+
"type": "many",
|
|
680
|
+
"table": "comments",
|
|
681
|
+
"localKey": "id",
|
|
682
|
+
"foreignKey": "post_id"
|
|
683
|
+
},
|
|
684
|
+
"Like": {
|
|
685
|
+
"type": "many",
|
|
686
|
+
"table": "likes",
|
|
687
|
+
"localKey": "id",
|
|
688
|
+
"foreignKey": "post_id"
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
```
|
|
694
|
+
|
|
695
|
+
### Important Notes
|
|
696
|
+
|
|
697
|
+
> **Always refer to your actual schema file** (`../borebell.in/schema/sync.json`) for the exact relationship definitions in your project. The structure shown above matches the format used in your schema.
|
|
698
|
+
|
|
699
|
+
- `type: "one"` = One-to-One or Belongs-To relationship
|
|
700
|
+
- `type: "many"` = One-to-Many relationship
|
|
701
|
+
- `through` = Required for Many-to-Many relationships (specifies the join table)
|
|
702
|
+
- `localKey` = The key in the current model
|
|
703
|
+
- `foreignKey` = The key in the related table
|
|
704
|
+
- `throughLocalKey` = The key in the join table pointing to current model (for many-to-many)
|
|
705
|
+
- `throughForeignKey` = The key in the join table pointing to related model (for many-to-many)
|
|
706
|
+
|
|
503
707
|
## Soft Delete Support
|
|
504
708
|
|
|
505
709
|
### Enabling Soft Delete
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dreamtree-org/korm-js",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.41",
|
|
4
4
|
"description": "Knowledge Object-Relational Mapping - A powerful, modular ORM system for Node.js with dynamic database operations, complex queries, relationships, and nested requests",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "Partha Preetham Krishna",
|