metal-orm 1.0.105 → 1.0.106
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/dist/index.cjs +86 -130
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -6
- package/dist/index.d.ts +8 -6
- package/dist/index.js +86 -130
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/orm/entity-relations.ts +7 -30
- package/src/orm/entity.ts +151 -162
- package/src/orm/relations/belongs-to.ts +126 -130
- package/src/orm/relations/has-many.ts +186 -190
- package/src/orm/relations/has-one.ts +158 -162
- package/src/orm/relations/many-to-many.ts +224 -228
package/dist/index.d.cts
CHANGED
|
@@ -6463,7 +6463,6 @@ type Rows$2 = Record<string, unknown>[];
|
|
|
6463
6463
|
* @template TChild - The type of child entities in the collection
|
|
6464
6464
|
*/
|
|
6465
6465
|
declare class DefaultHasManyCollection<TChild> implements HasManyCollection<TChild> {
|
|
6466
|
-
#private;
|
|
6467
6466
|
private readonly ctx;
|
|
6468
6467
|
private readonly meta;
|
|
6469
6468
|
private readonly root;
|
|
@@ -6473,6 +6472,10 @@ declare class DefaultHasManyCollection<TChild> implements HasManyCollection<TChi
|
|
|
6473
6472
|
private readonly loader;
|
|
6474
6473
|
private readonly createEntity;
|
|
6475
6474
|
private readonly localKey;
|
|
6475
|
+
private loaded;
|
|
6476
|
+
private items;
|
|
6477
|
+
private readonly added;
|
|
6478
|
+
private readonly removed;
|
|
6476
6479
|
/**
|
|
6477
6480
|
* Creates a new DefaultHasManyCollection instance.
|
|
6478
6481
|
* @param ctx - The entity context
|
|
@@ -6524,7 +6527,6 @@ declare class DefaultHasManyCollection<TChild> implements HasManyCollection<TChi
|
|
|
6524
6527
|
* Clears all entities from the collection.
|
|
6525
6528
|
*/
|
|
6526
6529
|
clear(): void;
|
|
6527
|
-
isLoaded(): boolean;
|
|
6528
6530
|
private get relationKey();
|
|
6529
6531
|
private hydrateFromCache;
|
|
6530
6532
|
/**
|
|
@@ -6542,7 +6544,6 @@ type Rows$1 = Record<string, unknown>;
|
|
|
6542
6544
|
* @template TParent The type of the parent entity.
|
|
6543
6545
|
*/
|
|
6544
6546
|
declare class DefaultBelongsToReference<TParent extends object> implements BelongsToReferenceApi<TParent> {
|
|
6545
|
-
#private;
|
|
6546
6547
|
private readonly ctx;
|
|
6547
6548
|
private readonly meta;
|
|
6548
6549
|
private readonly root;
|
|
@@ -6552,6 +6553,8 @@ declare class DefaultBelongsToReference<TParent extends object> implements Belon
|
|
|
6552
6553
|
private readonly loader;
|
|
6553
6554
|
private readonly createEntity;
|
|
6554
6555
|
private readonly targetKey;
|
|
6556
|
+
private loaded;
|
|
6557
|
+
private current;
|
|
6555
6558
|
/**
|
|
6556
6559
|
* @param ctx The entity context for tracking changes.
|
|
6557
6560
|
* @param meta Metadata for the child entity.
|
|
@@ -6567,7 +6570,6 @@ declare class DefaultBelongsToReference<TParent extends object> implements Belon
|
|
|
6567
6570
|
load(): Promise<TParent | null>;
|
|
6568
6571
|
get(): TParent | null;
|
|
6569
6572
|
set(data: Partial<TParent> | TParent | null): TParent | null;
|
|
6570
|
-
isLoaded(): boolean;
|
|
6571
6573
|
private get relationKey();
|
|
6572
6574
|
private populateFromHydrationCache;
|
|
6573
6575
|
toJSON(): TParent | null;
|
|
@@ -6582,7 +6584,6 @@ type Rows = Record<string, unknown>[];
|
|
|
6582
6584
|
* @template TTarget The type of the target entities in the collection.
|
|
6583
6585
|
*/
|
|
6584
6586
|
declare class DefaultManyToManyCollection<TTarget, TPivot extends object | undefined = undefined> implements ManyToManyCollection<TTarget, TPivot> {
|
|
6585
|
-
#private;
|
|
6586
6587
|
private readonly ctx;
|
|
6587
6588
|
private readonly meta;
|
|
6588
6589
|
private readonly root;
|
|
@@ -6592,6 +6593,8 @@ declare class DefaultManyToManyCollection<TTarget, TPivot extends object | undef
|
|
|
6592
6593
|
private readonly loader;
|
|
6593
6594
|
private readonly createEntity;
|
|
6594
6595
|
private readonly localKey;
|
|
6596
|
+
private loaded;
|
|
6597
|
+
private items;
|
|
6595
6598
|
/**
|
|
6596
6599
|
* @param ctx The entity context for tracking changes.
|
|
6597
6600
|
* @param meta Metadata for the root entity.
|
|
@@ -6640,7 +6643,6 @@ declare class DefaultManyToManyCollection<TTarget, TPivot extends object | undef
|
|
|
6640
6643
|
* @param ids Array of primary key values to sync with.
|
|
6641
6644
|
*/
|
|
6642
6645
|
syncByIds(ids: (number | string)[]): Promise<void>;
|
|
6643
|
-
isLoaded(): boolean;
|
|
6644
6646
|
private ensureEntity;
|
|
6645
6647
|
private extractId;
|
|
6646
6648
|
private get relationKey();
|
package/dist/index.d.ts
CHANGED
|
@@ -6463,7 +6463,6 @@ type Rows$2 = Record<string, unknown>[];
|
|
|
6463
6463
|
* @template TChild - The type of child entities in the collection
|
|
6464
6464
|
*/
|
|
6465
6465
|
declare class DefaultHasManyCollection<TChild> implements HasManyCollection<TChild> {
|
|
6466
|
-
#private;
|
|
6467
6466
|
private readonly ctx;
|
|
6468
6467
|
private readonly meta;
|
|
6469
6468
|
private readonly root;
|
|
@@ -6473,6 +6472,10 @@ declare class DefaultHasManyCollection<TChild> implements HasManyCollection<TChi
|
|
|
6473
6472
|
private readonly loader;
|
|
6474
6473
|
private readonly createEntity;
|
|
6475
6474
|
private readonly localKey;
|
|
6475
|
+
private loaded;
|
|
6476
|
+
private items;
|
|
6477
|
+
private readonly added;
|
|
6478
|
+
private readonly removed;
|
|
6476
6479
|
/**
|
|
6477
6480
|
* Creates a new DefaultHasManyCollection instance.
|
|
6478
6481
|
* @param ctx - The entity context
|
|
@@ -6524,7 +6527,6 @@ declare class DefaultHasManyCollection<TChild> implements HasManyCollection<TChi
|
|
|
6524
6527
|
* Clears all entities from the collection.
|
|
6525
6528
|
*/
|
|
6526
6529
|
clear(): void;
|
|
6527
|
-
isLoaded(): boolean;
|
|
6528
6530
|
private get relationKey();
|
|
6529
6531
|
private hydrateFromCache;
|
|
6530
6532
|
/**
|
|
@@ -6542,7 +6544,6 @@ type Rows$1 = Record<string, unknown>;
|
|
|
6542
6544
|
* @template TParent The type of the parent entity.
|
|
6543
6545
|
*/
|
|
6544
6546
|
declare class DefaultBelongsToReference<TParent extends object> implements BelongsToReferenceApi<TParent> {
|
|
6545
|
-
#private;
|
|
6546
6547
|
private readonly ctx;
|
|
6547
6548
|
private readonly meta;
|
|
6548
6549
|
private readonly root;
|
|
@@ -6552,6 +6553,8 @@ declare class DefaultBelongsToReference<TParent extends object> implements Belon
|
|
|
6552
6553
|
private readonly loader;
|
|
6553
6554
|
private readonly createEntity;
|
|
6554
6555
|
private readonly targetKey;
|
|
6556
|
+
private loaded;
|
|
6557
|
+
private current;
|
|
6555
6558
|
/**
|
|
6556
6559
|
* @param ctx The entity context for tracking changes.
|
|
6557
6560
|
* @param meta Metadata for the child entity.
|
|
@@ -6567,7 +6570,6 @@ declare class DefaultBelongsToReference<TParent extends object> implements Belon
|
|
|
6567
6570
|
load(): Promise<TParent | null>;
|
|
6568
6571
|
get(): TParent | null;
|
|
6569
6572
|
set(data: Partial<TParent> | TParent | null): TParent | null;
|
|
6570
|
-
isLoaded(): boolean;
|
|
6571
6573
|
private get relationKey();
|
|
6572
6574
|
private populateFromHydrationCache;
|
|
6573
6575
|
toJSON(): TParent | null;
|
|
@@ -6582,7 +6584,6 @@ type Rows = Record<string, unknown>[];
|
|
|
6582
6584
|
* @template TTarget The type of the target entities in the collection.
|
|
6583
6585
|
*/
|
|
6584
6586
|
declare class DefaultManyToManyCollection<TTarget, TPivot extends object | undefined = undefined> implements ManyToManyCollection<TTarget, TPivot> {
|
|
6585
|
-
#private;
|
|
6586
6587
|
private readonly ctx;
|
|
6587
6588
|
private readonly meta;
|
|
6588
6589
|
private readonly root;
|
|
@@ -6592,6 +6593,8 @@ declare class DefaultManyToManyCollection<TTarget, TPivot extends object | undef
|
|
|
6592
6593
|
private readonly loader;
|
|
6593
6594
|
private readonly createEntity;
|
|
6594
6595
|
private readonly localKey;
|
|
6596
|
+
private loaded;
|
|
6597
|
+
private items;
|
|
6595
6598
|
/**
|
|
6596
6599
|
* @param ctx The entity context for tracking changes.
|
|
6597
6600
|
* @param meta Metadata for the root entity.
|
|
@@ -6640,7 +6643,6 @@ declare class DefaultManyToManyCollection<TTarget, TPivot extends object | undef
|
|
|
6640
6643
|
* @param ids Array of primary key values to sync with.
|
|
6641
6644
|
*/
|
|
6642
6645
|
syncByIds(ids: (number | string)[]): Promise<void>;
|
|
6643
|
-
isLoaded(): boolean;
|
|
6644
6646
|
private ensureEntity;
|
|
6645
6647
|
private extractId;
|
|
6646
6648
|
private get relationKey();
|
package/dist/index.js
CHANGED
|
@@ -4902,8 +4902,7 @@ var hideInternal = (obj, keys) => {
|
|
|
4902
4902
|
Object.defineProperty(obj, key, {
|
|
4903
4903
|
value: obj[key],
|
|
4904
4904
|
writable: false,
|
|
4905
|
-
configurable:
|
|
4906
|
-
// Must be configurable for Proxy get trap to work properly
|
|
4905
|
+
configurable: false,
|
|
4907
4906
|
enumerable: false
|
|
4908
4907
|
});
|
|
4909
4908
|
}
|
|
@@ -4934,41 +4933,41 @@ var DefaultHasManyCollection = class {
|
|
|
4934
4933
|
hideInternal(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "localKey"]);
|
|
4935
4934
|
this.hydrateFromCache();
|
|
4936
4935
|
}
|
|
4937
|
-
|
|
4938
|
-
|
|
4939
|
-
|
|
4940
|
-
|
|
4936
|
+
loaded = false;
|
|
4937
|
+
items = [];
|
|
4938
|
+
added = /* @__PURE__ */ new Set();
|
|
4939
|
+
removed = /* @__PURE__ */ new Set();
|
|
4941
4940
|
/**
|
|
4942
4941
|
* Loads the related entities if not already loaded.
|
|
4943
4942
|
* @returns Promise resolving to the array of child entities
|
|
4944
4943
|
*/
|
|
4945
4944
|
async load() {
|
|
4946
|
-
if (this
|
|
4945
|
+
if (this.loaded) return this.items;
|
|
4947
4946
|
const map = await this.loader();
|
|
4948
4947
|
const key = toKey3(this.root[this.localKey]);
|
|
4949
4948
|
const rows = map.get(key) ?? [];
|
|
4950
|
-
this
|
|
4951
|
-
this
|
|
4952
|
-
return this
|
|
4949
|
+
this.items = rows.map((row) => this.createEntity(row));
|
|
4950
|
+
this.loaded = true;
|
|
4951
|
+
return this.items;
|
|
4953
4952
|
}
|
|
4954
4953
|
/**
|
|
4955
4954
|
* Gets the current items in the collection.
|
|
4956
4955
|
* @returns Array of child entities
|
|
4957
4956
|
*/
|
|
4958
4957
|
getItems() {
|
|
4959
|
-
return this
|
|
4958
|
+
return this.items;
|
|
4960
4959
|
}
|
|
4961
4960
|
/**
|
|
4962
4961
|
* Array-compatible length for testing frameworks.
|
|
4963
4962
|
*/
|
|
4964
4963
|
get length() {
|
|
4965
|
-
return this
|
|
4964
|
+
return this.items.length;
|
|
4966
4965
|
}
|
|
4967
4966
|
/**
|
|
4968
4967
|
* Enables iteration over the collection like an array.
|
|
4969
4968
|
*/
|
|
4970
4969
|
[Symbol.iterator]() {
|
|
4971
|
-
return this
|
|
4970
|
+
return this.items[Symbol.iterator]();
|
|
4972
4971
|
}
|
|
4973
4972
|
/**
|
|
4974
4973
|
* Adds a new child entity to the collection.
|
|
@@ -4982,8 +4981,8 @@ var DefaultHasManyCollection = class {
|
|
|
4982
4981
|
[this.relation.foreignKey]: keyValue
|
|
4983
4982
|
};
|
|
4984
4983
|
const entity = this.createEntity(childRow);
|
|
4985
|
-
this
|
|
4986
|
-
this
|
|
4984
|
+
this.added.add(entity);
|
|
4985
|
+
this.items.push(entity);
|
|
4987
4986
|
this.ctx.registerRelationChange(
|
|
4988
4987
|
this.root,
|
|
4989
4988
|
this.relationKey,
|
|
@@ -5002,7 +5001,7 @@ var DefaultHasManyCollection = class {
|
|
|
5002
5001
|
const keyValue = this.root[this.localKey];
|
|
5003
5002
|
entity[this.relation.foreignKey] = keyValue;
|
|
5004
5003
|
this.ctx.markDirty(entity);
|
|
5005
|
-
this
|
|
5004
|
+
this.items.push(entity);
|
|
5006
5005
|
this.ctx.registerRelationChange(
|
|
5007
5006
|
this.root,
|
|
5008
5007
|
this.relationKey,
|
|
@@ -5017,8 +5016,8 @@ var DefaultHasManyCollection = class {
|
|
|
5017
5016
|
* @param entity - The entity to remove
|
|
5018
5017
|
*/
|
|
5019
5018
|
remove(entity) {
|
|
5020
|
-
this
|
|
5021
|
-
this
|
|
5019
|
+
this.items = this.items.filter((item) => item !== entity);
|
|
5020
|
+
this.removed.add(entity);
|
|
5022
5021
|
this.ctx.registerRelationChange(
|
|
5023
5022
|
this.root,
|
|
5024
5023
|
this.relationKey,
|
|
@@ -5032,13 +5031,10 @@ var DefaultHasManyCollection = class {
|
|
|
5032
5031
|
* Clears all entities from the collection.
|
|
5033
5032
|
*/
|
|
5034
5033
|
clear() {
|
|
5035
|
-
for (const entity of [...this
|
|
5034
|
+
for (const entity of [...this.items]) {
|
|
5036
5035
|
this.remove(entity);
|
|
5037
5036
|
}
|
|
5038
5037
|
}
|
|
5039
|
-
isLoaded() {
|
|
5040
|
-
return this.#loaded;
|
|
5041
|
-
}
|
|
5042
5038
|
get relationKey() {
|
|
5043
5039
|
return `${this.rootTable.name}.${this.relationName}`;
|
|
5044
5040
|
}
|
|
@@ -5047,15 +5043,15 @@ var DefaultHasManyCollection = class {
|
|
|
5047
5043
|
if (keyValue === void 0 || keyValue === null) return;
|
|
5048
5044
|
const rows = getHydrationRows(this.meta, this.relationName, keyValue);
|
|
5049
5045
|
if (!rows?.length) return;
|
|
5050
|
-
this
|
|
5051
|
-
this
|
|
5046
|
+
this.items = rows.map((row) => this.createEntity(row));
|
|
5047
|
+
this.loaded = true;
|
|
5052
5048
|
}
|
|
5053
5049
|
/**
|
|
5054
5050
|
* Returns the items for JSON serialization.
|
|
5055
5051
|
* @returns Array of child entities
|
|
5056
5052
|
*/
|
|
5057
5053
|
toJSON() {
|
|
5058
|
-
return this
|
|
5054
|
+
return this.items;
|
|
5059
5055
|
}
|
|
5060
5056
|
};
|
|
5061
5057
|
|
|
@@ -5066,8 +5062,7 @@ var hideInternal2 = (obj, keys) => {
|
|
|
5066
5062
|
Object.defineProperty(obj, key, {
|
|
5067
5063
|
value: obj[key],
|
|
5068
5064
|
writable: false,
|
|
5069
|
-
configurable:
|
|
5070
|
-
// Must be configurable for Proxy get trap to work properly
|
|
5065
|
+
configurable: false,
|
|
5071
5066
|
enumerable: false
|
|
5072
5067
|
});
|
|
5073
5068
|
}
|
|
@@ -5107,42 +5102,42 @@ var DefaultHasOneReference = class {
|
|
|
5107
5102
|
]);
|
|
5108
5103
|
this.populateFromHydrationCache();
|
|
5109
5104
|
}
|
|
5110
|
-
|
|
5111
|
-
|
|
5105
|
+
loaded = false;
|
|
5106
|
+
current = null;
|
|
5112
5107
|
async load() {
|
|
5113
|
-
if (this
|
|
5108
|
+
if (this.loaded) return this.current;
|
|
5114
5109
|
const map = await this.loader();
|
|
5115
5110
|
const keyValue = this.root[this.localKey];
|
|
5116
5111
|
if (keyValue === void 0 || keyValue === null) {
|
|
5117
|
-
this
|
|
5118
|
-
return this
|
|
5112
|
+
this.loaded = true;
|
|
5113
|
+
return this.current;
|
|
5119
5114
|
}
|
|
5120
5115
|
const row = map.get(toKey4(keyValue));
|
|
5121
|
-
this
|
|
5122
|
-
this
|
|
5123
|
-
return this
|
|
5116
|
+
this.current = row ? this.createEntity(row) : null;
|
|
5117
|
+
this.loaded = true;
|
|
5118
|
+
return this.current;
|
|
5124
5119
|
}
|
|
5125
5120
|
get() {
|
|
5126
|
-
return this
|
|
5121
|
+
return this.current;
|
|
5127
5122
|
}
|
|
5128
5123
|
set(data) {
|
|
5129
5124
|
if (data === null) {
|
|
5130
5125
|
return this.detachCurrent();
|
|
5131
5126
|
}
|
|
5132
5127
|
const entity = hasEntityMeta(data) ? data : this.createEntity(data);
|
|
5133
|
-
if (this
|
|
5128
|
+
if (this.current && this.current !== entity) {
|
|
5134
5129
|
this.ctx.registerRelationChange(
|
|
5135
5130
|
this.root,
|
|
5136
5131
|
this.relationKey,
|
|
5137
5132
|
this.rootTable,
|
|
5138
5133
|
this.relationName,
|
|
5139
5134
|
this.relation,
|
|
5140
|
-
{ kind: "remove", entity: this
|
|
5135
|
+
{ kind: "remove", entity: this.current }
|
|
5141
5136
|
);
|
|
5142
5137
|
}
|
|
5143
5138
|
this.assignForeignKey(entity);
|
|
5144
|
-
this
|
|
5145
|
-
this
|
|
5139
|
+
this.current = entity;
|
|
5140
|
+
this.loaded = true;
|
|
5146
5141
|
this.ctx.registerRelationChange(
|
|
5147
5142
|
this.root,
|
|
5148
5143
|
this.relationKey,
|
|
@@ -5153,17 +5148,14 @@ var DefaultHasOneReference = class {
|
|
|
5153
5148
|
);
|
|
5154
5149
|
return entity;
|
|
5155
5150
|
}
|
|
5156
|
-
isLoaded() {
|
|
5157
|
-
return this.#loaded;
|
|
5158
|
-
}
|
|
5159
5151
|
toJSON() {
|
|
5160
|
-
return this
|
|
5152
|
+
return this.current;
|
|
5161
5153
|
}
|
|
5162
5154
|
detachCurrent() {
|
|
5163
|
-
const previous = this
|
|
5155
|
+
const previous = this.current;
|
|
5164
5156
|
if (!previous) return null;
|
|
5165
|
-
this
|
|
5166
|
-
this
|
|
5157
|
+
this.current = null;
|
|
5158
|
+
this.loaded = true;
|
|
5167
5159
|
this.ctx.registerRelationChange(
|
|
5168
5160
|
this.root,
|
|
5169
5161
|
this.relationKey,
|
|
@@ -5186,8 +5178,8 @@ var DefaultHasOneReference = class {
|
|
|
5186
5178
|
if (keyValue === void 0 || keyValue === null) return;
|
|
5187
5179
|
const row = getHydrationRecord(this.meta, this.relationName, keyValue);
|
|
5188
5180
|
if (!row) return;
|
|
5189
|
-
this
|
|
5190
|
-
this
|
|
5181
|
+
this.current = this.createEntity(row);
|
|
5182
|
+
this.loaded = true;
|
|
5191
5183
|
}
|
|
5192
5184
|
};
|
|
5193
5185
|
|
|
@@ -5198,8 +5190,7 @@ var hideInternal3 = (obj, keys) => {
|
|
|
5198
5190
|
Object.defineProperty(obj, key, {
|
|
5199
5191
|
value: obj[key],
|
|
5200
5192
|
writable: false,
|
|
5201
|
-
configurable:
|
|
5202
|
-
// Must be configurable for Proxy get trap to work properly
|
|
5193
|
+
configurable: false,
|
|
5203
5194
|
enumerable: false
|
|
5204
5195
|
});
|
|
5205
5196
|
}
|
|
@@ -5229,29 +5220,29 @@ var DefaultBelongsToReference = class {
|
|
|
5229
5220
|
hideInternal3(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "targetKey"]);
|
|
5230
5221
|
this.populateFromHydrationCache();
|
|
5231
5222
|
}
|
|
5232
|
-
|
|
5233
|
-
|
|
5223
|
+
loaded = false;
|
|
5224
|
+
current = null;
|
|
5234
5225
|
async load() {
|
|
5235
|
-
if (this
|
|
5226
|
+
if (this.loaded) return this.current;
|
|
5236
5227
|
const map = await this.loader();
|
|
5237
5228
|
const fkValue = this.root[this.relation.foreignKey];
|
|
5238
5229
|
if (fkValue === null || fkValue === void 0) {
|
|
5239
|
-
this
|
|
5230
|
+
this.current = null;
|
|
5240
5231
|
} else {
|
|
5241
5232
|
const row = map.get(toKey5(fkValue));
|
|
5242
|
-
this
|
|
5233
|
+
this.current = row ? this.createEntity(row) : null;
|
|
5243
5234
|
}
|
|
5244
|
-
this
|
|
5245
|
-
return this
|
|
5235
|
+
this.loaded = true;
|
|
5236
|
+
return this.current;
|
|
5246
5237
|
}
|
|
5247
5238
|
get() {
|
|
5248
|
-
return this
|
|
5239
|
+
return this.current;
|
|
5249
5240
|
}
|
|
5250
5241
|
set(data) {
|
|
5251
5242
|
if (data === null) {
|
|
5252
|
-
const previous = this
|
|
5243
|
+
const previous = this.current;
|
|
5253
5244
|
this.root[this.relation.foreignKey] = null;
|
|
5254
|
-
this
|
|
5245
|
+
this.current = null;
|
|
5255
5246
|
this.ctx.registerRelationChange(
|
|
5256
5247
|
this.root,
|
|
5257
5248
|
this.relationKey,
|
|
@@ -5267,7 +5258,7 @@ var DefaultBelongsToReference = class {
|
|
|
5267
5258
|
if (pkValue !== void 0) {
|
|
5268
5259
|
this.root[this.relation.foreignKey] = pkValue;
|
|
5269
5260
|
}
|
|
5270
|
-
this
|
|
5261
|
+
this.current = entity;
|
|
5271
5262
|
this.ctx.registerRelationChange(
|
|
5272
5263
|
this.root,
|
|
5273
5264
|
this.relationKey,
|
|
@@ -5278,9 +5269,6 @@ var DefaultBelongsToReference = class {
|
|
|
5278
5269
|
);
|
|
5279
5270
|
return entity;
|
|
5280
5271
|
}
|
|
5281
|
-
isLoaded() {
|
|
5282
|
-
return this.#loaded;
|
|
5283
|
-
}
|
|
5284
5272
|
get relationKey() {
|
|
5285
5273
|
return `${this.rootTable.name}.${this.relationName}`;
|
|
5286
5274
|
}
|
|
@@ -5289,11 +5277,11 @@ var DefaultBelongsToReference = class {
|
|
|
5289
5277
|
if (fkValue === void 0 || fkValue === null) return;
|
|
5290
5278
|
const row = getHydrationRecord(this.meta, this.relationName, fkValue);
|
|
5291
5279
|
if (!row) return;
|
|
5292
|
-
this
|
|
5293
|
-
this
|
|
5280
|
+
this.current = this.createEntity(row);
|
|
5281
|
+
this.loaded = true;
|
|
5294
5282
|
}
|
|
5295
5283
|
toJSON() {
|
|
5296
|
-
return this
|
|
5284
|
+
return this.current;
|
|
5297
5285
|
}
|
|
5298
5286
|
};
|
|
5299
5287
|
|
|
@@ -5304,8 +5292,7 @@ var hideInternal4 = (obj, keys) => {
|
|
|
5304
5292
|
Object.defineProperty(obj, key, {
|
|
5305
5293
|
value: obj[key],
|
|
5306
5294
|
writable: false,
|
|
5307
|
-
configurable:
|
|
5308
|
-
// Must be configurable for Proxy get trap to work properly
|
|
5295
|
+
configurable: false,
|
|
5309
5296
|
enumerable: false
|
|
5310
5297
|
});
|
|
5311
5298
|
}
|
|
@@ -5335,45 +5322,45 @@ var DefaultManyToManyCollection = class {
|
|
|
5335
5322
|
hideInternal4(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "localKey"]);
|
|
5336
5323
|
this.hydrateFromCache();
|
|
5337
5324
|
}
|
|
5338
|
-
|
|
5339
|
-
|
|
5325
|
+
loaded = false;
|
|
5326
|
+
items = [];
|
|
5340
5327
|
/**
|
|
5341
5328
|
* Loads the collection items if not already loaded.
|
|
5342
5329
|
* @returns A promise that resolves to the array of target entities.
|
|
5343
5330
|
*/
|
|
5344
5331
|
async load() {
|
|
5345
|
-
if (this
|
|
5332
|
+
if (this.loaded) return this.items;
|
|
5346
5333
|
const map = await this.loader();
|
|
5347
5334
|
const key = toKey6(this.root[this.localKey]);
|
|
5348
5335
|
const rows = map.get(key) ?? [];
|
|
5349
|
-
this
|
|
5336
|
+
this.items = rows.map((row) => {
|
|
5350
5337
|
const entity = this.createEntity(row);
|
|
5351
5338
|
if (row._pivot) {
|
|
5352
5339
|
entity._pivot = row._pivot;
|
|
5353
5340
|
}
|
|
5354
5341
|
return entity;
|
|
5355
5342
|
});
|
|
5356
|
-
this
|
|
5357
|
-
return this
|
|
5343
|
+
this.loaded = true;
|
|
5344
|
+
return this.items;
|
|
5358
5345
|
}
|
|
5359
5346
|
/**
|
|
5360
5347
|
* Returns the currently loaded items.
|
|
5361
5348
|
* @returns Array of target entities.
|
|
5362
5349
|
*/
|
|
5363
5350
|
getItems() {
|
|
5364
|
-
return this
|
|
5351
|
+
return this.items;
|
|
5365
5352
|
}
|
|
5366
5353
|
/**
|
|
5367
5354
|
* Array-compatible length for testing frameworks.
|
|
5368
5355
|
*/
|
|
5369
5356
|
get length() {
|
|
5370
|
-
return this
|
|
5357
|
+
return this.items.length;
|
|
5371
5358
|
}
|
|
5372
5359
|
/**
|
|
5373
5360
|
* Enables iteration over the collection like an array.
|
|
5374
5361
|
*/
|
|
5375
5362
|
[Symbol.iterator]() {
|
|
5376
|
-
return this
|
|
5363
|
+
return this.items[Symbol.iterator]();
|
|
5377
5364
|
}
|
|
5378
5365
|
/**
|
|
5379
5366
|
* Attaches an entity to the collection.
|
|
@@ -5383,13 +5370,13 @@ var DefaultManyToManyCollection = class {
|
|
|
5383
5370
|
attach(target) {
|
|
5384
5371
|
const entity = this.ensureEntity(target);
|
|
5385
5372
|
const id = this.extractId(entity);
|
|
5386
|
-
if (id != null && this
|
|
5373
|
+
if (id != null && this.items.some((item) => this.extractId(item) === id)) {
|
|
5387
5374
|
return;
|
|
5388
5375
|
}
|
|
5389
|
-
if (id == null && this
|
|
5376
|
+
if (id == null && this.items.includes(entity)) {
|
|
5390
5377
|
return;
|
|
5391
5378
|
}
|
|
5392
|
-
this
|
|
5379
|
+
this.items.push(entity);
|
|
5393
5380
|
this.ctx.registerRelationChange(
|
|
5394
5381
|
this.root,
|
|
5395
5382
|
this.relationKey,
|
|
@@ -5407,9 +5394,9 @@ var DefaultManyToManyCollection = class {
|
|
|
5407
5394
|
detach(target) {
|
|
5408
5395
|
const id = typeof target === "number" || typeof target === "string" ? target : this.extractId(target);
|
|
5409
5396
|
if (id == null) return;
|
|
5410
|
-
const existing = this
|
|
5397
|
+
const existing = this.items.find((item) => this.extractId(item) === id);
|
|
5411
5398
|
if (!existing) return;
|
|
5412
|
-
this
|
|
5399
|
+
this.items = this.items.filter((item) => this.extractId(item) !== id);
|
|
5413
5400
|
this.ctx.registerRelationChange(
|
|
5414
5401
|
this.root,
|
|
5415
5402
|
this.relationKey,
|
|
@@ -5427,22 +5414,19 @@ var DefaultManyToManyCollection = class {
|
|
|
5427
5414
|
async syncByIds(ids) {
|
|
5428
5415
|
await this.load();
|
|
5429
5416
|
const normalized = new Set(ids.map((id) => toKey6(id)));
|
|
5430
|
-
const currentIds = new Set(this
|
|
5417
|
+
const currentIds = new Set(this.items.map((item) => toKey6(this.extractId(item))));
|
|
5431
5418
|
for (const id of normalized) {
|
|
5432
5419
|
if (!currentIds.has(id)) {
|
|
5433
5420
|
this.attach(id);
|
|
5434
5421
|
}
|
|
5435
5422
|
}
|
|
5436
|
-
for (const item of [...this
|
|
5423
|
+
for (const item of [...this.items]) {
|
|
5437
5424
|
const itemId = toKey6(this.extractId(item));
|
|
5438
5425
|
if (!normalized.has(itemId)) {
|
|
5439
5426
|
this.detach(item);
|
|
5440
5427
|
}
|
|
5441
5428
|
}
|
|
5442
5429
|
}
|
|
5443
|
-
isLoaded() {
|
|
5444
|
-
return this.#loaded;
|
|
5445
|
-
}
|
|
5446
5430
|
ensureEntity(target) {
|
|
5447
5431
|
if (typeof target === "number" || typeof target === "string") {
|
|
5448
5432
|
const stub = {
|
|
@@ -5470,17 +5454,17 @@ var DefaultManyToManyCollection = class {
|
|
|
5470
5454
|
if (keyValue === void 0 || keyValue === null) return;
|
|
5471
5455
|
const rows = getHydrationRows(this.meta, this.relationName, keyValue);
|
|
5472
5456
|
if (!rows?.length) return;
|
|
5473
|
-
this
|
|
5457
|
+
this.items = rows.map((row) => {
|
|
5474
5458
|
const entity = this.createEntity(row);
|
|
5475
5459
|
if (row._pivot) {
|
|
5476
5460
|
entity._pivot = row._pivot;
|
|
5477
5461
|
}
|
|
5478
5462
|
return entity;
|
|
5479
5463
|
});
|
|
5480
|
-
this
|
|
5464
|
+
this.loaded = true;
|
|
5481
5465
|
}
|
|
5482
5466
|
toJSON() {
|
|
5483
|
-
return this
|
|
5467
|
+
return this.items;
|
|
5484
5468
|
}
|
|
5485
5469
|
};
|
|
5486
5470
|
|
|
@@ -5822,31 +5806,12 @@ var relationLoaderCache = (meta, relationName, factory) => {
|
|
|
5822
5806
|
// src/orm/entity-relations.ts
|
|
5823
5807
|
var proxifyRelationWrapper = (wrapper) => {
|
|
5824
5808
|
return new Proxy(wrapper, {
|
|
5825
|
-
get(target, prop,
|
|
5826
|
-
if (prop === "toJSON") {
|
|
5827
|
-
return () => {
|
|
5828
|
-
const wrapperToJSON = target.toJSON;
|
|
5829
|
-
if (typeof wrapperToJSON === "function") {
|
|
5830
|
-
return wrapperToJSON.call(target);
|
|
5831
|
-
}
|
|
5832
|
-
const getRef2 = target.get;
|
|
5833
|
-
if (typeof getRef2 === "function") {
|
|
5834
|
-
return getRef2.call(target);
|
|
5835
|
-
}
|
|
5836
|
-
const getItems2 = target.getItems;
|
|
5837
|
-
if (typeof getItems2 === "function") {
|
|
5838
|
-
return getItems2.call(target);
|
|
5839
|
-
}
|
|
5840
|
-
return target;
|
|
5841
|
-
};
|
|
5842
|
-
}
|
|
5809
|
+
get(target, prop, receiver) {
|
|
5843
5810
|
if (typeof prop === "symbol") {
|
|
5844
|
-
|
|
5845
|
-
return typeof value === "function" ? value.bind(target) : value;
|
|
5811
|
+
return Reflect.get(target, prop, receiver);
|
|
5846
5812
|
}
|
|
5847
5813
|
if (prop in target) {
|
|
5848
|
-
|
|
5849
|
-
return typeof value === "function" ? value.bind(target) : value;
|
|
5814
|
+
return Reflect.get(target, prop, receiver);
|
|
5850
5815
|
}
|
|
5851
5816
|
const getItems = target.getItems;
|
|
5852
5817
|
if (typeof getItems === "function") {
|
|
@@ -5868,12 +5833,12 @@ var proxifyRelationWrapper = (wrapper) => {
|
|
|
5868
5833
|
}
|
|
5869
5834
|
return void 0;
|
|
5870
5835
|
},
|
|
5871
|
-
set(target, prop, value,
|
|
5836
|
+
set(target, prop, value, receiver) {
|
|
5872
5837
|
if (typeof prop === "symbol") {
|
|
5873
|
-
return Reflect.set(target, prop, value,
|
|
5838
|
+
return Reflect.set(target, prop, value, receiver);
|
|
5874
5839
|
}
|
|
5875
5840
|
if (prop in target) {
|
|
5876
|
-
return Reflect.set(target, prop, value,
|
|
5841
|
+
return Reflect.set(target, prop, value, receiver);
|
|
5877
5842
|
}
|
|
5878
5843
|
const getRef = target.get;
|
|
5879
5844
|
if (typeof getRef === "function") {
|
|
@@ -5887,7 +5852,7 @@ var proxifyRelationWrapper = (wrapper) => {
|
|
|
5887
5852
|
const items = getItems.call(target);
|
|
5888
5853
|
return Reflect.set(items, prop, value);
|
|
5889
5854
|
}
|
|
5890
|
-
return Reflect.set(target, prop, value,
|
|
5855
|
+
return Reflect.set(target, prop, value, receiver);
|
|
5891
5856
|
}
|
|
5892
5857
|
});
|
|
5893
5858
|
};
|
|
@@ -5989,15 +5954,7 @@ var instantiateWrapper = (meta, relationName, relation, owner, createEntity) =>
|
|
|
5989
5954
|
// src/orm/entity.ts
|
|
5990
5955
|
var isRelationWrapperLoaded = (value) => {
|
|
5991
5956
|
if (!value || typeof value !== "object") return false;
|
|
5992
|
-
|
|
5993
|
-
if (typeof wrapper.isLoaded === "function") return wrapper.isLoaded();
|
|
5994
|
-
return false;
|
|
5995
|
-
};
|
|
5996
|
-
var unwrapRelationForJson = (value) => {
|
|
5997
|
-
if (value && typeof value === "object" && typeof value.toJSON === "function") {
|
|
5998
|
-
return value.toJSON();
|
|
5999
|
-
}
|
|
6000
|
-
return value;
|
|
5957
|
+
return Boolean(value.loaded);
|
|
6001
5958
|
};
|
|
6002
5959
|
var createEntityProxy = (ctx, table, row, lazyRelations = [], lazyRelationOptions = /* @__PURE__ */ new Map()) => {
|
|
6003
5960
|
const target = { ...row };
|
|
@@ -6014,13 +5971,12 @@ var createEntityProxy = (ctx, table, row, lazyRelations = [], lazyRelationOption
|
|
|
6014
5971
|
const buildJson = (self) => {
|
|
6015
5972
|
const json = {};
|
|
6016
5973
|
for (const key of Object.keys(target)) {
|
|
6017
|
-
|
|
6018
|
-
json[key] = unwrapRelationForJson(value);
|
|
5974
|
+
json[key] = self[key];
|
|
6019
5975
|
}
|
|
6020
5976
|
for (const [relationName, wrapper] of meta.relationWrappers) {
|
|
6021
5977
|
if (Object.prototype.hasOwnProperty.call(json, relationName)) continue;
|
|
6022
5978
|
if (isRelationWrapperLoaded(wrapper)) {
|
|
6023
|
-
json[relationName] =
|
|
5979
|
+
json[relationName] = wrapper;
|
|
6024
5980
|
}
|
|
6025
5981
|
}
|
|
6026
5982
|
return json;
|