metal-orm 1.0.104 → 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 +81 -103
- 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 +81 -103
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/orm/entity-relations.ts +207 -209
- package/src/orm/entity.ts +151 -160
- 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
|
@@ -4933,41 +4933,41 @@ var DefaultHasManyCollection = class {
|
|
|
4933
4933
|
hideInternal(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "localKey"]);
|
|
4934
4934
|
this.hydrateFromCache();
|
|
4935
4935
|
}
|
|
4936
|
-
|
|
4937
|
-
|
|
4938
|
-
|
|
4939
|
-
|
|
4936
|
+
loaded = false;
|
|
4937
|
+
items = [];
|
|
4938
|
+
added = /* @__PURE__ */ new Set();
|
|
4939
|
+
removed = /* @__PURE__ */ new Set();
|
|
4940
4940
|
/**
|
|
4941
4941
|
* Loads the related entities if not already loaded.
|
|
4942
4942
|
* @returns Promise resolving to the array of child entities
|
|
4943
4943
|
*/
|
|
4944
4944
|
async load() {
|
|
4945
|
-
if (this
|
|
4945
|
+
if (this.loaded) return this.items;
|
|
4946
4946
|
const map = await this.loader();
|
|
4947
4947
|
const key = toKey3(this.root[this.localKey]);
|
|
4948
4948
|
const rows = map.get(key) ?? [];
|
|
4949
|
-
this
|
|
4950
|
-
this
|
|
4951
|
-
return this
|
|
4949
|
+
this.items = rows.map((row) => this.createEntity(row));
|
|
4950
|
+
this.loaded = true;
|
|
4951
|
+
return this.items;
|
|
4952
4952
|
}
|
|
4953
4953
|
/**
|
|
4954
4954
|
* Gets the current items in the collection.
|
|
4955
4955
|
* @returns Array of child entities
|
|
4956
4956
|
*/
|
|
4957
4957
|
getItems() {
|
|
4958
|
-
return this
|
|
4958
|
+
return this.items;
|
|
4959
4959
|
}
|
|
4960
4960
|
/**
|
|
4961
4961
|
* Array-compatible length for testing frameworks.
|
|
4962
4962
|
*/
|
|
4963
4963
|
get length() {
|
|
4964
|
-
return this
|
|
4964
|
+
return this.items.length;
|
|
4965
4965
|
}
|
|
4966
4966
|
/**
|
|
4967
4967
|
* Enables iteration over the collection like an array.
|
|
4968
4968
|
*/
|
|
4969
4969
|
[Symbol.iterator]() {
|
|
4970
|
-
return this
|
|
4970
|
+
return this.items[Symbol.iterator]();
|
|
4971
4971
|
}
|
|
4972
4972
|
/**
|
|
4973
4973
|
* Adds a new child entity to the collection.
|
|
@@ -4981,8 +4981,8 @@ var DefaultHasManyCollection = class {
|
|
|
4981
4981
|
[this.relation.foreignKey]: keyValue
|
|
4982
4982
|
};
|
|
4983
4983
|
const entity = this.createEntity(childRow);
|
|
4984
|
-
this
|
|
4985
|
-
this
|
|
4984
|
+
this.added.add(entity);
|
|
4985
|
+
this.items.push(entity);
|
|
4986
4986
|
this.ctx.registerRelationChange(
|
|
4987
4987
|
this.root,
|
|
4988
4988
|
this.relationKey,
|
|
@@ -5001,7 +5001,7 @@ var DefaultHasManyCollection = class {
|
|
|
5001
5001
|
const keyValue = this.root[this.localKey];
|
|
5002
5002
|
entity[this.relation.foreignKey] = keyValue;
|
|
5003
5003
|
this.ctx.markDirty(entity);
|
|
5004
|
-
this
|
|
5004
|
+
this.items.push(entity);
|
|
5005
5005
|
this.ctx.registerRelationChange(
|
|
5006
5006
|
this.root,
|
|
5007
5007
|
this.relationKey,
|
|
@@ -5016,8 +5016,8 @@ var DefaultHasManyCollection = class {
|
|
|
5016
5016
|
* @param entity - The entity to remove
|
|
5017
5017
|
*/
|
|
5018
5018
|
remove(entity) {
|
|
5019
|
-
this
|
|
5020
|
-
this
|
|
5019
|
+
this.items = this.items.filter((item) => item !== entity);
|
|
5020
|
+
this.removed.add(entity);
|
|
5021
5021
|
this.ctx.registerRelationChange(
|
|
5022
5022
|
this.root,
|
|
5023
5023
|
this.relationKey,
|
|
@@ -5031,13 +5031,10 @@ var DefaultHasManyCollection = class {
|
|
|
5031
5031
|
* Clears all entities from the collection.
|
|
5032
5032
|
*/
|
|
5033
5033
|
clear() {
|
|
5034
|
-
for (const entity of [...this
|
|
5034
|
+
for (const entity of [...this.items]) {
|
|
5035
5035
|
this.remove(entity);
|
|
5036
5036
|
}
|
|
5037
5037
|
}
|
|
5038
|
-
isLoaded() {
|
|
5039
|
-
return this.#loaded;
|
|
5040
|
-
}
|
|
5041
5038
|
get relationKey() {
|
|
5042
5039
|
return `${this.rootTable.name}.${this.relationName}`;
|
|
5043
5040
|
}
|
|
@@ -5046,15 +5043,15 @@ var DefaultHasManyCollection = class {
|
|
|
5046
5043
|
if (keyValue === void 0 || keyValue === null) return;
|
|
5047
5044
|
const rows = getHydrationRows(this.meta, this.relationName, keyValue);
|
|
5048
5045
|
if (!rows?.length) return;
|
|
5049
|
-
this
|
|
5050
|
-
this
|
|
5046
|
+
this.items = rows.map((row) => this.createEntity(row));
|
|
5047
|
+
this.loaded = true;
|
|
5051
5048
|
}
|
|
5052
5049
|
/**
|
|
5053
5050
|
* Returns the items for JSON serialization.
|
|
5054
5051
|
* @returns Array of child entities
|
|
5055
5052
|
*/
|
|
5056
5053
|
toJSON() {
|
|
5057
|
-
return this
|
|
5054
|
+
return this.items;
|
|
5058
5055
|
}
|
|
5059
5056
|
};
|
|
5060
5057
|
|
|
@@ -5105,42 +5102,42 @@ var DefaultHasOneReference = class {
|
|
|
5105
5102
|
]);
|
|
5106
5103
|
this.populateFromHydrationCache();
|
|
5107
5104
|
}
|
|
5108
|
-
|
|
5109
|
-
|
|
5105
|
+
loaded = false;
|
|
5106
|
+
current = null;
|
|
5110
5107
|
async load() {
|
|
5111
|
-
if (this
|
|
5108
|
+
if (this.loaded) return this.current;
|
|
5112
5109
|
const map = await this.loader();
|
|
5113
5110
|
const keyValue = this.root[this.localKey];
|
|
5114
5111
|
if (keyValue === void 0 || keyValue === null) {
|
|
5115
|
-
this
|
|
5116
|
-
return this
|
|
5112
|
+
this.loaded = true;
|
|
5113
|
+
return this.current;
|
|
5117
5114
|
}
|
|
5118
5115
|
const row = map.get(toKey4(keyValue));
|
|
5119
|
-
this
|
|
5120
|
-
this
|
|
5121
|
-
return this
|
|
5116
|
+
this.current = row ? this.createEntity(row) : null;
|
|
5117
|
+
this.loaded = true;
|
|
5118
|
+
return this.current;
|
|
5122
5119
|
}
|
|
5123
5120
|
get() {
|
|
5124
|
-
return this
|
|
5121
|
+
return this.current;
|
|
5125
5122
|
}
|
|
5126
5123
|
set(data) {
|
|
5127
5124
|
if (data === null) {
|
|
5128
5125
|
return this.detachCurrent();
|
|
5129
5126
|
}
|
|
5130
5127
|
const entity = hasEntityMeta(data) ? data : this.createEntity(data);
|
|
5131
|
-
if (this
|
|
5128
|
+
if (this.current && this.current !== entity) {
|
|
5132
5129
|
this.ctx.registerRelationChange(
|
|
5133
5130
|
this.root,
|
|
5134
5131
|
this.relationKey,
|
|
5135
5132
|
this.rootTable,
|
|
5136
5133
|
this.relationName,
|
|
5137
5134
|
this.relation,
|
|
5138
|
-
{ kind: "remove", entity: this
|
|
5135
|
+
{ kind: "remove", entity: this.current }
|
|
5139
5136
|
);
|
|
5140
5137
|
}
|
|
5141
5138
|
this.assignForeignKey(entity);
|
|
5142
|
-
this
|
|
5143
|
-
this
|
|
5139
|
+
this.current = entity;
|
|
5140
|
+
this.loaded = true;
|
|
5144
5141
|
this.ctx.registerRelationChange(
|
|
5145
5142
|
this.root,
|
|
5146
5143
|
this.relationKey,
|
|
@@ -5151,17 +5148,14 @@ var DefaultHasOneReference = class {
|
|
|
5151
5148
|
);
|
|
5152
5149
|
return entity;
|
|
5153
5150
|
}
|
|
5154
|
-
isLoaded() {
|
|
5155
|
-
return this.#loaded;
|
|
5156
|
-
}
|
|
5157
5151
|
toJSON() {
|
|
5158
|
-
return this
|
|
5152
|
+
return this.current;
|
|
5159
5153
|
}
|
|
5160
5154
|
detachCurrent() {
|
|
5161
|
-
const previous = this
|
|
5155
|
+
const previous = this.current;
|
|
5162
5156
|
if (!previous) return null;
|
|
5163
|
-
this
|
|
5164
|
-
this
|
|
5157
|
+
this.current = null;
|
|
5158
|
+
this.loaded = true;
|
|
5165
5159
|
this.ctx.registerRelationChange(
|
|
5166
5160
|
this.root,
|
|
5167
5161
|
this.relationKey,
|
|
@@ -5184,8 +5178,8 @@ var DefaultHasOneReference = class {
|
|
|
5184
5178
|
if (keyValue === void 0 || keyValue === null) return;
|
|
5185
5179
|
const row = getHydrationRecord(this.meta, this.relationName, keyValue);
|
|
5186
5180
|
if (!row) return;
|
|
5187
|
-
this
|
|
5188
|
-
this
|
|
5181
|
+
this.current = this.createEntity(row);
|
|
5182
|
+
this.loaded = true;
|
|
5189
5183
|
}
|
|
5190
5184
|
};
|
|
5191
5185
|
|
|
@@ -5226,29 +5220,29 @@ var DefaultBelongsToReference = class {
|
|
|
5226
5220
|
hideInternal3(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "targetKey"]);
|
|
5227
5221
|
this.populateFromHydrationCache();
|
|
5228
5222
|
}
|
|
5229
|
-
|
|
5230
|
-
|
|
5223
|
+
loaded = false;
|
|
5224
|
+
current = null;
|
|
5231
5225
|
async load() {
|
|
5232
|
-
if (this
|
|
5226
|
+
if (this.loaded) return this.current;
|
|
5233
5227
|
const map = await this.loader();
|
|
5234
5228
|
const fkValue = this.root[this.relation.foreignKey];
|
|
5235
5229
|
if (fkValue === null || fkValue === void 0) {
|
|
5236
|
-
this
|
|
5230
|
+
this.current = null;
|
|
5237
5231
|
} else {
|
|
5238
5232
|
const row = map.get(toKey5(fkValue));
|
|
5239
|
-
this
|
|
5233
|
+
this.current = row ? this.createEntity(row) : null;
|
|
5240
5234
|
}
|
|
5241
|
-
this
|
|
5242
|
-
return this
|
|
5235
|
+
this.loaded = true;
|
|
5236
|
+
return this.current;
|
|
5243
5237
|
}
|
|
5244
5238
|
get() {
|
|
5245
|
-
return this
|
|
5239
|
+
return this.current;
|
|
5246
5240
|
}
|
|
5247
5241
|
set(data) {
|
|
5248
5242
|
if (data === null) {
|
|
5249
|
-
const previous = this
|
|
5243
|
+
const previous = this.current;
|
|
5250
5244
|
this.root[this.relation.foreignKey] = null;
|
|
5251
|
-
this
|
|
5245
|
+
this.current = null;
|
|
5252
5246
|
this.ctx.registerRelationChange(
|
|
5253
5247
|
this.root,
|
|
5254
5248
|
this.relationKey,
|
|
@@ -5264,7 +5258,7 @@ var DefaultBelongsToReference = class {
|
|
|
5264
5258
|
if (pkValue !== void 0) {
|
|
5265
5259
|
this.root[this.relation.foreignKey] = pkValue;
|
|
5266
5260
|
}
|
|
5267
|
-
this
|
|
5261
|
+
this.current = entity;
|
|
5268
5262
|
this.ctx.registerRelationChange(
|
|
5269
5263
|
this.root,
|
|
5270
5264
|
this.relationKey,
|
|
@@ -5275,9 +5269,6 @@ var DefaultBelongsToReference = class {
|
|
|
5275
5269
|
);
|
|
5276
5270
|
return entity;
|
|
5277
5271
|
}
|
|
5278
|
-
isLoaded() {
|
|
5279
|
-
return this.#loaded;
|
|
5280
|
-
}
|
|
5281
5272
|
get relationKey() {
|
|
5282
5273
|
return `${this.rootTable.name}.${this.relationName}`;
|
|
5283
5274
|
}
|
|
@@ -5286,11 +5277,11 @@ var DefaultBelongsToReference = class {
|
|
|
5286
5277
|
if (fkValue === void 0 || fkValue === null) return;
|
|
5287
5278
|
const row = getHydrationRecord(this.meta, this.relationName, fkValue);
|
|
5288
5279
|
if (!row) return;
|
|
5289
|
-
this
|
|
5290
|
-
this
|
|
5280
|
+
this.current = this.createEntity(row);
|
|
5281
|
+
this.loaded = true;
|
|
5291
5282
|
}
|
|
5292
5283
|
toJSON() {
|
|
5293
|
-
return this
|
|
5284
|
+
return this.current;
|
|
5294
5285
|
}
|
|
5295
5286
|
};
|
|
5296
5287
|
|
|
@@ -5331,45 +5322,45 @@ var DefaultManyToManyCollection = class {
|
|
|
5331
5322
|
hideInternal4(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "localKey"]);
|
|
5332
5323
|
this.hydrateFromCache();
|
|
5333
5324
|
}
|
|
5334
|
-
|
|
5335
|
-
|
|
5325
|
+
loaded = false;
|
|
5326
|
+
items = [];
|
|
5336
5327
|
/**
|
|
5337
5328
|
* Loads the collection items if not already loaded.
|
|
5338
5329
|
* @returns A promise that resolves to the array of target entities.
|
|
5339
5330
|
*/
|
|
5340
5331
|
async load() {
|
|
5341
|
-
if (this
|
|
5332
|
+
if (this.loaded) return this.items;
|
|
5342
5333
|
const map = await this.loader();
|
|
5343
5334
|
const key = toKey6(this.root[this.localKey]);
|
|
5344
5335
|
const rows = map.get(key) ?? [];
|
|
5345
|
-
this
|
|
5336
|
+
this.items = rows.map((row) => {
|
|
5346
5337
|
const entity = this.createEntity(row);
|
|
5347
5338
|
if (row._pivot) {
|
|
5348
5339
|
entity._pivot = row._pivot;
|
|
5349
5340
|
}
|
|
5350
5341
|
return entity;
|
|
5351
5342
|
});
|
|
5352
|
-
this
|
|
5353
|
-
return this
|
|
5343
|
+
this.loaded = true;
|
|
5344
|
+
return this.items;
|
|
5354
5345
|
}
|
|
5355
5346
|
/**
|
|
5356
5347
|
* Returns the currently loaded items.
|
|
5357
5348
|
* @returns Array of target entities.
|
|
5358
5349
|
*/
|
|
5359
5350
|
getItems() {
|
|
5360
|
-
return this
|
|
5351
|
+
return this.items;
|
|
5361
5352
|
}
|
|
5362
5353
|
/**
|
|
5363
5354
|
* Array-compatible length for testing frameworks.
|
|
5364
5355
|
*/
|
|
5365
5356
|
get length() {
|
|
5366
|
-
return this
|
|
5357
|
+
return this.items.length;
|
|
5367
5358
|
}
|
|
5368
5359
|
/**
|
|
5369
5360
|
* Enables iteration over the collection like an array.
|
|
5370
5361
|
*/
|
|
5371
5362
|
[Symbol.iterator]() {
|
|
5372
|
-
return this
|
|
5363
|
+
return this.items[Symbol.iterator]();
|
|
5373
5364
|
}
|
|
5374
5365
|
/**
|
|
5375
5366
|
* Attaches an entity to the collection.
|
|
@@ -5379,13 +5370,13 @@ var DefaultManyToManyCollection = class {
|
|
|
5379
5370
|
attach(target) {
|
|
5380
5371
|
const entity = this.ensureEntity(target);
|
|
5381
5372
|
const id = this.extractId(entity);
|
|
5382
|
-
if (id != null && this
|
|
5373
|
+
if (id != null && this.items.some((item) => this.extractId(item) === id)) {
|
|
5383
5374
|
return;
|
|
5384
5375
|
}
|
|
5385
|
-
if (id == null && this
|
|
5376
|
+
if (id == null && this.items.includes(entity)) {
|
|
5386
5377
|
return;
|
|
5387
5378
|
}
|
|
5388
|
-
this
|
|
5379
|
+
this.items.push(entity);
|
|
5389
5380
|
this.ctx.registerRelationChange(
|
|
5390
5381
|
this.root,
|
|
5391
5382
|
this.relationKey,
|
|
@@ -5403,9 +5394,9 @@ var DefaultManyToManyCollection = class {
|
|
|
5403
5394
|
detach(target) {
|
|
5404
5395
|
const id = typeof target === "number" || typeof target === "string" ? target : this.extractId(target);
|
|
5405
5396
|
if (id == null) return;
|
|
5406
|
-
const existing = this
|
|
5397
|
+
const existing = this.items.find((item) => this.extractId(item) === id);
|
|
5407
5398
|
if (!existing) return;
|
|
5408
|
-
this
|
|
5399
|
+
this.items = this.items.filter((item) => this.extractId(item) !== id);
|
|
5409
5400
|
this.ctx.registerRelationChange(
|
|
5410
5401
|
this.root,
|
|
5411
5402
|
this.relationKey,
|
|
@@ -5423,22 +5414,19 @@ var DefaultManyToManyCollection = class {
|
|
|
5423
5414
|
async syncByIds(ids) {
|
|
5424
5415
|
await this.load();
|
|
5425
5416
|
const normalized = new Set(ids.map((id) => toKey6(id)));
|
|
5426
|
-
const currentIds = new Set(this
|
|
5417
|
+
const currentIds = new Set(this.items.map((item) => toKey6(this.extractId(item))));
|
|
5427
5418
|
for (const id of normalized) {
|
|
5428
5419
|
if (!currentIds.has(id)) {
|
|
5429
5420
|
this.attach(id);
|
|
5430
5421
|
}
|
|
5431
5422
|
}
|
|
5432
|
-
for (const item of [...this
|
|
5423
|
+
for (const item of [...this.items]) {
|
|
5433
5424
|
const itemId = toKey6(this.extractId(item));
|
|
5434
5425
|
if (!normalized.has(itemId)) {
|
|
5435
5426
|
this.detach(item);
|
|
5436
5427
|
}
|
|
5437
5428
|
}
|
|
5438
5429
|
}
|
|
5439
|
-
isLoaded() {
|
|
5440
|
-
return this.#loaded;
|
|
5441
|
-
}
|
|
5442
5430
|
ensureEntity(target) {
|
|
5443
5431
|
if (typeof target === "number" || typeof target === "string") {
|
|
5444
5432
|
const stub = {
|
|
@@ -5466,17 +5454,17 @@ var DefaultManyToManyCollection = class {
|
|
|
5466
5454
|
if (keyValue === void 0 || keyValue === null) return;
|
|
5467
5455
|
const rows = getHydrationRows(this.meta, this.relationName, keyValue);
|
|
5468
5456
|
if (!rows?.length) return;
|
|
5469
|
-
this
|
|
5457
|
+
this.items = rows.map((row) => {
|
|
5470
5458
|
const entity = this.createEntity(row);
|
|
5471
5459
|
if (row._pivot) {
|
|
5472
5460
|
entity._pivot = row._pivot;
|
|
5473
5461
|
}
|
|
5474
5462
|
return entity;
|
|
5475
5463
|
});
|
|
5476
|
-
this
|
|
5464
|
+
this.loaded = true;
|
|
5477
5465
|
}
|
|
5478
5466
|
toJSON() {
|
|
5479
|
-
return this
|
|
5467
|
+
return this.items;
|
|
5480
5468
|
}
|
|
5481
5469
|
};
|
|
5482
5470
|
|
|
@@ -5818,14 +5806,12 @@ var relationLoaderCache = (meta, relationName, factory) => {
|
|
|
5818
5806
|
// src/orm/entity-relations.ts
|
|
5819
5807
|
var proxifyRelationWrapper = (wrapper) => {
|
|
5820
5808
|
return new Proxy(wrapper, {
|
|
5821
|
-
get(target, prop,
|
|
5809
|
+
get(target, prop, receiver) {
|
|
5822
5810
|
if (typeof prop === "symbol") {
|
|
5823
|
-
|
|
5824
|
-
return typeof value === "function" ? value.bind(target) : value;
|
|
5811
|
+
return Reflect.get(target, prop, receiver);
|
|
5825
5812
|
}
|
|
5826
5813
|
if (prop in target) {
|
|
5827
|
-
|
|
5828
|
-
return typeof value === "function" ? value.bind(target) : value;
|
|
5814
|
+
return Reflect.get(target, prop, receiver);
|
|
5829
5815
|
}
|
|
5830
5816
|
const getItems = target.getItems;
|
|
5831
5817
|
if (typeof getItems === "function") {
|
|
@@ -5847,12 +5833,12 @@ var proxifyRelationWrapper = (wrapper) => {
|
|
|
5847
5833
|
}
|
|
5848
5834
|
return void 0;
|
|
5849
5835
|
},
|
|
5850
|
-
set(target, prop, value,
|
|
5836
|
+
set(target, prop, value, receiver) {
|
|
5851
5837
|
if (typeof prop === "symbol") {
|
|
5852
|
-
return Reflect.set(target, prop, value,
|
|
5838
|
+
return Reflect.set(target, prop, value, receiver);
|
|
5853
5839
|
}
|
|
5854
5840
|
if (prop in target) {
|
|
5855
|
-
return Reflect.set(target, prop, value,
|
|
5841
|
+
return Reflect.set(target, prop, value, receiver);
|
|
5856
5842
|
}
|
|
5857
5843
|
const getRef = target.get;
|
|
5858
5844
|
if (typeof getRef === "function") {
|
|
@@ -5866,7 +5852,7 @@ var proxifyRelationWrapper = (wrapper) => {
|
|
|
5866
5852
|
const items = getItems.call(target);
|
|
5867
5853
|
return Reflect.set(items, prop, value);
|
|
5868
5854
|
}
|
|
5869
|
-
return Reflect.set(target, prop, value,
|
|
5855
|
+
return Reflect.set(target, prop, value, receiver);
|
|
5870
5856
|
}
|
|
5871
5857
|
});
|
|
5872
5858
|
};
|
|
@@ -5968,15 +5954,7 @@ var instantiateWrapper = (meta, relationName, relation, owner, createEntity) =>
|
|
|
5968
5954
|
// src/orm/entity.ts
|
|
5969
5955
|
var isRelationWrapperLoaded = (value) => {
|
|
5970
5956
|
if (!value || typeof value !== "object") return false;
|
|
5971
|
-
|
|
5972
|
-
if (typeof wrapper.isLoaded === "function") return wrapper.isLoaded();
|
|
5973
|
-
return false;
|
|
5974
|
-
};
|
|
5975
|
-
var unwrapRelationForJson = (value) => {
|
|
5976
|
-
if (value && typeof value === "object" && typeof value.toJSON === "function") {
|
|
5977
|
-
return value.toJSON();
|
|
5978
|
-
}
|
|
5979
|
-
return value;
|
|
5957
|
+
return Boolean(value.loaded);
|
|
5980
5958
|
};
|
|
5981
5959
|
var createEntityProxy = (ctx, table, row, lazyRelations = [], lazyRelationOptions = /* @__PURE__ */ new Map()) => {
|
|
5982
5960
|
const target = { ...row };
|
|
@@ -5998,7 +5976,7 @@ var createEntityProxy = (ctx, table, row, lazyRelations = [], lazyRelationOption
|
|
|
5998
5976
|
for (const [relationName, wrapper] of meta.relationWrappers) {
|
|
5999
5977
|
if (Object.prototype.hasOwnProperty.call(json, relationName)) continue;
|
|
6000
5978
|
if (isRelationWrapperLoaded(wrapper)) {
|
|
6001
|
-
json[relationName] =
|
|
5979
|
+
json[relationName] = wrapper;
|
|
6002
5980
|
}
|
|
6003
5981
|
}
|
|
6004
5982
|
return json;
|