metal-orm 1.0.105 → 1.0.107
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 +136 -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 +136 -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 +10 -17
- package/src/orm/relations/belongs-to.ts +28 -19
- package/src/orm/relations/has-many.ts +35 -26
- package/src/orm/relations/has-one.ts +33 -24
- package/src/orm/relations/many-to-many.ts +33 -24
package/dist/index.js
CHANGED
|
@@ -4902,8 +4902,18 @@ var hideInternal = (obj, keys) => {
|
|
|
4902
4902
|
Object.defineProperty(obj, key, {
|
|
4903
4903
|
value: obj[key],
|
|
4904
4904
|
writable: false,
|
|
4905
|
+
configurable: false,
|
|
4906
|
+
enumerable: false
|
|
4907
|
+
});
|
|
4908
|
+
}
|
|
4909
|
+
};
|
|
4910
|
+
var hideWritable = (obj, keys) => {
|
|
4911
|
+
for (const key of keys) {
|
|
4912
|
+
const value = obj[key];
|
|
4913
|
+
Object.defineProperty(obj, key, {
|
|
4914
|
+
value,
|
|
4915
|
+
writable: true,
|
|
4905
4916
|
configurable: true,
|
|
4906
|
-
// Must be configurable for Proxy get trap to work properly
|
|
4907
4917
|
enumerable: false
|
|
4908
4918
|
});
|
|
4909
4919
|
}
|
|
@@ -4932,43 +4942,44 @@ var DefaultHasManyCollection = class {
|
|
|
4932
4942
|
this.createEntity = createEntity;
|
|
4933
4943
|
this.localKey = localKey;
|
|
4934
4944
|
hideInternal(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "localKey"]);
|
|
4945
|
+
hideWritable(this, ["loaded", "items", "added", "removed"]);
|
|
4935
4946
|
this.hydrateFromCache();
|
|
4936
4947
|
}
|
|
4937
|
-
|
|
4938
|
-
|
|
4939
|
-
|
|
4940
|
-
|
|
4948
|
+
loaded = false;
|
|
4949
|
+
items = [];
|
|
4950
|
+
added = /* @__PURE__ */ new Set();
|
|
4951
|
+
removed = /* @__PURE__ */ new Set();
|
|
4941
4952
|
/**
|
|
4942
4953
|
* Loads the related entities if not already loaded.
|
|
4943
4954
|
* @returns Promise resolving to the array of child entities
|
|
4944
4955
|
*/
|
|
4945
4956
|
async load() {
|
|
4946
|
-
if (this
|
|
4957
|
+
if (this.loaded) return this.items;
|
|
4947
4958
|
const map = await this.loader();
|
|
4948
4959
|
const key = toKey3(this.root[this.localKey]);
|
|
4949
4960
|
const rows = map.get(key) ?? [];
|
|
4950
|
-
this
|
|
4951
|
-
this
|
|
4952
|
-
return this
|
|
4961
|
+
this.items = rows.map((row) => this.createEntity(row));
|
|
4962
|
+
this.loaded = true;
|
|
4963
|
+
return this.items;
|
|
4953
4964
|
}
|
|
4954
4965
|
/**
|
|
4955
4966
|
* Gets the current items in the collection.
|
|
4956
4967
|
* @returns Array of child entities
|
|
4957
4968
|
*/
|
|
4958
4969
|
getItems() {
|
|
4959
|
-
return this
|
|
4970
|
+
return this.items;
|
|
4960
4971
|
}
|
|
4961
4972
|
/**
|
|
4962
4973
|
* Array-compatible length for testing frameworks.
|
|
4963
4974
|
*/
|
|
4964
4975
|
get length() {
|
|
4965
|
-
return this
|
|
4976
|
+
return this.items.length;
|
|
4966
4977
|
}
|
|
4967
4978
|
/**
|
|
4968
4979
|
* Enables iteration over the collection like an array.
|
|
4969
4980
|
*/
|
|
4970
4981
|
[Symbol.iterator]() {
|
|
4971
|
-
return this
|
|
4982
|
+
return this.items[Symbol.iterator]();
|
|
4972
4983
|
}
|
|
4973
4984
|
/**
|
|
4974
4985
|
* Adds a new child entity to the collection.
|
|
@@ -4982,8 +4993,8 @@ var DefaultHasManyCollection = class {
|
|
|
4982
4993
|
[this.relation.foreignKey]: keyValue
|
|
4983
4994
|
};
|
|
4984
4995
|
const entity = this.createEntity(childRow);
|
|
4985
|
-
this
|
|
4986
|
-
this
|
|
4996
|
+
this.added.add(entity);
|
|
4997
|
+
this.items.push(entity);
|
|
4987
4998
|
this.ctx.registerRelationChange(
|
|
4988
4999
|
this.root,
|
|
4989
5000
|
this.relationKey,
|
|
@@ -5002,7 +5013,7 @@ var DefaultHasManyCollection = class {
|
|
|
5002
5013
|
const keyValue = this.root[this.localKey];
|
|
5003
5014
|
entity[this.relation.foreignKey] = keyValue;
|
|
5004
5015
|
this.ctx.markDirty(entity);
|
|
5005
|
-
this
|
|
5016
|
+
this.items.push(entity);
|
|
5006
5017
|
this.ctx.registerRelationChange(
|
|
5007
5018
|
this.root,
|
|
5008
5019
|
this.relationKey,
|
|
@@ -5017,8 +5028,8 @@ var DefaultHasManyCollection = class {
|
|
|
5017
5028
|
* @param entity - The entity to remove
|
|
5018
5029
|
*/
|
|
5019
5030
|
remove(entity) {
|
|
5020
|
-
this
|
|
5021
|
-
this
|
|
5031
|
+
this.items = this.items.filter((item) => item !== entity);
|
|
5032
|
+
this.removed.add(entity);
|
|
5022
5033
|
this.ctx.registerRelationChange(
|
|
5023
5034
|
this.root,
|
|
5024
5035
|
this.relationKey,
|
|
@@ -5032,13 +5043,10 @@ var DefaultHasManyCollection = class {
|
|
|
5032
5043
|
* Clears all entities from the collection.
|
|
5033
5044
|
*/
|
|
5034
5045
|
clear() {
|
|
5035
|
-
for (const entity of [...this
|
|
5046
|
+
for (const entity of [...this.items]) {
|
|
5036
5047
|
this.remove(entity);
|
|
5037
5048
|
}
|
|
5038
5049
|
}
|
|
5039
|
-
isLoaded() {
|
|
5040
|
-
return this.#loaded;
|
|
5041
|
-
}
|
|
5042
5050
|
get relationKey() {
|
|
5043
5051
|
return `${this.rootTable.name}.${this.relationName}`;
|
|
5044
5052
|
}
|
|
@@ -5047,15 +5055,15 @@ var DefaultHasManyCollection = class {
|
|
|
5047
5055
|
if (keyValue === void 0 || keyValue === null) return;
|
|
5048
5056
|
const rows = getHydrationRows(this.meta, this.relationName, keyValue);
|
|
5049
5057
|
if (!rows?.length) return;
|
|
5050
|
-
this
|
|
5051
|
-
this
|
|
5058
|
+
this.items = rows.map((row) => this.createEntity(row));
|
|
5059
|
+
this.loaded = true;
|
|
5052
5060
|
}
|
|
5053
5061
|
/**
|
|
5054
5062
|
* Returns the items for JSON serialization.
|
|
5055
5063
|
* @returns Array of child entities
|
|
5056
5064
|
*/
|
|
5057
5065
|
toJSON() {
|
|
5058
|
-
return this
|
|
5066
|
+
return this.items;
|
|
5059
5067
|
}
|
|
5060
5068
|
};
|
|
5061
5069
|
|
|
@@ -5066,8 +5074,18 @@ var hideInternal2 = (obj, keys) => {
|
|
|
5066
5074
|
Object.defineProperty(obj, key, {
|
|
5067
5075
|
value: obj[key],
|
|
5068
5076
|
writable: false,
|
|
5077
|
+
configurable: false,
|
|
5078
|
+
enumerable: false
|
|
5079
|
+
});
|
|
5080
|
+
}
|
|
5081
|
+
};
|
|
5082
|
+
var hideWritable2 = (obj, keys) => {
|
|
5083
|
+
for (const key of keys) {
|
|
5084
|
+
const value = obj[key];
|
|
5085
|
+
Object.defineProperty(obj, key, {
|
|
5086
|
+
value,
|
|
5087
|
+
writable: true,
|
|
5069
5088
|
configurable: true,
|
|
5070
|
-
// Must be configurable for Proxy get trap to work properly
|
|
5071
5089
|
enumerable: false
|
|
5072
5090
|
});
|
|
5073
5091
|
}
|
|
@@ -5105,44 +5123,45 @@ var DefaultHasOneReference = class {
|
|
|
5105
5123
|
"createEntity",
|
|
5106
5124
|
"localKey"
|
|
5107
5125
|
]);
|
|
5126
|
+
hideWritable2(this, ["loaded", "current"]);
|
|
5108
5127
|
this.populateFromHydrationCache();
|
|
5109
5128
|
}
|
|
5110
|
-
|
|
5111
|
-
|
|
5129
|
+
loaded = false;
|
|
5130
|
+
current = null;
|
|
5112
5131
|
async load() {
|
|
5113
|
-
if (this
|
|
5132
|
+
if (this.loaded) return this.current;
|
|
5114
5133
|
const map = await this.loader();
|
|
5115
5134
|
const keyValue = this.root[this.localKey];
|
|
5116
5135
|
if (keyValue === void 0 || keyValue === null) {
|
|
5117
|
-
this
|
|
5118
|
-
return this
|
|
5136
|
+
this.loaded = true;
|
|
5137
|
+
return this.current;
|
|
5119
5138
|
}
|
|
5120
5139
|
const row = map.get(toKey4(keyValue));
|
|
5121
|
-
this
|
|
5122
|
-
this
|
|
5123
|
-
return this
|
|
5140
|
+
this.current = row ? this.createEntity(row) : null;
|
|
5141
|
+
this.loaded = true;
|
|
5142
|
+
return this.current;
|
|
5124
5143
|
}
|
|
5125
5144
|
get() {
|
|
5126
|
-
return this
|
|
5145
|
+
return this.current;
|
|
5127
5146
|
}
|
|
5128
5147
|
set(data) {
|
|
5129
5148
|
if (data === null) {
|
|
5130
5149
|
return this.detachCurrent();
|
|
5131
5150
|
}
|
|
5132
5151
|
const entity = hasEntityMeta(data) ? data : this.createEntity(data);
|
|
5133
|
-
if (this
|
|
5152
|
+
if (this.current && this.current !== entity) {
|
|
5134
5153
|
this.ctx.registerRelationChange(
|
|
5135
5154
|
this.root,
|
|
5136
5155
|
this.relationKey,
|
|
5137
5156
|
this.rootTable,
|
|
5138
5157
|
this.relationName,
|
|
5139
5158
|
this.relation,
|
|
5140
|
-
{ kind: "remove", entity: this
|
|
5159
|
+
{ kind: "remove", entity: this.current }
|
|
5141
5160
|
);
|
|
5142
5161
|
}
|
|
5143
5162
|
this.assignForeignKey(entity);
|
|
5144
|
-
this
|
|
5145
|
-
this
|
|
5163
|
+
this.current = entity;
|
|
5164
|
+
this.loaded = true;
|
|
5146
5165
|
this.ctx.registerRelationChange(
|
|
5147
5166
|
this.root,
|
|
5148
5167
|
this.relationKey,
|
|
@@ -5153,17 +5172,14 @@ var DefaultHasOneReference = class {
|
|
|
5153
5172
|
);
|
|
5154
5173
|
return entity;
|
|
5155
5174
|
}
|
|
5156
|
-
isLoaded() {
|
|
5157
|
-
return this.#loaded;
|
|
5158
|
-
}
|
|
5159
5175
|
toJSON() {
|
|
5160
|
-
return this
|
|
5176
|
+
return this.current;
|
|
5161
5177
|
}
|
|
5162
5178
|
detachCurrent() {
|
|
5163
|
-
const previous = this
|
|
5179
|
+
const previous = this.current;
|
|
5164
5180
|
if (!previous) return null;
|
|
5165
|
-
this
|
|
5166
|
-
this
|
|
5181
|
+
this.current = null;
|
|
5182
|
+
this.loaded = true;
|
|
5167
5183
|
this.ctx.registerRelationChange(
|
|
5168
5184
|
this.root,
|
|
5169
5185
|
this.relationKey,
|
|
@@ -5186,8 +5202,8 @@ var DefaultHasOneReference = class {
|
|
|
5186
5202
|
if (keyValue === void 0 || keyValue === null) return;
|
|
5187
5203
|
const row = getHydrationRecord(this.meta, this.relationName, keyValue);
|
|
5188
5204
|
if (!row) return;
|
|
5189
|
-
this
|
|
5190
|
-
this
|
|
5205
|
+
this.current = this.createEntity(row);
|
|
5206
|
+
this.loaded = true;
|
|
5191
5207
|
}
|
|
5192
5208
|
};
|
|
5193
5209
|
|
|
@@ -5198,8 +5214,18 @@ var hideInternal3 = (obj, keys) => {
|
|
|
5198
5214
|
Object.defineProperty(obj, key, {
|
|
5199
5215
|
value: obj[key],
|
|
5200
5216
|
writable: false,
|
|
5217
|
+
configurable: false,
|
|
5218
|
+
enumerable: false
|
|
5219
|
+
});
|
|
5220
|
+
}
|
|
5221
|
+
};
|
|
5222
|
+
var hideWritable3 = (obj, keys) => {
|
|
5223
|
+
for (const key of keys) {
|
|
5224
|
+
const value = obj[key];
|
|
5225
|
+
Object.defineProperty(obj, key, {
|
|
5226
|
+
value,
|
|
5227
|
+
writable: true,
|
|
5201
5228
|
configurable: true,
|
|
5202
|
-
// Must be configurable for Proxy get trap to work properly
|
|
5203
5229
|
enumerable: false
|
|
5204
5230
|
});
|
|
5205
5231
|
}
|
|
@@ -5227,31 +5253,32 @@ var DefaultBelongsToReference = class {
|
|
|
5227
5253
|
this.createEntity = createEntity;
|
|
5228
5254
|
this.targetKey = targetKey;
|
|
5229
5255
|
hideInternal3(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "targetKey"]);
|
|
5256
|
+
hideWritable3(this, ["loaded", "current"]);
|
|
5230
5257
|
this.populateFromHydrationCache();
|
|
5231
5258
|
}
|
|
5232
|
-
|
|
5233
|
-
|
|
5259
|
+
loaded = false;
|
|
5260
|
+
current = null;
|
|
5234
5261
|
async load() {
|
|
5235
|
-
if (this
|
|
5262
|
+
if (this.loaded) return this.current;
|
|
5236
5263
|
const map = await this.loader();
|
|
5237
5264
|
const fkValue = this.root[this.relation.foreignKey];
|
|
5238
5265
|
if (fkValue === null || fkValue === void 0) {
|
|
5239
|
-
this
|
|
5266
|
+
this.current = null;
|
|
5240
5267
|
} else {
|
|
5241
5268
|
const row = map.get(toKey5(fkValue));
|
|
5242
|
-
this
|
|
5269
|
+
this.current = row ? this.createEntity(row) : null;
|
|
5243
5270
|
}
|
|
5244
|
-
this
|
|
5245
|
-
return this
|
|
5271
|
+
this.loaded = true;
|
|
5272
|
+
return this.current;
|
|
5246
5273
|
}
|
|
5247
5274
|
get() {
|
|
5248
|
-
return this
|
|
5275
|
+
return this.current;
|
|
5249
5276
|
}
|
|
5250
5277
|
set(data) {
|
|
5251
5278
|
if (data === null) {
|
|
5252
|
-
const previous = this
|
|
5279
|
+
const previous = this.current;
|
|
5253
5280
|
this.root[this.relation.foreignKey] = null;
|
|
5254
|
-
this
|
|
5281
|
+
this.current = null;
|
|
5255
5282
|
this.ctx.registerRelationChange(
|
|
5256
5283
|
this.root,
|
|
5257
5284
|
this.relationKey,
|
|
@@ -5267,7 +5294,7 @@ var DefaultBelongsToReference = class {
|
|
|
5267
5294
|
if (pkValue !== void 0) {
|
|
5268
5295
|
this.root[this.relation.foreignKey] = pkValue;
|
|
5269
5296
|
}
|
|
5270
|
-
this
|
|
5297
|
+
this.current = entity;
|
|
5271
5298
|
this.ctx.registerRelationChange(
|
|
5272
5299
|
this.root,
|
|
5273
5300
|
this.relationKey,
|
|
@@ -5278,9 +5305,6 @@ var DefaultBelongsToReference = class {
|
|
|
5278
5305
|
);
|
|
5279
5306
|
return entity;
|
|
5280
5307
|
}
|
|
5281
|
-
isLoaded() {
|
|
5282
|
-
return this.#loaded;
|
|
5283
|
-
}
|
|
5284
5308
|
get relationKey() {
|
|
5285
5309
|
return `${this.rootTable.name}.${this.relationName}`;
|
|
5286
5310
|
}
|
|
@@ -5289,11 +5313,11 @@ var DefaultBelongsToReference = class {
|
|
|
5289
5313
|
if (fkValue === void 0 || fkValue === null) return;
|
|
5290
5314
|
const row = getHydrationRecord(this.meta, this.relationName, fkValue);
|
|
5291
5315
|
if (!row) return;
|
|
5292
|
-
this
|
|
5293
|
-
this
|
|
5316
|
+
this.current = this.createEntity(row);
|
|
5317
|
+
this.loaded = true;
|
|
5294
5318
|
}
|
|
5295
5319
|
toJSON() {
|
|
5296
|
-
return this
|
|
5320
|
+
return this.current;
|
|
5297
5321
|
}
|
|
5298
5322
|
};
|
|
5299
5323
|
|
|
@@ -5304,8 +5328,18 @@ var hideInternal4 = (obj, keys) => {
|
|
|
5304
5328
|
Object.defineProperty(obj, key, {
|
|
5305
5329
|
value: obj[key],
|
|
5306
5330
|
writable: false,
|
|
5331
|
+
configurable: false,
|
|
5332
|
+
enumerable: false
|
|
5333
|
+
});
|
|
5334
|
+
}
|
|
5335
|
+
};
|
|
5336
|
+
var hideWritable4 = (obj, keys) => {
|
|
5337
|
+
for (const key of keys) {
|
|
5338
|
+
const value = obj[key];
|
|
5339
|
+
Object.defineProperty(obj, key, {
|
|
5340
|
+
value,
|
|
5341
|
+
writable: true,
|
|
5307
5342
|
configurable: true,
|
|
5308
|
-
// Must be configurable for Proxy get trap to work properly
|
|
5309
5343
|
enumerable: false
|
|
5310
5344
|
});
|
|
5311
5345
|
}
|
|
@@ -5333,47 +5367,48 @@ var DefaultManyToManyCollection = class {
|
|
|
5333
5367
|
this.createEntity = createEntity;
|
|
5334
5368
|
this.localKey = localKey;
|
|
5335
5369
|
hideInternal4(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "localKey"]);
|
|
5370
|
+
hideWritable4(this, ["loaded", "items"]);
|
|
5336
5371
|
this.hydrateFromCache();
|
|
5337
5372
|
}
|
|
5338
|
-
|
|
5339
|
-
|
|
5373
|
+
loaded = false;
|
|
5374
|
+
items = [];
|
|
5340
5375
|
/**
|
|
5341
5376
|
* Loads the collection items if not already loaded.
|
|
5342
5377
|
* @returns A promise that resolves to the array of target entities.
|
|
5343
5378
|
*/
|
|
5344
5379
|
async load() {
|
|
5345
|
-
if (this
|
|
5380
|
+
if (this.loaded) return this.items;
|
|
5346
5381
|
const map = await this.loader();
|
|
5347
5382
|
const key = toKey6(this.root[this.localKey]);
|
|
5348
5383
|
const rows = map.get(key) ?? [];
|
|
5349
|
-
this
|
|
5384
|
+
this.items = rows.map((row) => {
|
|
5350
5385
|
const entity = this.createEntity(row);
|
|
5351
5386
|
if (row._pivot) {
|
|
5352
5387
|
entity._pivot = row._pivot;
|
|
5353
5388
|
}
|
|
5354
5389
|
return entity;
|
|
5355
5390
|
});
|
|
5356
|
-
this
|
|
5357
|
-
return this
|
|
5391
|
+
this.loaded = true;
|
|
5392
|
+
return this.items;
|
|
5358
5393
|
}
|
|
5359
5394
|
/**
|
|
5360
5395
|
* Returns the currently loaded items.
|
|
5361
5396
|
* @returns Array of target entities.
|
|
5362
5397
|
*/
|
|
5363
5398
|
getItems() {
|
|
5364
|
-
return this
|
|
5399
|
+
return this.items;
|
|
5365
5400
|
}
|
|
5366
5401
|
/**
|
|
5367
5402
|
* Array-compatible length for testing frameworks.
|
|
5368
5403
|
*/
|
|
5369
5404
|
get length() {
|
|
5370
|
-
return this
|
|
5405
|
+
return this.items.length;
|
|
5371
5406
|
}
|
|
5372
5407
|
/**
|
|
5373
5408
|
* Enables iteration over the collection like an array.
|
|
5374
5409
|
*/
|
|
5375
5410
|
[Symbol.iterator]() {
|
|
5376
|
-
return this
|
|
5411
|
+
return this.items[Symbol.iterator]();
|
|
5377
5412
|
}
|
|
5378
5413
|
/**
|
|
5379
5414
|
* Attaches an entity to the collection.
|
|
@@ -5383,13 +5418,13 @@ var DefaultManyToManyCollection = class {
|
|
|
5383
5418
|
attach(target) {
|
|
5384
5419
|
const entity = this.ensureEntity(target);
|
|
5385
5420
|
const id = this.extractId(entity);
|
|
5386
|
-
if (id != null && this
|
|
5421
|
+
if (id != null && this.items.some((item) => this.extractId(item) === id)) {
|
|
5387
5422
|
return;
|
|
5388
5423
|
}
|
|
5389
|
-
if (id == null && this
|
|
5424
|
+
if (id == null && this.items.includes(entity)) {
|
|
5390
5425
|
return;
|
|
5391
5426
|
}
|
|
5392
|
-
this
|
|
5427
|
+
this.items.push(entity);
|
|
5393
5428
|
this.ctx.registerRelationChange(
|
|
5394
5429
|
this.root,
|
|
5395
5430
|
this.relationKey,
|
|
@@ -5407,9 +5442,9 @@ var DefaultManyToManyCollection = class {
|
|
|
5407
5442
|
detach(target) {
|
|
5408
5443
|
const id = typeof target === "number" || typeof target === "string" ? target : this.extractId(target);
|
|
5409
5444
|
if (id == null) return;
|
|
5410
|
-
const existing = this
|
|
5445
|
+
const existing = this.items.find((item) => this.extractId(item) === id);
|
|
5411
5446
|
if (!existing) return;
|
|
5412
|
-
this
|
|
5447
|
+
this.items = this.items.filter((item) => this.extractId(item) !== id);
|
|
5413
5448
|
this.ctx.registerRelationChange(
|
|
5414
5449
|
this.root,
|
|
5415
5450
|
this.relationKey,
|
|
@@ -5427,22 +5462,19 @@ var DefaultManyToManyCollection = class {
|
|
|
5427
5462
|
async syncByIds(ids) {
|
|
5428
5463
|
await this.load();
|
|
5429
5464
|
const normalized = new Set(ids.map((id) => toKey6(id)));
|
|
5430
|
-
const currentIds = new Set(this
|
|
5465
|
+
const currentIds = new Set(this.items.map((item) => toKey6(this.extractId(item))));
|
|
5431
5466
|
for (const id of normalized) {
|
|
5432
5467
|
if (!currentIds.has(id)) {
|
|
5433
5468
|
this.attach(id);
|
|
5434
5469
|
}
|
|
5435
5470
|
}
|
|
5436
|
-
for (const item of [...this
|
|
5471
|
+
for (const item of [...this.items]) {
|
|
5437
5472
|
const itemId = toKey6(this.extractId(item));
|
|
5438
5473
|
if (!normalized.has(itemId)) {
|
|
5439
5474
|
this.detach(item);
|
|
5440
5475
|
}
|
|
5441
5476
|
}
|
|
5442
5477
|
}
|
|
5443
|
-
isLoaded() {
|
|
5444
|
-
return this.#loaded;
|
|
5445
|
-
}
|
|
5446
5478
|
ensureEntity(target) {
|
|
5447
5479
|
if (typeof target === "number" || typeof target === "string") {
|
|
5448
5480
|
const stub = {
|
|
@@ -5470,17 +5502,17 @@ var DefaultManyToManyCollection = class {
|
|
|
5470
5502
|
if (keyValue === void 0 || keyValue === null) return;
|
|
5471
5503
|
const rows = getHydrationRows(this.meta, this.relationName, keyValue);
|
|
5472
5504
|
if (!rows?.length) return;
|
|
5473
|
-
this
|
|
5505
|
+
this.items = rows.map((row) => {
|
|
5474
5506
|
const entity = this.createEntity(row);
|
|
5475
5507
|
if (row._pivot) {
|
|
5476
5508
|
entity._pivot = row._pivot;
|
|
5477
5509
|
}
|
|
5478
5510
|
return entity;
|
|
5479
5511
|
});
|
|
5480
|
-
this
|
|
5512
|
+
this.loaded = true;
|
|
5481
5513
|
}
|
|
5482
5514
|
toJSON() {
|
|
5483
|
-
return this
|
|
5515
|
+
return this.items;
|
|
5484
5516
|
}
|
|
5485
5517
|
};
|
|
5486
5518
|
|
|
@@ -5822,31 +5854,12 @@ var relationLoaderCache = (meta, relationName, factory) => {
|
|
|
5822
5854
|
// src/orm/entity-relations.ts
|
|
5823
5855
|
var proxifyRelationWrapper = (wrapper) => {
|
|
5824
5856
|
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
|
-
}
|
|
5857
|
+
get(target, prop, receiver) {
|
|
5843
5858
|
if (typeof prop === "symbol") {
|
|
5844
|
-
|
|
5845
|
-
return typeof value === "function" ? value.bind(target) : value;
|
|
5859
|
+
return Reflect.get(target, prop, receiver);
|
|
5846
5860
|
}
|
|
5847
5861
|
if (prop in target) {
|
|
5848
|
-
|
|
5849
|
-
return typeof value === "function" ? value.bind(target) : value;
|
|
5862
|
+
return Reflect.get(target, prop, receiver);
|
|
5850
5863
|
}
|
|
5851
5864
|
const getItems = target.getItems;
|
|
5852
5865
|
if (typeof getItems === "function") {
|
|
@@ -5868,12 +5881,12 @@ var proxifyRelationWrapper = (wrapper) => {
|
|
|
5868
5881
|
}
|
|
5869
5882
|
return void 0;
|
|
5870
5883
|
},
|
|
5871
|
-
set(target, prop, value,
|
|
5884
|
+
set(target, prop, value, receiver) {
|
|
5872
5885
|
if (typeof prop === "symbol") {
|
|
5873
|
-
return Reflect.set(target, prop, value,
|
|
5886
|
+
return Reflect.set(target, prop, value, receiver);
|
|
5874
5887
|
}
|
|
5875
5888
|
if (prop in target) {
|
|
5876
|
-
return Reflect.set(target, prop, value,
|
|
5889
|
+
return Reflect.set(target, prop, value, receiver);
|
|
5877
5890
|
}
|
|
5878
5891
|
const getRef = target.get;
|
|
5879
5892
|
if (typeof getRef === "function") {
|
|
@@ -5887,7 +5900,7 @@ var proxifyRelationWrapper = (wrapper) => {
|
|
|
5887
5900
|
const items = getItems.call(target);
|
|
5888
5901
|
return Reflect.set(items, prop, value);
|
|
5889
5902
|
}
|
|
5890
|
-
return Reflect.set(target, prop, value,
|
|
5903
|
+
return Reflect.set(target, prop, value, receiver);
|
|
5891
5904
|
}
|
|
5892
5905
|
});
|
|
5893
5906
|
};
|
|
@@ -5989,15 +6002,7 @@ var instantiateWrapper = (meta, relationName, relation, owner, createEntity) =>
|
|
|
5989
6002
|
// src/orm/entity.ts
|
|
5990
6003
|
var isRelationWrapperLoaded = (value) => {
|
|
5991
6004
|
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;
|
|
6005
|
+
return Boolean(value.loaded);
|
|
6001
6006
|
};
|
|
6002
6007
|
var createEntityProxy = (ctx, table, row, lazyRelations = [], lazyRelationOptions = /* @__PURE__ */ new Map()) => {
|
|
6003
6008
|
const target = { ...row };
|
|
@@ -6014,13 +6019,14 @@ var createEntityProxy = (ctx, table, row, lazyRelations = [], lazyRelationOption
|
|
|
6014
6019
|
const buildJson = (self) => {
|
|
6015
6020
|
const json = {};
|
|
6016
6021
|
for (const key of Object.keys(target)) {
|
|
6017
|
-
|
|
6018
|
-
json[key] =
|
|
6019
|
-
}
|
|
6020
|
-
for (const
|
|
6021
|
-
|
|
6022
|
-
if (isRelationWrapperLoaded(wrapper)) {
|
|
6023
|
-
|
|
6022
|
+
if (table.relations[key]) continue;
|
|
6023
|
+
json[key] = self[key];
|
|
6024
|
+
}
|
|
6025
|
+
for (const relationName of Object.keys(table.relations)) {
|
|
6026
|
+
const wrapper = self[relationName];
|
|
6027
|
+
if (wrapper && isRelationWrapperLoaded(wrapper)) {
|
|
6028
|
+
const wrapperWithToJSON = wrapper;
|
|
6029
|
+
json[relationName] = typeof wrapperWithToJSON.toJSON === "function" ? wrapperWithToJSON.toJSON() : wrapper;
|
|
6024
6030
|
}
|
|
6025
6031
|
}
|
|
6026
6032
|
return json;
|