@orchestr-sh/orchestr 1.5.9 → 1.5.11
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 +275 -977
- package/dist/Database/Ensemble/Concerns/HasDynamicRelationGetters.d.ts +27 -0
- package/dist/Database/Ensemble/Concerns/HasDynamicRelationGetters.d.ts.map +1 -0
- package/dist/Database/Ensemble/Concerns/HasDynamicRelationGetters.js +109 -0
- package/dist/Database/Ensemble/Concerns/HasDynamicRelationGetters.js.map +1 -0
- package/dist/Database/Ensemble/Concerns/HasDynamicRelations.d.ts +59 -3
- package/dist/Database/Ensemble/Concerns/HasDynamicRelations.d.ts.map +1 -1
- package/dist/Database/Ensemble/Concerns/HasDynamicRelations.js +159 -31
- package/dist/Database/Ensemble/Concerns/HasDynamicRelations.js.map +1 -1
- package/dist/Database/Ensemble/Concerns/HasRelationships.d.ts +20 -0
- package/dist/Database/Ensemble/Concerns/HasRelationships.d.ts.map +1 -1
- package/dist/Database/Ensemble/Concerns/HasRelationships.js +57 -0
- package/dist/Database/Ensemble/Concerns/HasRelationships.js.map +1 -1
- package/dist/Database/Ensemble/Ensemble.d.ts.map +1 -1
- package/dist/Database/Ensemble/Ensemble.js +0 -4
- package/dist/Database/Ensemble/Ensemble.js.map +1 -1
- package/dist/Database/Ensemble/EnsembleBuilder.d.ts +1 -1
- package/dist/Database/Ensemble/EnsembleBuilder.d.ts.map +1 -1
- package/dist/Database/Ensemble/EnsembleBuilder.js +7 -10
- package/dist/Database/Ensemble/EnsembleBuilder.js.map +1 -1
- package/dist/Database/Ensemble/Relations/BelongsTo.d.ts +5 -0
- package/dist/Database/Ensemble/Relations/BelongsTo.d.ts.map +1 -1
- package/dist/Database/Ensemble/Relations/BelongsTo.js +8 -0
- package/dist/Database/Ensemble/Relations/BelongsTo.js.map +1 -1
- package/dist/Database/Ensemble/Relations/MorphMany.d.ts +97 -0
- package/dist/Database/Ensemble/Relations/MorphMany.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphMany.js +189 -0
- package/dist/Database/Ensemble/Relations/MorphMany.js.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphMap.d.ts +59 -0
- package/dist/Database/Ensemble/Relations/MorphMap.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphMap.js +84 -0
- package/dist/Database/Ensemble/Relations/MorphMap.js.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphOne.d.ts +93 -0
- package/dist/Database/Ensemble/Relations/MorphOne.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphOne.js +179 -0
- package/dist/Database/Ensemble/Relations/MorphOne.js.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphTo.d.ts +98 -0
- package/dist/Database/Ensemble/Relations/MorphTo.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphTo.js +214 -0
- package/dist/Database/Ensemble/Relations/MorphTo.js.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphToMany.d.ts +73 -0
- package/dist/Database/Ensemble/Relations/MorphToMany.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphToMany.js +164 -0
- package/dist/Database/Ensemble/Relations/MorphToMany.js.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphedByMany.d.ts +29 -0
- package/dist/Database/Ensemble/Relations/MorphedByMany.d.ts.map +1 -0
- package/dist/Database/Ensemble/Relations/MorphedByMany.js +58 -0
- package/dist/Database/Ensemble/Relations/MorphedByMany.js.map +1 -0
- package/dist/Database/Ensemble/Relations/index.d.ts +6 -0
- package/dist/Database/Ensemble/Relations/index.d.ts.map +1 -1
- package/dist/Database/Ensemble/Relations/index.js +14 -1
- package/dist/Database/Ensemble/Relations/index.js.map +1 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* MorphMap
|
|
4
|
+
*
|
|
5
|
+
* Global morph map for polymorphic relations
|
|
6
|
+
* Maps aliases to model classes for cleaner database storage
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.MorphMap = void 0;
|
|
10
|
+
class MorphMap {
|
|
11
|
+
/**
|
|
12
|
+
* Map from alias to model class
|
|
13
|
+
*/
|
|
14
|
+
static map = new Map();
|
|
15
|
+
/**
|
|
16
|
+
* Reverse map from model class to alias
|
|
17
|
+
*/
|
|
18
|
+
static reverseMap = new Map();
|
|
19
|
+
/**
|
|
20
|
+
* Set the morph map
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* MorphMap.set({
|
|
24
|
+
* 'post': Post,
|
|
25
|
+
* 'video': Video,
|
|
26
|
+
* 'comment': Comment
|
|
27
|
+
* });
|
|
28
|
+
*/
|
|
29
|
+
static set(map) {
|
|
30
|
+
this.map.clear();
|
|
31
|
+
this.reverseMap.clear();
|
|
32
|
+
for (const [alias, modelClass] of Object.entries(map)) {
|
|
33
|
+
this.map.set(alias, modelClass);
|
|
34
|
+
this.reverseMap.set(modelClass, alias);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Get model class from alias
|
|
39
|
+
*/
|
|
40
|
+
static getClass(alias) {
|
|
41
|
+
return this.map.get(alias);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Get alias from model class
|
|
45
|
+
* Returns the class name if no alias is defined
|
|
46
|
+
*/
|
|
47
|
+
static getAlias(modelClass) {
|
|
48
|
+
return this.reverseMap.get(modelClass) || modelClass.name;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Get the morph type for a model instance
|
|
52
|
+
* This is what gets stored in the database {name}_type column
|
|
53
|
+
*/
|
|
54
|
+
static getMorphedModel(instance) {
|
|
55
|
+
return this.getAlias(instance.constructor);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Clear the morph map
|
|
59
|
+
*/
|
|
60
|
+
static clear() {
|
|
61
|
+
this.map.clear();
|
|
62
|
+
this.reverseMap.clear();
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Check if an alias exists in the map
|
|
66
|
+
*/
|
|
67
|
+
static has(alias) {
|
|
68
|
+
return this.map.has(alias);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Get all registered aliases
|
|
72
|
+
*/
|
|
73
|
+
static aliases() {
|
|
74
|
+
return Array.from(this.map.keys());
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get all registered model classes
|
|
78
|
+
*/
|
|
79
|
+
static classes() {
|
|
80
|
+
return Array.from(this.map.values());
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
exports.MorphMap = MorphMap;
|
|
84
|
+
//# sourceMappingURL=MorphMap.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MorphMap.js","sourceRoot":"","sources":["../../../../src/Database/Ensemble/Relations/MorphMap.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAIH,MAAa,QAAQ;IACnB;;OAEG;IACK,MAAM,CAAC,GAAG,GAAoC,IAAI,GAAG,EAAE,CAAC;IAEhE;;OAEG;IACK,MAAM,CAAC,UAAU,GAAoC,IAAI,GAAG,EAAE,CAAC;IAEvE;;;;;;;;;OASG;IACH,MAAM,CAAC,GAAG,CAAC,GAAuC;QAChD,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;QACjB,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;QAExB,KAAK,MAAM,CAAC,KAAK,EAAE,UAAU,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC;YACtD,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,CAAC;YAChC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC;QACzC,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,QAAQ,CAAC,KAAa;QAC3B,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,QAAQ,CAAC,UAA8B;QAC5C,OAAO,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,UAAU,CAAC,IAAI,UAAU,CAAC,IAAI,CAAC;IAC5D,CAAC;IAED;;;OAGG;IACH,MAAM,CAAC,eAAe,CAAC,QAAkB;QACvC,OAAO,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,WAAiC,CAAC,CAAC;IACnE,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,KAAK;QACV,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;QACjB,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;IAC1B,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,GAAG,CAAC,KAAa;QACtB,OAAO,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC7B,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,OAAO;QACZ,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,CAAC,CAAC;IACrC,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,OAAO;QACZ,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,CAAC,CAAC;IACvC,CAAC;;AAjFH,4BAkFC"}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MorphOne Relationship
|
|
3
|
+
*
|
|
4
|
+
* Represents a polymorphic one-to-one relationship
|
|
5
|
+
* Example: Post has one Image (where Image belongs to many types)
|
|
6
|
+
*/
|
|
7
|
+
import { Ensemble } from '../Ensemble';
|
|
8
|
+
import { EnsembleBuilder } from '../EnsembleBuilder';
|
|
9
|
+
import { EnsembleCollection } from '../EnsembleCollection';
|
|
10
|
+
import { Relation } from './Relation';
|
|
11
|
+
export declare class MorphOne<TRelated extends Ensemble, TParent extends Ensemble> extends Relation<TRelated, TParent> {
|
|
12
|
+
/**
|
|
13
|
+
* The foreign key of the parent model (e.g., 'imageable_id')
|
|
14
|
+
*/
|
|
15
|
+
protected foreignKey: string;
|
|
16
|
+
/**
|
|
17
|
+
* The morph type column (e.g., 'imageable_type')
|
|
18
|
+
*/
|
|
19
|
+
protected morphType: string;
|
|
20
|
+
/**
|
|
21
|
+
* The local key of the parent model
|
|
22
|
+
*/
|
|
23
|
+
protected localKey: string;
|
|
24
|
+
/**
|
|
25
|
+
* The morph class name or alias
|
|
26
|
+
*/
|
|
27
|
+
protected morphClass: string;
|
|
28
|
+
/**
|
|
29
|
+
* Create a new morph one relationship instance
|
|
30
|
+
*/
|
|
31
|
+
constructor(query: EnsembleBuilder<TRelated>, parent: TParent, name: string, type: string | null, id: string | null, localKey: string);
|
|
32
|
+
/**
|
|
33
|
+
* Set the base constraints on the relation query
|
|
34
|
+
*/
|
|
35
|
+
addConstraints(): void;
|
|
36
|
+
/**
|
|
37
|
+
* Set the constraints for an eager load of the relation
|
|
38
|
+
*/
|
|
39
|
+
addEagerConstraints(models: TParent[]): void;
|
|
40
|
+
/**
|
|
41
|
+
* Initialize the relation on a set of models
|
|
42
|
+
*/
|
|
43
|
+
initRelation(models: TParent[], relation: string): TParent[];
|
|
44
|
+
/**
|
|
45
|
+
* Match the eagerly loaded results to their parents
|
|
46
|
+
*/
|
|
47
|
+
match(models: TParent[], results: EnsembleCollection<TRelated>, relation: string): TParent[];
|
|
48
|
+
/**
|
|
49
|
+
* Build model dictionary keyed by the relation's foreign key
|
|
50
|
+
*/
|
|
51
|
+
protected buildDictionary(results: EnsembleCollection<TRelated>): Record<any, TRelated[]>;
|
|
52
|
+
/**
|
|
53
|
+
* Get the results of the relationship
|
|
54
|
+
*/
|
|
55
|
+
getResults(): Promise<TRelated | null>;
|
|
56
|
+
/**
|
|
57
|
+
* Get the key value of the parent's local key
|
|
58
|
+
*/
|
|
59
|
+
protected getParentKey(): any;
|
|
60
|
+
/**
|
|
61
|
+
* Get the morph class for the parent
|
|
62
|
+
*/
|
|
63
|
+
protected getMorphClass(): string;
|
|
64
|
+
/**
|
|
65
|
+
* Get the foreign key for the relationship
|
|
66
|
+
*/
|
|
67
|
+
getForeignKeyName(): string;
|
|
68
|
+
/**
|
|
69
|
+
* Get the morph type for the relationship
|
|
70
|
+
*/
|
|
71
|
+
getMorphType(): string;
|
|
72
|
+
/**
|
|
73
|
+
* Get the local key for the relationship
|
|
74
|
+
*/
|
|
75
|
+
getLocalKeyName(): string;
|
|
76
|
+
/**
|
|
77
|
+
* Make a new related instance
|
|
78
|
+
*/
|
|
79
|
+
make(attributes?: Record<string, any>): TRelated;
|
|
80
|
+
/**
|
|
81
|
+
* Create a new instance of the related model
|
|
82
|
+
*/
|
|
83
|
+
create(attributes?: Record<string, any>): Promise<TRelated>;
|
|
84
|
+
/**
|
|
85
|
+
* Save a model and set the foreign key and morph type
|
|
86
|
+
*/
|
|
87
|
+
save(model: TRelated): Promise<TRelated>;
|
|
88
|
+
/**
|
|
89
|
+
* Update the parent model on the relationship
|
|
90
|
+
*/
|
|
91
|
+
update(attributes: Record<string, any>): Promise<number>;
|
|
92
|
+
}
|
|
93
|
+
//# sourceMappingURL=MorphOne.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MorphOne.d.ts","sourceRoot":"","sources":["../../../../src/Database/Ensemble/Relations/MorphOne.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE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;IAIjC;;OAEG;IACH,iBAAiB,IAAI,MAAM;IAI3B;;OAEG;IACH,YAAY,IAAI,MAAM;IAItB;;OAEG;IACH,eAAe,IAAI,MAAM;IAIzB;;OAEG;IACH,IAAI,CAAC,UAAU,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,GAAG,QAAQ;IAIpD;;OAEG;IACG,MAAM,CAAC,UAAU,GAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAM,GAAG,OAAO,CAAC,QAAQ,CAAC;IAYrE;;OAEG;IACG,IAAI,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;IAS9C;;OAEG;IACG,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,OAAO,CAAC,MAAM,CAAC;CAQ/D"}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* MorphOne Relationship
|
|
4
|
+
*
|
|
5
|
+
* Represents a polymorphic one-to-one relationship
|
|
6
|
+
* Example: Post has one Image (where Image belongs to many types)
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.MorphOne = void 0;
|
|
10
|
+
const Relation_1 = require("./Relation");
|
|
11
|
+
const MorphMap_1 = require("./MorphMap");
|
|
12
|
+
class MorphOne extends Relation_1.Relation {
|
|
13
|
+
/**
|
|
14
|
+
* The foreign key of the parent model (e.g., 'imageable_id')
|
|
15
|
+
*/
|
|
16
|
+
foreignKey;
|
|
17
|
+
/**
|
|
18
|
+
* The morph type column (e.g., 'imageable_type')
|
|
19
|
+
*/
|
|
20
|
+
morphType;
|
|
21
|
+
/**
|
|
22
|
+
* The local key of the parent model
|
|
23
|
+
*/
|
|
24
|
+
localKey;
|
|
25
|
+
/**
|
|
26
|
+
* The morph class name or alias
|
|
27
|
+
*/
|
|
28
|
+
morphClass;
|
|
29
|
+
/**
|
|
30
|
+
* Create a new morph one relationship instance
|
|
31
|
+
*/
|
|
32
|
+
constructor(query, parent, name, type, id, localKey) {
|
|
33
|
+
super(query, parent);
|
|
34
|
+
this.morphType = type || `${name}_type`;
|
|
35
|
+
this.foreignKey = id || `${name}_id`;
|
|
36
|
+
this.localKey = localKey;
|
|
37
|
+
this.morphClass = this.getMorphClass();
|
|
38
|
+
this.initializeRelation();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Set the base constraints on the relation query
|
|
42
|
+
*/
|
|
43
|
+
addConstraints() {
|
|
44
|
+
if (Relation_1.Relation['constraints']) {
|
|
45
|
+
const parentKey = this.getParentKey();
|
|
46
|
+
// Only add constraints if parent key exists
|
|
47
|
+
if (parentKey !== null && parentKey !== undefined) {
|
|
48
|
+
this.query
|
|
49
|
+
.where(this.foreignKey, '=', parentKey)
|
|
50
|
+
.where(this.morphType, '=', this.morphClass);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Set the constraints for an eager load of the relation
|
|
56
|
+
*/
|
|
57
|
+
addEagerConstraints(models) {
|
|
58
|
+
const keys = this.getKeys(models, this.localKey);
|
|
59
|
+
this.query
|
|
60
|
+
.whereIn(this.foreignKey, keys)
|
|
61
|
+
.where(this.morphType, '=', this.morphClass);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Initialize the relation on a set of models
|
|
65
|
+
*/
|
|
66
|
+
initRelation(models, relation) {
|
|
67
|
+
for (const model of models) {
|
|
68
|
+
model.setRelation(relation, null);
|
|
69
|
+
}
|
|
70
|
+
return models;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Match the eagerly loaded results to their parents
|
|
74
|
+
*/
|
|
75
|
+
match(models, results, relation) {
|
|
76
|
+
const dictionary = this.buildDictionary(results);
|
|
77
|
+
for (const model of models) {
|
|
78
|
+
const key = model.getAttribute(this.localKey);
|
|
79
|
+
if (key !== null && key !== undefined && dictionary[key]) {
|
|
80
|
+
model.setRelation(relation, dictionary[key][0]);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return models;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Build model dictionary keyed by the relation's foreign key
|
|
87
|
+
*/
|
|
88
|
+
buildDictionary(results) {
|
|
89
|
+
const dictionary = {};
|
|
90
|
+
for (const result of results) {
|
|
91
|
+
const key = result.getAttribute(this.foreignKey);
|
|
92
|
+
if (key !== null && key !== undefined) {
|
|
93
|
+
if (!dictionary[key]) {
|
|
94
|
+
dictionary[key] = [];
|
|
95
|
+
}
|
|
96
|
+
dictionary[key].push(result);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return dictionary;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Get the results of the relationship
|
|
103
|
+
*/
|
|
104
|
+
async getResults() {
|
|
105
|
+
if (!this.getParentKey()) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
this.ensureConstraints();
|
|
109
|
+
return this.query.first();
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Get the key value of the parent's local key
|
|
113
|
+
*/
|
|
114
|
+
getParentKey() {
|
|
115
|
+
return this.parent.getAttribute(this.localKey);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get the morph class for the parent
|
|
119
|
+
*/
|
|
120
|
+
getMorphClass() {
|
|
121
|
+
return MorphMap_1.MorphMap.getMorphedModel(this.parent);
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Get the foreign key for the relationship
|
|
125
|
+
*/
|
|
126
|
+
getForeignKeyName() {
|
|
127
|
+
return this.foreignKey;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Get the morph type for the relationship
|
|
131
|
+
*/
|
|
132
|
+
getMorphType() {
|
|
133
|
+
return this.morphType;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Get the local key for the relationship
|
|
137
|
+
*/
|
|
138
|
+
getLocalKeyName() {
|
|
139
|
+
return this.localKey;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Make a new related instance
|
|
143
|
+
*/
|
|
144
|
+
make(attributes = {}) {
|
|
145
|
+
return this.related.newInstance(attributes);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Create a new instance of the related model
|
|
149
|
+
*/
|
|
150
|
+
async create(attributes = {}) {
|
|
151
|
+
const instance = this.make(attributes);
|
|
152
|
+
// Set both the foreign key and morph type
|
|
153
|
+
instance.setAttribute(this.foreignKey, this.getParentKey());
|
|
154
|
+
instance.setAttribute(this.morphType, this.morphClass);
|
|
155
|
+
await instance.save();
|
|
156
|
+
return instance;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Save a model and set the foreign key and morph type
|
|
160
|
+
*/
|
|
161
|
+
async save(model) {
|
|
162
|
+
model.setAttribute(this.foreignKey, this.getParentKey());
|
|
163
|
+
model.setAttribute(this.morphType, this.morphClass);
|
|
164
|
+
await model.save();
|
|
165
|
+
return model;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Update the parent model on the relationship
|
|
169
|
+
*/
|
|
170
|
+
async update(attributes) {
|
|
171
|
+
const updatedAtColumn = this.related.getUpdatedAtColumn();
|
|
172
|
+
if (updatedAtColumn) {
|
|
173
|
+
attributes[updatedAtColumn] = new Date();
|
|
174
|
+
}
|
|
175
|
+
return this.query.update(attributes);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
exports.MorphOne = MorphOne;
|
|
179
|
+
//# sourceMappingURL=MorphOne.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MorphOne.js","sourceRoot":"","sources":["../../../../src/Database/Ensemble/Relations/MorphOne.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;AAKH,yCAAsC;AACtC,yCAAsC;AAEtC,MAAa,QAA8D,SAAQ,mBAA2B;IAC5G;;OAEG;I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EG;IACH,KAAK,CAAC,IAAI,CAAC,KAAe;QACxB,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,UAAU,EAAE,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC;QACzD,KAAK,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;QAEpD,MAAM,KAAK,CAAC,IAAI,EAAE,CAAC;QAEnB,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,MAAM,CAAC,UAA+B;QAC1C,MAAM,eAAe,GAAG,IAAI,CAAC,OAAO,CAAC,kBAAkB,EAAE,CAAC;QAC1D,IAAI,eAAe,EAAE,CAAC;YACpB,UAAU,CAAC,eAAe,CAAC,GAAG,IAAI,IAAI,EAAE,CAAC;QAC3C,CAAC;QAED,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;IACvC,CAAC;CACF;AAhND,4BAgNC"}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MorphTo Relationship
|
|
3
|
+
*
|
|
4
|
+
* Represents a polymorphic inverse relationship
|
|
5
|
+
* Example: Comment belongs to Post or Video (commentable)
|
|
6
|
+
*/
|
|
7
|
+
import { Ensemble } from '../Ensemble';
|
|
8
|
+
import { EnsembleBuilder } from '../EnsembleBuilder';
|
|
9
|
+
import { EnsembleCollection } from '../EnsembleCollection';
|
|
10
|
+
import { Relation } from './Relation';
|
|
11
|
+
export declare class MorphTo<TParent extends Ensemble> extends Relation<Ensemble, TParent> {
|
|
12
|
+
/**
|
|
13
|
+
* The foreign key of the parent model (e.g., 'commentable_id')
|
|
14
|
+
*/
|
|
15
|
+
protected foreignKey: string;
|
|
16
|
+
/**
|
|
17
|
+
* The morph type column (e.g., 'commentable_type')
|
|
18
|
+
*/
|
|
19
|
+
protected morphType: string;
|
|
20
|
+
/**
|
|
21
|
+
* The owner key on the related model
|
|
22
|
+
*/
|
|
23
|
+
protected ownerKey: string;
|
|
24
|
+
/**
|
|
25
|
+
* The name of the relationship
|
|
26
|
+
*/
|
|
27
|
+
protected relationName: string;
|
|
28
|
+
/**
|
|
29
|
+
* Dictionary of models grouped by type
|
|
30
|
+
*/
|
|
31
|
+
protected dictionary: Map<string, Map<any, Ensemble>>;
|
|
32
|
+
/**
|
|
33
|
+
* Models loaded by type
|
|
34
|
+
*/
|
|
35
|
+
protected models: Map<string, Ensemble[]>;
|
|
36
|
+
/**
|
|
37
|
+
* Create a new morph to relationship instance
|
|
38
|
+
*/
|
|
39
|
+
constructor(query: EnsembleBuilder<Ensemble>, parent: TParent, foreignKey: string, morphType: string, ownerKey: string, relationName: string);
|
|
40
|
+
/**
|
|
41
|
+
* Set the base constraints on the relation query
|
|
42
|
+
* MorphTo doesn't use base constraints - queries are built per type
|
|
43
|
+
*/
|
|
44
|
+
addConstraints(): void;
|
|
45
|
+
/**
|
|
46
|
+
* Set the constraints for an eager load of the relation
|
|
47
|
+
*/
|
|
48
|
+
addEagerConstraints(models: TParent[]): void;
|
|
49
|
+
/**
|
|
50
|
+
* Build dictionary of models grouped by morph type
|
|
51
|
+
*/
|
|
52
|
+
protected buildDictionary(models: TParent[]): void;
|
|
53
|
+
/**
|
|
54
|
+
* Initialize the relation on a set of models
|
|
55
|
+
*/
|
|
56
|
+
initRelation(models: TParent[], relation: string): TParent[];
|
|
57
|
+
/**
|
|
58
|
+
* Match the eagerly loaded results to their parents
|
|
59
|
+
*/
|
|
60
|
+
match(models: TParent[], results: EnsembleCollection<Ensemble>, relation: string): TParent[];
|
|
61
|
+
/**
|
|
62
|
+
* Get the eager loading results
|
|
63
|
+
*/
|
|
64
|
+
getEager(): Promise<Ensemble[]>;
|
|
65
|
+
/**
|
|
66
|
+
* Get the results of the relationship
|
|
67
|
+
*/
|
|
68
|
+
getResults(): Promise<Ensemble | null>;
|
|
69
|
+
/**
|
|
70
|
+
* Associate the model instance to the parent model
|
|
71
|
+
*/
|
|
72
|
+
associate(model: Ensemble | null): this;
|
|
73
|
+
/**
|
|
74
|
+
* Dissociate the model from the parent
|
|
75
|
+
*/
|
|
76
|
+
dissociate(): this;
|
|
77
|
+
/**
|
|
78
|
+
* Get the foreign key value
|
|
79
|
+
*/
|
|
80
|
+
protected getForeignKeyValue(): any;
|
|
81
|
+
/**
|
|
82
|
+
* Get the morph type value
|
|
83
|
+
*/
|
|
84
|
+
protected getMorphTypeValue(): string | null;
|
|
85
|
+
/**
|
|
86
|
+
* Get the foreign key for the relationship
|
|
87
|
+
*/
|
|
88
|
+
getForeignKeyName(): string;
|
|
89
|
+
/**
|
|
90
|
+
* Get the morph type for the relationship
|
|
91
|
+
*/
|
|
92
|
+
getMorphType(): string;
|
|
93
|
+
/**
|
|
94
|
+
* Get the owner key for the relationship
|
|
95
|
+
*/
|
|
96
|
+
getOwnerKeyName(): string;
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=MorphTo.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MorphTo.d.ts","sourceRoot":"","sources":["../../../../src/Database/Ensemble/Relations/MorphTo.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAEH,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAGtC,qBAAa,OAAO,CAAC,OAAO,SAAS,QAAQ,CAAE,SAAQ,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAChF;;OAEG;IACH,SAAS,CAAC,UAAU,EAAE,MAAM,CAAC;IAE7B;;OAEG;IACH,SAAS,CAAC,SAAS,EAAE,MAAM,CAAC;IAE5B;;OAEG;IACH,SAAS,CAAC,QAAQ,EAAE,MAAM,CAAC;IAE3B;;OAEG;IACH,SAAS,CAAC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B,IAAI,MAAM;IAI3B;;OAEG;IACH,YAAY,IAAI,MAAM;IAItB;;OAEG;IACH,eAAe,IAAI,MAAM;CAG1B"}
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* MorphTo Relationship
|
|
4
|
+
*
|
|
5
|
+
* Represents a polymorphic inverse relationship
|
|
6
|
+
* Example: Comment belongs to Post or Video (commentable)
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.MorphTo = void 0;
|
|
10
|
+
const Relation_1 = require("./Relation");
|
|
11
|
+
const MorphMap_1 = require("./MorphMap");
|
|
12
|
+
class MorphTo extends Relation_1.Relation {
|
|
13
|
+
/**
|
|
14
|
+
* The foreign key of the parent model (e.g., 'commentable_id')
|
|
15
|
+
*/
|
|
16
|
+
foreignKey;
|
|
17
|
+
/**
|
|
18
|
+
* The morph type column (e.g., 'commentable_type')
|
|
19
|
+
*/
|
|
20
|
+
morphType;
|
|
21
|
+
/**
|
|
22
|
+
* The owner key on the related model
|
|
23
|
+
*/
|
|
24
|
+
ownerKey;
|
|
25
|
+
/**
|
|
26
|
+
* The name of the relationship
|
|
27
|
+
*/
|
|
28
|
+
relationName;
|
|
29
|
+
/**
|
|
30
|
+
* Dictionary of models grouped by type
|
|
31
|
+
*/
|
|
32
|
+
dictionary = new Map();
|
|
33
|
+
/**
|
|
34
|
+
* Models loaded by type
|
|
35
|
+
*/
|
|
36
|
+
models = new Map();
|
|
37
|
+
/**
|
|
38
|
+
* Create a new morph to relationship instance
|
|
39
|
+
*/
|
|
40
|
+
constructor(query, parent, foreignKey, morphType, ownerKey, relationName) {
|
|
41
|
+
super(query, parent);
|
|
42
|
+
this.foreignKey = foreignKey;
|
|
43
|
+
this.morphType = morphType;
|
|
44
|
+
this.ownerKey = ownerKey;
|
|
45
|
+
this.relationName = relationName;
|
|
46
|
+
// Don't call initializeRelation() - MorphTo doesn't add constraints in constructor
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Set the base constraints on the relation query
|
|
50
|
+
* MorphTo doesn't use base constraints - queries are built per type
|
|
51
|
+
*/
|
|
52
|
+
addConstraints() {
|
|
53
|
+
// MorphTo doesn't add constraints here
|
|
54
|
+
// Constraints are added dynamically based on type
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Set the constraints for an eager load of the relation
|
|
58
|
+
*/
|
|
59
|
+
addEagerConstraints(models) {
|
|
60
|
+
// Build dictionary grouped by type
|
|
61
|
+
this.buildDictionary(models);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Build dictionary of models grouped by morph type
|
|
65
|
+
*/
|
|
66
|
+
buildDictionary(models) {
|
|
67
|
+
this.dictionary.clear();
|
|
68
|
+
for (const model of models) {
|
|
69
|
+
const type = model.getAttribute(this.morphType);
|
|
70
|
+
const id = model.getAttribute(this.foreignKey);
|
|
71
|
+
if (!type || (id === null || id === undefined)) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (!this.dictionary.has(type)) {
|
|
75
|
+
this.dictionary.set(type, new Map());
|
|
76
|
+
}
|
|
77
|
+
const typeDict = this.dictionary.get(type);
|
|
78
|
+
typeDict.set(id, null); // Placeholder, will be replaced in match
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Initialize the relation on a set of models
|
|
83
|
+
*/
|
|
84
|
+
initRelation(models, relation) {
|
|
85
|
+
for (const model of models) {
|
|
86
|
+
model.setRelation(relation, null);
|
|
87
|
+
}
|
|
88
|
+
return models;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Match the eagerly loaded results to their parents
|
|
92
|
+
*/
|
|
93
|
+
match(models, results, relation) {
|
|
94
|
+
// Match each model with its related parent
|
|
95
|
+
for (const model of models) {
|
|
96
|
+
const type = model.getAttribute(this.morphType);
|
|
97
|
+
const id = model.getAttribute(this.foreignKey);
|
|
98
|
+
if (!type || (id === null || id === undefined)) {
|
|
99
|
+
model.setRelation(relation, null);
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
const typeDict = this.dictionary.get(type);
|
|
103
|
+
if (typeDict && typeDict.has(id)) {
|
|
104
|
+
const related = typeDict.get(id);
|
|
105
|
+
model.setRelation(relation, related || null);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
model.setRelation(relation, null);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return models;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Get the eager loading results
|
|
115
|
+
*/
|
|
116
|
+
async getEager() {
|
|
117
|
+
const allResults = [];
|
|
118
|
+
for (const [type, ids] of this.dictionary.entries()) {
|
|
119
|
+
const modelClass = MorphMap_1.MorphMap.getClass(type);
|
|
120
|
+
if (!modelClass) {
|
|
121
|
+
console.warn(`MorphTo: Unknown morph type "${type}". Did you forget to register it in MorphMap?`);
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const instance = new modelClass();
|
|
125
|
+
const models = await instance
|
|
126
|
+
.newQuery()
|
|
127
|
+
.whereIn(this.ownerKey, Array.from(ids.keys()))
|
|
128
|
+
.get();
|
|
129
|
+
// Store in dictionary for matching
|
|
130
|
+
for (const model of models) {
|
|
131
|
+
const key = model.getAttribute(this.ownerKey);
|
|
132
|
+
const typeDict = this.dictionary.get(type);
|
|
133
|
+
if (typeDict && typeDict.has(key)) {
|
|
134
|
+
typeDict.set(key, model);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
allResults.push(...models);
|
|
138
|
+
}
|
|
139
|
+
return allResults;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Get the results of the relationship
|
|
143
|
+
*/
|
|
144
|
+
async getResults() {
|
|
145
|
+
const type = this.parent.getAttribute(this.morphType);
|
|
146
|
+
const id = this.parent.getAttribute(this.foreignKey);
|
|
147
|
+
if (!type || (id === null || id === undefined)) {
|
|
148
|
+
return null;
|
|
149
|
+
}
|
|
150
|
+
const modelClass = MorphMap_1.MorphMap.getClass(type);
|
|
151
|
+
if (!modelClass) {
|
|
152
|
+
console.warn(`MorphTo: Unknown morph type "${type}". Did you forget to register it in MorphMap?`);
|
|
153
|
+
return null;
|
|
154
|
+
}
|
|
155
|
+
const instance = new modelClass();
|
|
156
|
+
return instance
|
|
157
|
+
.newQuery()
|
|
158
|
+
.where(this.ownerKey, '=', id)
|
|
159
|
+
.first();
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* Associate the model instance to the parent model
|
|
163
|
+
*/
|
|
164
|
+
associate(model) {
|
|
165
|
+
if (model === null) {
|
|
166
|
+
return this.dissociate();
|
|
167
|
+
}
|
|
168
|
+
this.parent.setAttribute(this.foreignKey, model.getAttribute(this.ownerKey));
|
|
169
|
+
this.parent.setAttribute(this.morphType, MorphMap_1.MorphMap.getMorphedModel(model));
|
|
170
|
+
this.parent.setRelation(this.relationName, model);
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Dissociate the model from the parent
|
|
175
|
+
*/
|
|
176
|
+
dissociate() {
|
|
177
|
+
this.parent.setAttribute(this.foreignKey, null);
|
|
178
|
+
this.parent.setAttribute(this.morphType, null);
|
|
179
|
+
this.parent.setRelation(this.relationName, null);
|
|
180
|
+
return this;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Get the foreign key value
|
|
184
|
+
*/
|
|
185
|
+
getForeignKeyValue() {
|
|
186
|
+
return this.parent.getAttribute(this.foreignKey);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Get the morph type value
|
|
190
|
+
*/
|
|
191
|
+
getMorphTypeValue() {
|
|
192
|
+
return this.parent.getAttribute(this.morphType);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Get the foreign key for the relationship
|
|
196
|
+
*/
|
|
197
|
+
getForeignKeyName() {
|
|
198
|
+
return this.foreignKey;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Get the morph type for the relationship
|
|
202
|
+
*/
|
|
203
|
+
getMorphType() {
|
|
204
|
+
return this.morphType;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Get the owner key for the relationship
|
|
208
|
+
*/
|
|
209
|
+
getOwnerKeyName() {
|
|
210
|
+
return this.ownerKey;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
exports.MorphTo = MorphTo;
|
|
214
|
+
//# sourceMappingURL=MorphTo.js.map
|