@orchestr-sh/orchestr 1.3.0 → 1.4.0
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 +120 -34
- package/dist/Database/Ensemble/Concerns/HasRelationships.d.ts +96 -0
- package/dist/Database/Ensemble/Concerns/HasRelationships.d.ts.map +1 -0
- package/dist/Database/Ensemble/Concerns/HasRelationships.js +145 -0
- package/dist/Database/Ensemble/Concerns/HasRelationships.js.map +1 -0
- package/dist/Database/Ensemble/Ensemble.d.ts +18 -1
- package/dist/Database/Ensemble/Ensemble.d.ts.map +1 -1
- package/dist/Database/Ensemble/Ensemble.js +45 -1
- package/dist/Database/Ensemble/Ensemble.js.map +1 -1
- package/dist/Database/Ensemble/EnsembleBuilder.d.ts +19 -2
- package/dist/Database/Ensemble/EnsembleBuilder.d.ts.map +1 -1
- package/dist/Database/Ensemble/EnsembleBuilder.js +105 -4
- package/dist/Database/Ensemble/EnsembleBuilder.js.map +1 -1
- package/dist/Database/Ensemble/Relations/BelongsTo.d.ts +92 -0
- package/dist/Database/Ensemble/Relations/BelongsTo.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/BelongsTo.js +174 -0
- package/dist/Database/Ensemble/Relations/BelongsTo.js.map +1 -0
- package/dist/Database/Ensemble/Relations/HasMany.d.ts +100 -0
- package/dist/Database/Ensemble/Relations/HasMany.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/HasMany.js +199 -0
- package/dist/Database/Ensemble/Relations/HasMany.js.map +1 -0
- package/dist/Database/Ensemble/Relations/HasOne.d.ts +76 -0
- package/dist/Database/Ensemble/Relations/HasOne.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/HasOne.js +142 -0
- package/dist/Database/Ensemble/Relations/HasOne.js.map +1 -0
- package/dist/Database/Ensemble/Relations/Relation.d.ts +114 -0
- package/dist/Database/Ensemble/Relations/Relation.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/Relation.js +137 -0
- package/dist/Database/Ensemble/Relations/Relation.js.map +1 -0
- package/dist/Database/Ensemble/Relations/index.d.ts +10 -0
- package/dist/Database/Ensemble/Relations/index.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/index.js +17 -0
- package/dist/Database/Ensemble/Relations/index.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ Built from the ground up with Laravel's core components:
|
|
|
14
14
|
- **Controllers** - MVC architecture support
|
|
15
15
|
- **Facades** - Static proxy access to services (Route, DB)
|
|
16
16
|
- **Query Builder** - Fluent database query builder with full Laravel API
|
|
17
|
-
- **Ensemble ORM** - ActiveRecord ORM (Laravel's Eloquent equivalent) with relationships, soft deletes, and more
|
|
17
|
+
- **Ensemble ORM** - ActiveRecord ORM (Laravel's Eloquent equivalent) with relationships (HasOne, HasMany, BelongsTo), eager/lazy loading, soft deletes, and more
|
|
18
18
|
- **Database Manager** - Multi-connection database management
|
|
19
19
|
- **Application Lifecycle** - Complete Laravel bootstrap process
|
|
20
20
|
|
|
@@ -26,11 +26,6 @@ npm install @orchestr-sh/orchestr reflect-metadata
|
|
|
26
26
|
|
|
27
27
|
**Note**: `reflect-metadata` is required for dependency injection to work.
|
|
28
28
|
|
|
29
|
-
## Documentation
|
|
30
|
-
|
|
31
|
-
- **[Dependency Injection Guide](./DEPENDENCY_INJECTION.md)** - Complete guide to using DI with troubleshooting
|
|
32
|
-
- **[DI Example](./examples/dependency-injection/)** - Working example with services and controllers
|
|
33
|
-
|
|
34
29
|
## Quick Start
|
|
35
30
|
|
|
36
31
|
```typescript
|
|
@@ -454,9 +449,9 @@ await DB.table('users')
|
|
|
454
449
|
ActiveRecord ORM (Eloquent equivalent) with relationships and advanced features:
|
|
455
450
|
|
|
456
451
|
```typescript
|
|
457
|
-
import { Ensemble,
|
|
452
|
+
import { Ensemble, HasOne, HasMany, BelongsTo, softDeletes } from 'orchestr';
|
|
458
453
|
|
|
459
|
-
// Define
|
|
454
|
+
// Define models with relationships
|
|
460
455
|
class User extends Ensemble {
|
|
461
456
|
protected table = 'users';
|
|
462
457
|
protected fillable = ['name', 'email', 'password'];
|
|
@@ -465,12 +460,73 @@ class User extends Ensemble {
|
|
|
465
460
|
email_verified_at: 'datetime',
|
|
466
461
|
is_admin: 'boolean'
|
|
467
462
|
};
|
|
463
|
+
|
|
464
|
+
// One-to-One: User has one profile
|
|
465
|
+
profile(): HasOne<Profile, User> {
|
|
466
|
+
return this.hasOne(Profile);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// One-to-Many: User has many posts
|
|
470
|
+
posts(): HasMany<Post, User> {
|
|
471
|
+
return this.hasMany(Post);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
class Profile extends Ensemble {
|
|
476
|
+
protected table = 'profiles';
|
|
477
|
+
|
|
478
|
+
// Belongs To: Profile belongs to user
|
|
479
|
+
user(): BelongsTo<User, Profile> {
|
|
480
|
+
return this.belongsTo(User);
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
class Post extends Ensemble {
|
|
485
|
+
protected table = 'posts';
|
|
486
|
+
|
|
487
|
+
// Belongs To: Post belongs to author (user)
|
|
488
|
+
author(): BelongsTo<User, Post> {
|
|
489
|
+
return this.belongsTo(User, 'user_id');
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
// One-to-Many: Post has many comments
|
|
493
|
+
comments(): HasMany<Comment, Post> {
|
|
494
|
+
return this.hasMany(Comment);
|
|
495
|
+
}
|
|
468
496
|
}
|
|
469
497
|
|
|
470
498
|
// Query using the model
|
|
471
499
|
const users = await User.query().where('active', true).get();
|
|
472
500
|
const user = await User.query().find(1);
|
|
473
501
|
|
|
502
|
+
// Lazy loading relationships
|
|
503
|
+
await user.load('posts');
|
|
504
|
+
await user.load(['posts', 'profile']);
|
|
505
|
+
const posts = user.getRelation('posts');
|
|
506
|
+
|
|
507
|
+
// Eager loading (solves N+1 problem)
|
|
508
|
+
const users = await User.query()
|
|
509
|
+
.with(['posts.comments', 'profile'])
|
|
510
|
+
.get();
|
|
511
|
+
|
|
512
|
+
// Eager load with constraints
|
|
513
|
+
const users = await User.query()
|
|
514
|
+
.with({
|
|
515
|
+
posts: (query) => query.where('published', '=', true)
|
|
516
|
+
})
|
|
517
|
+
.get();
|
|
518
|
+
|
|
519
|
+
// Create related models
|
|
520
|
+
const post = await user.posts().create({
|
|
521
|
+
title: 'My Post',
|
|
522
|
+
content: 'Content here'
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
// Associate/dissociate (BelongsTo)
|
|
526
|
+
const post = new Post();
|
|
527
|
+
post.author().associate(user);
|
|
528
|
+
await post.save();
|
|
529
|
+
|
|
474
530
|
// Create
|
|
475
531
|
const user = new User();
|
|
476
532
|
user.name = 'John Doe';
|
|
@@ -491,37 +547,29 @@ await user.save();
|
|
|
491
547
|
// Delete
|
|
492
548
|
await user.delete();
|
|
493
549
|
|
|
494
|
-
// Mass assignment
|
|
495
|
-
await User.query().create({
|
|
496
|
-
name: 'John',
|
|
497
|
-
email: 'john@example.com'
|
|
498
|
-
});
|
|
499
|
-
|
|
500
550
|
// Soft deletes
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
class Post extends softDeletes(Ensemble) {
|
|
504
|
-
protected table = 'posts';
|
|
551
|
+
class Article extends softDeletes(Ensemble) {
|
|
552
|
+
protected table = 'articles';
|
|
505
553
|
}
|
|
506
554
|
|
|
507
|
-
const
|
|
508
|
-
await
|
|
509
|
-
await
|
|
510
|
-
await
|
|
555
|
+
const article = await Article.query().find(1);
|
|
556
|
+
await article.delete(); // Soft delete
|
|
557
|
+
await article.restore(); // Restore
|
|
558
|
+
await article.forceDelete(); // Permanent delete
|
|
511
559
|
|
|
512
560
|
// Query only non-deleted
|
|
513
|
-
const
|
|
561
|
+
const articles = await Article.query().get();
|
|
514
562
|
|
|
515
563
|
// Query with trashed
|
|
516
|
-
const
|
|
564
|
+
const allArticles = await Article.query().withTrashed().get();
|
|
517
565
|
|
|
518
566
|
// Query only trashed
|
|
519
|
-
const
|
|
567
|
+
const trashedArticles = await Article.query().onlyTrashed().get();
|
|
520
568
|
|
|
521
569
|
// Timestamps
|
|
522
570
|
// Automatically manages created_at and updated_at
|
|
523
|
-
class
|
|
524
|
-
protected table = '
|
|
571
|
+
class Post extends Ensemble {
|
|
572
|
+
protected table = 'posts';
|
|
525
573
|
public timestamps = true; // enabled by default
|
|
526
574
|
}
|
|
527
575
|
|
|
@@ -550,6 +598,8 @@ console.log(user.full_name); // Uses accessor
|
|
|
550
598
|
user.password = 'secret123'; // Uses mutator
|
|
551
599
|
```
|
|
552
600
|
|
|
601
|
+
**See [RELATIONSHIPS.md](./RELATIONSHIPS.md) for complete relationship documentation.**
|
|
602
|
+
|
|
553
603
|
### Database Setup
|
|
554
604
|
|
|
555
605
|
Configure multiple database connections:
|
|
@@ -621,7 +671,8 @@ kernel.listen(3000);
|
|
|
621
671
|
|
|
622
672
|
**app/Models/User.ts**
|
|
623
673
|
```typescript
|
|
624
|
-
import { Ensemble, softDeletes } from 'orchestr';
|
|
674
|
+
import { Ensemble, HasMany, softDeletes } from 'orchestr';
|
|
675
|
+
import { Post } from './Post';
|
|
625
676
|
|
|
626
677
|
export class User extends softDeletes(Ensemble) {
|
|
627
678
|
protected table = 'users';
|
|
@@ -632,6 +683,26 @@ export class User extends softDeletes(Ensemble) {
|
|
|
632
683
|
email_verified_at: 'datetime',
|
|
633
684
|
is_admin: 'boolean'
|
|
634
685
|
};
|
|
686
|
+
|
|
687
|
+
// Define relationship
|
|
688
|
+
posts(): HasMany<Post, User> {
|
|
689
|
+
return this.hasMany(Post);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
```
|
|
693
|
+
|
|
694
|
+
**app/Models/Post.ts**
|
|
695
|
+
```typescript
|
|
696
|
+
import { Ensemble, BelongsTo } from 'orchestr';
|
|
697
|
+
import { User } from './User';
|
|
698
|
+
|
|
699
|
+
export class Post extends Ensemble {
|
|
700
|
+
protected table = 'posts';
|
|
701
|
+
protected fillable = ['user_id', 'title', 'content', 'published_at'];
|
|
702
|
+
|
|
703
|
+
author(): BelongsTo<User, Post> {
|
|
704
|
+
return this.belongsTo(User, 'user_id');
|
|
705
|
+
}
|
|
635
706
|
}
|
|
636
707
|
```
|
|
637
708
|
|
|
@@ -651,15 +722,17 @@ Route.group({ prefix: 'api' }, () => {
|
|
|
651
722
|
return res.json({ users });
|
|
652
723
|
});
|
|
653
724
|
|
|
654
|
-
// Using Ensemble ORM
|
|
725
|
+
// Using Ensemble ORM with eager loading
|
|
655
726
|
Route.get('/users/:id', async (req, res) => {
|
|
656
|
-
const user = await User.query()
|
|
727
|
+
const user = await User.query()
|
|
728
|
+
.with('posts')
|
|
729
|
+
.find(req.routeParam('id'));
|
|
657
730
|
|
|
658
731
|
if (!user) {
|
|
659
732
|
return res.status(404).json({ message: 'User not found' });
|
|
660
733
|
}
|
|
661
734
|
|
|
662
|
-
return res.json({ user });
|
|
735
|
+
return res.json({ user: user.toObject() });
|
|
663
736
|
});
|
|
664
737
|
|
|
665
738
|
Route.post('/users', async (req, res) => {
|
|
@@ -709,9 +782,15 @@ src/
|
|
|
709
782
|
│ │ ├── EnsembleBuilder.ts # Model query builder
|
|
710
783
|
│ │ ├── EnsembleCollection.ts # Model collection
|
|
711
784
|
│ │ ├── SoftDeletes.ts # Soft delete trait
|
|
785
|
+
│ │ ├── Relations/
|
|
786
|
+
│ │ │ ├── Relation.ts # Base relation class
|
|
787
|
+
│ │ │ ├── HasOne.ts # One-to-one relationship
|
|
788
|
+
│ │ │ ├── HasMany.ts # One-to-many relationship
|
|
789
|
+
│ │ │ └── BelongsTo.ts # Inverse relationship
|
|
712
790
|
│ │ └── Concerns/
|
|
713
791
|
│ │ ├── HasAttributes.ts # Attribute handling & casting
|
|
714
|
-
│ │
|
|
792
|
+
│ │ ├── HasTimestamps.ts # Timestamp management
|
|
793
|
+
│ │ └── HasRelationships.ts # Relationship functionality
|
|
715
794
|
│ ├── Adapters/
|
|
716
795
|
│ │ └── DrizzleAdapter.ts # Drizzle ORM adapter
|
|
717
796
|
│ └── DatabaseServiceProvider.ts
|
|
@@ -751,7 +830,11 @@ Core components completed and in progress:
|
|
|
751
830
|
- [x] Multi-connection Database Manager
|
|
752
831
|
- [x] Soft Deletes
|
|
753
832
|
- [x] Model Attributes & Casting
|
|
754
|
-
- [
|
|
833
|
+
- [x] Model Relationships (HasOne, HasMany, BelongsTo)
|
|
834
|
+
- [x] Eager/Lazy Loading
|
|
835
|
+
- [ ] Many-to-Many Relationships (BelongsToMany)
|
|
836
|
+
- [ ] Relationship Queries (has, whereHas, withCount)
|
|
837
|
+
- [ ] Polymorphic Relationships
|
|
755
838
|
- [ ] Database Migrations
|
|
756
839
|
- [ ] Database Seeding
|
|
757
840
|
- [ ] Validation System
|
|
@@ -781,7 +864,10 @@ Core components completed and in progress:
|
|
|
781
864
|
| Soft Deletes | ✅ | ✅ |
|
|
782
865
|
| Timestamps | ✅ | ✅ |
|
|
783
866
|
| Attribute Casting | ✅ | ✅ |
|
|
784
|
-
|
|
|
867
|
+
| Basic Relationships | ✅ | ✅ |
|
|
868
|
+
| Eager/Lazy Loading | ✅ | ✅ |
|
|
869
|
+
| Many-to-Many | ✅ | 🚧 |
|
|
870
|
+
| Polymorphic Relations | ✅ | 🚧 |
|
|
785
871
|
| Migrations | ✅ | 🚧 |
|
|
786
872
|
| Seeding | ✅ | 🚧 |
|
|
787
873
|
| Validation | ✅ | 🚧 |
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HasRelationships Concern
|
|
3
|
+
*
|
|
4
|
+
* Provides relationship functionality to Ensemble models
|
|
5
|
+
*/
|
|
6
|
+
import { Ensemble } from '../Ensemble';
|
|
7
|
+
import { EnsembleBuilder } from '../EnsembleBuilder';
|
|
8
|
+
import { HasOne } from '../Relations/HasOne';
|
|
9
|
+
import { HasMany } from '../Relations/HasMany';
|
|
10
|
+
import { BelongsTo } from '../Relations/BelongsTo';
|
|
11
|
+
export interface RelationshipConfig {
|
|
12
|
+
type: 'hasOne' | 'hasMany' | 'belongsTo' | 'belongsToMany' | 'hasOneThrough' | 'hasManyThrough' | 'morphTo' | 'morphMany' | 'morphOne' | 'morphToMany' | 'morphedByMany';
|
|
13
|
+
related: string;
|
|
14
|
+
foreignKey?: string;
|
|
15
|
+
localKey?: string;
|
|
16
|
+
relation?: string;
|
|
17
|
+
}
|
|
18
|
+
export declare abstract class HasRelationshipsMixin {
|
|
19
|
+
/**
|
|
20
|
+
* The loaded relationships for the model
|
|
21
|
+
*/
|
|
22
|
+
protected relations: Record<string, any>;
|
|
23
|
+
/**
|
|
24
|
+
* Define a one-to-one relationship
|
|
25
|
+
*/
|
|
26
|
+
protected hasOne<TRelated extends Ensemble>(related: new () => TRelated, foreignKey?: string, localKey?: string): HasOne<TRelated, any>;
|
|
27
|
+
/**
|
|
28
|
+
* Define a one-to-many relationship
|
|
29
|
+
*/
|
|
30
|
+
protected hasMany<TRelated extends Ensemble>(related: new () => TRelated, foreignKey?: string, localKey?: string): HasMany<TRelated, any>;
|
|
31
|
+
/**
|
|
32
|
+
* Define an inverse one-to-one or many relationship
|
|
33
|
+
*/
|
|
34
|
+
protected belongsTo<TRelated extends Ensemble>(related: new () => TRelated, foreignKey?: string, ownerKey?: string, relation?: string): BelongsTo<TRelated, any>;
|
|
35
|
+
/**
|
|
36
|
+
* Get the default foreign key name for the model
|
|
37
|
+
*/
|
|
38
|
+
protected getForeignKey(): string;
|
|
39
|
+
/**
|
|
40
|
+
* Get a relationship value from a method
|
|
41
|
+
*/
|
|
42
|
+
protected getRelationshipFromMethod(method: string): any;
|
|
43
|
+
/**
|
|
44
|
+
* Get a relationship instance by name
|
|
45
|
+
*/
|
|
46
|
+
getRelation(relation: string): any;
|
|
47
|
+
/**
|
|
48
|
+
* Set the given relationship on the model
|
|
49
|
+
*/
|
|
50
|
+
setRelation(relation: string, value: any): this;
|
|
51
|
+
/**
|
|
52
|
+
* Unset a loaded relationship
|
|
53
|
+
*/
|
|
54
|
+
unsetRelation(relation: string): this;
|
|
55
|
+
/**
|
|
56
|
+
* Get all the loaded relations for the instance
|
|
57
|
+
*/
|
|
58
|
+
getRelations(): Record<string, any>;
|
|
59
|
+
/**
|
|
60
|
+
* Set the entire relations array on the model
|
|
61
|
+
*/
|
|
62
|
+
setRelations(relations: Record<string, any>): this;
|
|
63
|
+
/**
|
|
64
|
+
* Determine if the given relation is loaded
|
|
65
|
+
*/
|
|
66
|
+
relationLoaded(key: string): boolean;
|
|
67
|
+
/**
|
|
68
|
+
* Load a relationship if it hasn't been loaded yet
|
|
69
|
+
*/
|
|
70
|
+
load(relations: string | string[]): Promise<this>;
|
|
71
|
+
/**
|
|
72
|
+
* Eager load relations on the model
|
|
73
|
+
*/
|
|
74
|
+
loadMissing(relations: string | string[]): Promise<this>;
|
|
75
|
+
/**
|
|
76
|
+
* Touch the owning relations of the model
|
|
77
|
+
*/
|
|
78
|
+
touch(): Promise<boolean>;
|
|
79
|
+
/**
|
|
80
|
+
* Create a new model query instance for the model
|
|
81
|
+
*/
|
|
82
|
+
protected abstract newQuery(): EnsembleBuilder<any>;
|
|
83
|
+
/**
|
|
84
|
+
* Get the table name (from Ensemble)
|
|
85
|
+
*/
|
|
86
|
+
protected abstract getTable(): string;
|
|
87
|
+
/**
|
|
88
|
+
* Get the primary key name (from Ensemble)
|
|
89
|
+
*/
|
|
90
|
+
protected abstract getKeyName(): string;
|
|
91
|
+
/**
|
|
92
|
+
* Convert a string to snake case
|
|
93
|
+
*/
|
|
94
|
+
protected snake(value: string): string;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=HasRelationships.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HasRelationships.d.ts","sourceRoot":"","sources":["../../../../src/Database/Ensemble/Concerns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|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* HasRelationships Concern
|
|
4
|
+
*
|
|
5
|
+
* Provides relationship functionality to Ensemble models
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.HasRelationshipsMixin = void 0;
|
|
9
|
+
const Relation_1 = require("../Relations/Relation");
|
|
10
|
+
const HasOne_1 = require("../Relations/HasOne");
|
|
11
|
+
const HasMany_1 = require("../Relations/HasMany");
|
|
12
|
+
const BelongsTo_1 = require("../Relations/BelongsTo");
|
|
13
|
+
class HasRelationshipsMixin {
|
|
14
|
+
/**
|
|
15
|
+
* The loaded relationships for the model
|
|
16
|
+
*/
|
|
17
|
+
relations = {};
|
|
18
|
+
/**
|
|
19
|
+
* Define a one-to-one relationship
|
|
20
|
+
*/
|
|
21
|
+
hasOne(related, foreignKey, localKey) {
|
|
22
|
+
const instance = new related();
|
|
23
|
+
const finalForeignKey = foreignKey || this.getForeignKey();
|
|
24
|
+
const finalLocalKey = localKey || this.getKeyName();
|
|
25
|
+
return new HasOne_1.HasOne(instance.newQuery(), this, finalForeignKey, finalLocalKey);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Define a one-to-many relationship
|
|
29
|
+
*/
|
|
30
|
+
hasMany(related, foreignKey, localKey) {
|
|
31
|
+
const instance = new related();
|
|
32
|
+
const finalForeignKey = foreignKey || this.getForeignKey();
|
|
33
|
+
const finalLocalKey = localKey || this.getKeyName();
|
|
34
|
+
return new HasMany_1.HasMany(instance.newQuery(), this, finalForeignKey, finalLocalKey);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Define an inverse one-to-one or many relationship
|
|
38
|
+
*/
|
|
39
|
+
belongsTo(related, foreignKey, ownerKey, relation) {
|
|
40
|
+
const instance = new related();
|
|
41
|
+
const relationName = relation || this.snake(instance.constructor.name);
|
|
42
|
+
if (!foreignKey) {
|
|
43
|
+
foreignKey = this.snake(relationName) + '_' + instance.getKeyName();
|
|
44
|
+
}
|
|
45
|
+
const finalOwnerKey = ownerKey || instance.getKeyName();
|
|
46
|
+
return new BelongsTo_1.BelongsTo(instance.newQuery(), this, foreignKey, finalOwnerKey, relationName);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Get the default foreign key name for the model
|
|
50
|
+
*/
|
|
51
|
+
getForeignKey() {
|
|
52
|
+
return this.snake(this.constructor.name) + '_' + this.getKeyName();
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Get a relationship value from a method
|
|
56
|
+
*/
|
|
57
|
+
getRelationshipFromMethod(method) {
|
|
58
|
+
const relation = this[method]();
|
|
59
|
+
if (!(relation instanceof Relation_1.Relation)) {
|
|
60
|
+
throw new Error(`Relationship method must return an object of type Relation (${method})`);
|
|
61
|
+
}
|
|
62
|
+
return relation;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get a relationship instance by name
|
|
66
|
+
*/
|
|
67
|
+
getRelation(relation) {
|
|
68
|
+
return this.relations[relation];
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Set the given relationship on the model
|
|
72
|
+
*/
|
|
73
|
+
setRelation(relation, value) {
|
|
74
|
+
this.relations[relation] = value;
|
|
75
|
+
return this;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Unset a loaded relationship
|
|
79
|
+
*/
|
|
80
|
+
unsetRelation(relation) {
|
|
81
|
+
delete this.relations[relation];
|
|
82
|
+
return this;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Get all the loaded relations for the instance
|
|
86
|
+
*/
|
|
87
|
+
getRelations() {
|
|
88
|
+
return this.relations;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Set the entire relations array on the model
|
|
92
|
+
*/
|
|
93
|
+
setRelations(relations) {
|
|
94
|
+
this.relations = relations;
|
|
95
|
+
return this;
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Determine if the given relation is loaded
|
|
99
|
+
*/
|
|
100
|
+
relationLoaded(key) {
|
|
101
|
+
return key in this.relations;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Load a relationship if it hasn't been loaded yet
|
|
105
|
+
*/
|
|
106
|
+
async load(relations) {
|
|
107
|
+
const relationArray = Array.isArray(relations) ? relations : [relations];
|
|
108
|
+
for (const relation of relationArray) {
|
|
109
|
+
if (!this.relationLoaded(relation)) {
|
|
110
|
+
const results = await this.getRelationshipFromMethod(relation).getResults();
|
|
111
|
+
this.setRelation(relation, results);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return this;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Eager load relations on the model
|
|
118
|
+
*/
|
|
119
|
+
async loadMissing(relations) {
|
|
120
|
+
const relationArray = Array.isArray(relations) ? relations : [relations];
|
|
121
|
+
const missing = relationArray.filter(relation => !this.relationLoaded(relation));
|
|
122
|
+
if (missing.length > 0) {
|
|
123
|
+
await this.load(missing);
|
|
124
|
+
}
|
|
125
|
+
return this;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Touch the owning relations of the model
|
|
129
|
+
*/
|
|
130
|
+
async touch() {
|
|
131
|
+
// TODO: Implement when we have timestamps
|
|
132
|
+
return true;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Convert a string to snake case
|
|
136
|
+
*/
|
|
137
|
+
snake(value) {
|
|
138
|
+
return value
|
|
139
|
+
.replace(/([A-Z])/g, '_$1')
|
|
140
|
+
.toLowerCase()
|
|
141
|
+
.replace(/^_/, '');
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
exports.HasRelationshipsMixin = HasRelationshipsMixin;
|
|
145
|
+
//# sourceMappingURL=HasRelationships.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"HasRelationships.js","sourceRoot":"","sources":["../../../../src/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KAAK;aACT,OAAO,CAAC,UAAU,EAAE,KAAK,CAAC;aAC1B,WAAW,EAAE;aACb,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;IACvB,CAAC;CACF;AA5MD,sDA4MC"}
|
|
@@ -7,7 +7,8 @@
|
|
|
7
7
|
import { DatabaseManager } from '../DatabaseManager';
|
|
8
8
|
import { Connection } from '../Connection';
|
|
9
9
|
import { EnsembleBuilder } from './EnsembleBuilder';
|
|
10
|
-
|
|
10
|
+
import { HasRelationshipsMixin } from './Concerns/HasRelationships';
|
|
11
|
+
export declare abstract class Ensemble extends HasRelationshipsMixin {
|
|
11
12
|
/**
|
|
12
13
|
* The connection resolver instance
|
|
13
14
|
*/
|
|
@@ -262,5 +263,21 @@ export declare abstract class Ensemble {
|
|
|
262
263
|
* Pluralize a string (simple implementation)
|
|
263
264
|
*/
|
|
264
265
|
protected pluralize(value: string): string;
|
|
266
|
+
/**
|
|
267
|
+
* Create a new query instance for the model
|
|
268
|
+
*/
|
|
269
|
+
newQuery(): EnsembleBuilder<any>;
|
|
270
|
+
/**
|
|
271
|
+
* Create a new instance of the given model
|
|
272
|
+
*/
|
|
273
|
+
newInstance(attributes?: Record<string, any>, exists?: boolean): this;
|
|
274
|
+
/**
|
|
275
|
+
* Get the name of the "updated at" column
|
|
276
|
+
*/
|
|
277
|
+
getUpdatedAtColumn(): string | null;
|
|
278
|
+
/**
|
|
279
|
+
* Get the name of the "created at" column
|
|
280
|
+
*/
|
|
281
|
+
getCreatedAtColumn(): string | null;
|
|
265
282
|
}
|
|
266
283
|
//# sourceMappingURL=Ensemble.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Ensemble.d.ts","sourceRoot":"","sources":["../../../src/Database/Ensemble/Ensemble.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC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YAAY,IAAI,IAAI;IAK9B;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC;IAkBhC;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAiB9B;;OAEG;IACH,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAOvC;;OAEG;IACH,SAAS,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAOtC;;OAEG;IACH,SAAS,CAAC,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM;IAU1C;;OAEG;IACH,QAAQ,IAAI,eAAe,CAAC,GAAG,CAAC;IAIhC;;OAEG;IACH,WAAW,CAAC,UAAU,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,EAAE,MAAM,GAAE,OAAe,GAAG,IAAI;IAYhF;;OAEG;IACH,kBAAkB,IAAI,MAAM,GAAG,IAAI;IAInC;;OAEG;IACH,kBAAkB,IAAI,MAAM,GAAG,IAAI;CAGpC"}
|
|
@@ -8,7 +8,8 @@
|
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.Ensemble = void 0;
|
|
10
10
|
const EnsembleBuilder_1 = require("./EnsembleBuilder");
|
|
11
|
-
|
|
11
|
+
const HasRelationships_1 = require("./Concerns/HasRelationships");
|
|
12
|
+
class Ensemble extends HasRelationships_1.HasRelationshipsMixin {
|
|
12
13
|
/**
|
|
13
14
|
* The connection resolver instance
|
|
14
15
|
*/
|
|
@@ -93,6 +94,7 @@ class Ensemble {
|
|
|
93
94
|
* Create a new Eloquent model instance
|
|
94
95
|
*/
|
|
95
96
|
constructor(attributes = {}) {
|
|
97
|
+
super();
|
|
96
98
|
this.fill(attributes);
|
|
97
99
|
}
|
|
98
100
|
/**
|
|
@@ -219,6 +221,18 @@ class Ensemble {
|
|
|
219
221
|
for (const key of this.appends) {
|
|
220
222
|
attributes[key] = this.getAttribute(key);
|
|
221
223
|
}
|
|
224
|
+
// Add loaded relationships
|
|
225
|
+
for (const [key, value] of Object.entries(this.relations)) {
|
|
226
|
+
if (Array.isArray(value)) {
|
|
227
|
+
attributes[key] = value.map((model) => model.toObject());
|
|
228
|
+
}
|
|
229
|
+
else if (value && typeof value.toObject === 'function') {
|
|
230
|
+
attributes[key] = value.toObject();
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
attributes[key] = value;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
222
236
|
return attributes;
|
|
223
237
|
}
|
|
224
238
|
/**
|
|
@@ -519,6 +533,36 @@ class Ensemble {
|
|
|
519
533
|
}
|
|
520
534
|
return value + 's';
|
|
521
535
|
}
|
|
536
|
+
/**
|
|
537
|
+
* Create a new query instance for the model
|
|
538
|
+
*/
|
|
539
|
+
newQuery() {
|
|
540
|
+
return this.constructor.query();
|
|
541
|
+
}
|
|
542
|
+
/**
|
|
543
|
+
* Create a new instance of the given model
|
|
544
|
+
*/
|
|
545
|
+
newInstance(attributes = {}, exists = false) {
|
|
546
|
+
const ModelClass = this.constructor;
|
|
547
|
+
const instance = new ModelClass(attributes);
|
|
548
|
+
instance.exists = exists;
|
|
549
|
+
if (exists) {
|
|
550
|
+
instance.syncOriginal();
|
|
551
|
+
}
|
|
552
|
+
return instance;
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Get the name of the "updated at" column
|
|
556
|
+
*/
|
|
557
|
+
getUpdatedAtColumn() {
|
|
558
|
+
return this.timestamps ? this.constructor.UPDATED_AT : null;
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Get the name of the "created at" column
|
|
562
|
+
*/
|
|
563
|
+
getCreatedAtColumn() {
|
|
564
|
+
return this.timestamps ? this.constructor.CREATED_AT : null;
|
|
565
|
+
}
|
|
522
566
|
}
|
|
523
567
|
exports.Ensemble = Ensemble;
|
|
524
568
|
//# sourceMappingURL=Ensemble.js.map
|