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 CHANGED
@@ -5261,8 +5261,7 @@ var hideInternal = (obj, keys) => {
5261
5261
  Object.defineProperty(obj, key, {
5262
5262
  value: obj[key],
5263
5263
  writable: false,
5264
- configurable: true,
5265
- // Must be configurable for Proxy get trap to work properly
5264
+ configurable: false,
5266
5265
  enumerable: false
5267
5266
  });
5268
5267
  }
@@ -5293,41 +5292,41 @@ var DefaultHasManyCollection = class {
5293
5292
  hideInternal(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "localKey"]);
5294
5293
  this.hydrateFromCache();
5295
5294
  }
5296
- #loaded = false;
5297
- #items = [];
5298
- #added = /* @__PURE__ */ new Set();
5299
- #removed = /* @__PURE__ */ new Set();
5295
+ loaded = false;
5296
+ items = [];
5297
+ added = /* @__PURE__ */ new Set();
5298
+ removed = /* @__PURE__ */ new Set();
5300
5299
  /**
5301
5300
  * Loads the related entities if not already loaded.
5302
5301
  * @returns Promise resolving to the array of child entities
5303
5302
  */
5304
5303
  async load() {
5305
- if (this.#loaded) return this.#items;
5304
+ if (this.loaded) return this.items;
5306
5305
  const map = await this.loader();
5307
5306
  const key = toKey3(this.root[this.localKey]);
5308
5307
  const rows = map.get(key) ?? [];
5309
- this.#items = rows.map((row) => this.createEntity(row));
5310
- this.#loaded = true;
5311
- return this.#items;
5308
+ this.items = rows.map((row) => this.createEntity(row));
5309
+ this.loaded = true;
5310
+ return this.items;
5312
5311
  }
5313
5312
  /**
5314
5313
  * Gets the current items in the collection.
5315
5314
  * @returns Array of child entities
5316
5315
  */
5317
5316
  getItems() {
5318
- return this.#items;
5317
+ return this.items;
5319
5318
  }
5320
5319
  /**
5321
5320
  * Array-compatible length for testing frameworks.
5322
5321
  */
5323
5322
  get length() {
5324
- return this.#items.length;
5323
+ return this.items.length;
5325
5324
  }
5326
5325
  /**
5327
5326
  * Enables iteration over the collection like an array.
5328
5327
  */
5329
5328
  [Symbol.iterator]() {
5330
- return this.#items[Symbol.iterator]();
5329
+ return this.items[Symbol.iterator]();
5331
5330
  }
5332
5331
  /**
5333
5332
  * Adds a new child entity to the collection.
@@ -5341,8 +5340,8 @@ var DefaultHasManyCollection = class {
5341
5340
  [this.relation.foreignKey]: keyValue
5342
5341
  };
5343
5342
  const entity = this.createEntity(childRow);
5344
- this.#added.add(entity);
5345
- this.#items.push(entity);
5343
+ this.added.add(entity);
5344
+ this.items.push(entity);
5346
5345
  this.ctx.registerRelationChange(
5347
5346
  this.root,
5348
5347
  this.relationKey,
@@ -5361,7 +5360,7 @@ var DefaultHasManyCollection = class {
5361
5360
  const keyValue = this.root[this.localKey];
5362
5361
  entity[this.relation.foreignKey] = keyValue;
5363
5362
  this.ctx.markDirty(entity);
5364
- this.#items.push(entity);
5363
+ this.items.push(entity);
5365
5364
  this.ctx.registerRelationChange(
5366
5365
  this.root,
5367
5366
  this.relationKey,
@@ -5376,8 +5375,8 @@ var DefaultHasManyCollection = class {
5376
5375
  * @param entity - The entity to remove
5377
5376
  */
5378
5377
  remove(entity) {
5379
- this.#items = this.#items.filter((item) => item !== entity);
5380
- this.#removed.add(entity);
5378
+ this.items = this.items.filter((item) => item !== entity);
5379
+ this.removed.add(entity);
5381
5380
  this.ctx.registerRelationChange(
5382
5381
  this.root,
5383
5382
  this.relationKey,
@@ -5391,13 +5390,10 @@ var DefaultHasManyCollection = class {
5391
5390
  * Clears all entities from the collection.
5392
5391
  */
5393
5392
  clear() {
5394
- for (const entity of [...this.#items]) {
5393
+ for (const entity of [...this.items]) {
5395
5394
  this.remove(entity);
5396
5395
  }
5397
5396
  }
5398
- isLoaded() {
5399
- return this.#loaded;
5400
- }
5401
5397
  get relationKey() {
5402
5398
  return `${this.rootTable.name}.${this.relationName}`;
5403
5399
  }
@@ -5406,15 +5402,15 @@ var DefaultHasManyCollection = class {
5406
5402
  if (keyValue === void 0 || keyValue === null) return;
5407
5403
  const rows = getHydrationRows(this.meta, this.relationName, keyValue);
5408
5404
  if (!rows?.length) return;
5409
- this.#items = rows.map((row) => this.createEntity(row));
5410
- this.#loaded = true;
5405
+ this.items = rows.map((row) => this.createEntity(row));
5406
+ this.loaded = true;
5411
5407
  }
5412
5408
  /**
5413
5409
  * Returns the items for JSON serialization.
5414
5410
  * @returns Array of child entities
5415
5411
  */
5416
5412
  toJSON() {
5417
- return this.#items;
5413
+ return this.items;
5418
5414
  }
5419
5415
  };
5420
5416
 
@@ -5425,8 +5421,7 @@ var hideInternal2 = (obj, keys) => {
5425
5421
  Object.defineProperty(obj, key, {
5426
5422
  value: obj[key],
5427
5423
  writable: false,
5428
- configurable: true,
5429
- // Must be configurable for Proxy get trap to work properly
5424
+ configurable: false,
5430
5425
  enumerable: false
5431
5426
  });
5432
5427
  }
@@ -5466,42 +5461,42 @@ var DefaultHasOneReference = class {
5466
5461
  ]);
5467
5462
  this.populateFromHydrationCache();
5468
5463
  }
5469
- #loaded = false;
5470
- #current = null;
5464
+ loaded = false;
5465
+ current = null;
5471
5466
  async load() {
5472
- if (this.#loaded) return this.#current;
5467
+ if (this.loaded) return this.current;
5473
5468
  const map = await this.loader();
5474
5469
  const keyValue = this.root[this.localKey];
5475
5470
  if (keyValue === void 0 || keyValue === null) {
5476
- this.#loaded = true;
5477
- return this.#current;
5471
+ this.loaded = true;
5472
+ return this.current;
5478
5473
  }
5479
5474
  const row = map.get(toKey4(keyValue));
5480
- this.#current = row ? this.createEntity(row) : null;
5481
- this.#loaded = true;
5482
- return this.#current;
5475
+ this.current = row ? this.createEntity(row) : null;
5476
+ this.loaded = true;
5477
+ return this.current;
5483
5478
  }
5484
5479
  get() {
5485
- return this.#current;
5480
+ return this.current;
5486
5481
  }
5487
5482
  set(data) {
5488
5483
  if (data === null) {
5489
5484
  return this.detachCurrent();
5490
5485
  }
5491
5486
  const entity = hasEntityMeta(data) ? data : this.createEntity(data);
5492
- if (this.#current && this.#current !== entity) {
5487
+ if (this.current && this.current !== entity) {
5493
5488
  this.ctx.registerRelationChange(
5494
5489
  this.root,
5495
5490
  this.relationKey,
5496
5491
  this.rootTable,
5497
5492
  this.relationName,
5498
5493
  this.relation,
5499
- { kind: "remove", entity: this.#current }
5494
+ { kind: "remove", entity: this.current }
5500
5495
  );
5501
5496
  }
5502
5497
  this.assignForeignKey(entity);
5503
- this.#current = entity;
5504
- this.#loaded = true;
5498
+ this.current = entity;
5499
+ this.loaded = true;
5505
5500
  this.ctx.registerRelationChange(
5506
5501
  this.root,
5507
5502
  this.relationKey,
@@ -5512,17 +5507,14 @@ var DefaultHasOneReference = class {
5512
5507
  );
5513
5508
  return entity;
5514
5509
  }
5515
- isLoaded() {
5516
- return this.#loaded;
5517
- }
5518
5510
  toJSON() {
5519
- return this.#current;
5511
+ return this.current;
5520
5512
  }
5521
5513
  detachCurrent() {
5522
- const previous = this.#current;
5514
+ const previous = this.current;
5523
5515
  if (!previous) return null;
5524
- this.#current = null;
5525
- this.#loaded = true;
5516
+ this.current = null;
5517
+ this.loaded = true;
5526
5518
  this.ctx.registerRelationChange(
5527
5519
  this.root,
5528
5520
  this.relationKey,
@@ -5545,8 +5537,8 @@ var DefaultHasOneReference = class {
5545
5537
  if (keyValue === void 0 || keyValue === null) return;
5546
5538
  const row = getHydrationRecord(this.meta, this.relationName, keyValue);
5547
5539
  if (!row) return;
5548
- this.#current = this.createEntity(row);
5549
- this.#loaded = true;
5540
+ this.current = this.createEntity(row);
5541
+ this.loaded = true;
5550
5542
  }
5551
5543
  };
5552
5544
 
@@ -5557,8 +5549,7 @@ var hideInternal3 = (obj, keys) => {
5557
5549
  Object.defineProperty(obj, key, {
5558
5550
  value: obj[key],
5559
5551
  writable: false,
5560
- configurable: true,
5561
- // Must be configurable for Proxy get trap to work properly
5552
+ configurable: false,
5562
5553
  enumerable: false
5563
5554
  });
5564
5555
  }
@@ -5588,29 +5579,29 @@ var DefaultBelongsToReference = class {
5588
5579
  hideInternal3(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "targetKey"]);
5589
5580
  this.populateFromHydrationCache();
5590
5581
  }
5591
- #loaded = false;
5592
- #current = null;
5582
+ loaded = false;
5583
+ current = null;
5593
5584
  async load() {
5594
- if (this.#loaded) return this.#current;
5585
+ if (this.loaded) return this.current;
5595
5586
  const map = await this.loader();
5596
5587
  const fkValue = this.root[this.relation.foreignKey];
5597
5588
  if (fkValue === null || fkValue === void 0) {
5598
- this.#current = null;
5589
+ this.current = null;
5599
5590
  } else {
5600
5591
  const row = map.get(toKey5(fkValue));
5601
- this.#current = row ? this.createEntity(row) : null;
5592
+ this.current = row ? this.createEntity(row) : null;
5602
5593
  }
5603
- this.#loaded = true;
5604
- return this.#current;
5594
+ this.loaded = true;
5595
+ return this.current;
5605
5596
  }
5606
5597
  get() {
5607
- return this.#current;
5598
+ return this.current;
5608
5599
  }
5609
5600
  set(data) {
5610
5601
  if (data === null) {
5611
- const previous = this.#current;
5602
+ const previous = this.current;
5612
5603
  this.root[this.relation.foreignKey] = null;
5613
- this.#current = null;
5604
+ this.current = null;
5614
5605
  this.ctx.registerRelationChange(
5615
5606
  this.root,
5616
5607
  this.relationKey,
@@ -5626,7 +5617,7 @@ var DefaultBelongsToReference = class {
5626
5617
  if (pkValue !== void 0) {
5627
5618
  this.root[this.relation.foreignKey] = pkValue;
5628
5619
  }
5629
- this.#current = entity;
5620
+ this.current = entity;
5630
5621
  this.ctx.registerRelationChange(
5631
5622
  this.root,
5632
5623
  this.relationKey,
@@ -5637,9 +5628,6 @@ var DefaultBelongsToReference = class {
5637
5628
  );
5638
5629
  return entity;
5639
5630
  }
5640
- isLoaded() {
5641
- return this.#loaded;
5642
- }
5643
5631
  get relationKey() {
5644
5632
  return `${this.rootTable.name}.${this.relationName}`;
5645
5633
  }
@@ -5648,11 +5636,11 @@ var DefaultBelongsToReference = class {
5648
5636
  if (fkValue === void 0 || fkValue === null) return;
5649
5637
  const row = getHydrationRecord(this.meta, this.relationName, fkValue);
5650
5638
  if (!row) return;
5651
- this.#current = this.createEntity(row);
5652
- this.#loaded = true;
5639
+ this.current = this.createEntity(row);
5640
+ this.loaded = true;
5653
5641
  }
5654
5642
  toJSON() {
5655
- return this.#current;
5643
+ return this.current;
5656
5644
  }
5657
5645
  };
5658
5646
 
@@ -5663,8 +5651,7 @@ var hideInternal4 = (obj, keys) => {
5663
5651
  Object.defineProperty(obj, key, {
5664
5652
  value: obj[key],
5665
5653
  writable: false,
5666
- configurable: true,
5667
- // Must be configurable for Proxy get trap to work properly
5654
+ configurable: false,
5668
5655
  enumerable: false
5669
5656
  });
5670
5657
  }
@@ -5694,45 +5681,45 @@ var DefaultManyToManyCollection = class {
5694
5681
  hideInternal4(this, ["ctx", "meta", "root", "relationName", "relation", "rootTable", "loader", "createEntity", "localKey"]);
5695
5682
  this.hydrateFromCache();
5696
5683
  }
5697
- #loaded = false;
5698
- #items = [];
5684
+ loaded = false;
5685
+ items = [];
5699
5686
  /**
5700
5687
  * Loads the collection items if not already loaded.
5701
5688
  * @returns A promise that resolves to the array of target entities.
5702
5689
  */
5703
5690
  async load() {
5704
- if (this.#loaded) return this.#items;
5691
+ if (this.loaded) return this.items;
5705
5692
  const map = await this.loader();
5706
5693
  const key = toKey6(this.root[this.localKey]);
5707
5694
  const rows = map.get(key) ?? [];
5708
- this.#items = rows.map((row) => {
5695
+ this.items = rows.map((row) => {
5709
5696
  const entity = this.createEntity(row);
5710
5697
  if (row._pivot) {
5711
5698
  entity._pivot = row._pivot;
5712
5699
  }
5713
5700
  return entity;
5714
5701
  });
5715
- this.#loaded = true;
5716
- return this.#items;
5702
+ this.loaded = true;
5703
+ return this.items;
5717
5704
  }
5718
5705
  /**
5719
5706
  * Returns the currently loaded items.
5720
5707
  * @returns Array of target entities.
5721
5708
  */
5722
5709
  getItems() {
5723
- return this.#items;
5710
+ return this.items;
5724
5711
  }
5725
5712
  /**
5726
5713
  * Array-compatible length for testing frameworks.
5727
5714
  */
5728
5715
  get length() {
5729
- return this.#items.length;
5716
+ return this.items.length;
5730
5717
  }
5731
5718
  /**
5732
5719
  * Enables iteration over the collection like an array.
5733
5720
  */
5734
5721
  [Symbol.iterator]() {
5735
- return this.#items[Symbol.iterator]();
5722
+ return this.items[Symbol.iterator]();
5736
5723
  }
5737
5724
  /**
5738
5725
  * Attaches an entity to the collection.
@@ -5742,13 +5729,13 @@ var DefaultManyToManyCollection = class {
5742
5729
  attach(target) {
5743
5730
  const entity = this.ensureEntity(target);
5744
5731
  const id = this.extractId(entity);
5745
- if (id != null && this.#items.some((item) => this.extractId(item) === id)) {
5732
+ if (id != null && this.items.some((item) => this.extractId(item) === id)) {
5746
5733
  return;
5747
5734
  }
5748
- if (id == null && this.#items.includes(entity)) {
5735
+ if (id == null && this.items.includes(entity)) {
5749
5736
  return;
5750
5737
  }
5751
- this.#items.push(entity);
5738
+ this.items.push(entity);
5752
5739
  this.ctx.registerRelationChange(
5753
5740
  this.root,
5754
5741
  this.relationKey,
@@ -5766,9 +5753,9 @@ var DefaultManyToManyCollection = class {
5766
5753
  detach(target) {
5767
5754
  const id = typeof target === "number" || typeof target === "string" ? target : this.extractId(target);
5768
5755
  if (id == null) return;
5769
- const existing = this.#items.find((item) => this.extractId(item) === id);
5756
+ const existing = this.items.find((item) => this.extractId(item) === id);
5770
5757
  if (!existing) return;
5771
- this.#items = this.#items.filter((item) => this.extractId(item) !== id);
5758
+ this.items = this.items.filter((item) => this.extractId(item) !== id);
5772
5759
  this.ctx.registerRelationChange(
5773
5760
  this.root,
5774
5761
  this.relationKey,
@@ -5786,22 +5773,19 @@ var DefaultManyToManyCollection = class {
5786
5773
  async syncByIds(ids) {
5787
5774
  await this.load();
5788
5775
  const normalized = new Set(ids.map((id) => toKey6(id)));
5789
- const currentIds = new Set(this.#items.map((item) => toKey6(this.extractId(item))));
5776
+ const currentIds = new Set(this.items.map((item) => toKey6(this.extractId(item))));
5790
5777
  for (const id of normalized) {
5791
5778
  if (!currentIds.has(id)) {
5792
5779
  this.attach(id);
5793
5780
  }
5794
5781
  }
5795
- for (const item of [...this.#items]) {
5782
+ for (const item of [...this.items]) {
5796
5783
  const itemId = toKey6(this.extractId(item));
5797
5784
  if (!normalized.has(itemId)) {
5798
5785
  this.detach(item);
5799
5786
  }
5800
5787
  }
5801
5788
  }
5802
- isLoaded() {
5803
- return this.#loaded;
5804
- }
5805
5789
  ensureEntity(target) {
5806
5790
  if (typeof target === "number" || typeof target === "string") {
5807
5791
  const stub = {
@@ -5829,17 +5813,17 @@ var DefaultManyToManyCollection = class {
5829
5813
  if (keyValue === void 0 || keyValue === null) return;
5830
5814
  const rows = getHydrationRows(this.meta, this.relationName, keyValue);
5831
5815
  if (!rows?.length) return;
5832
- this.#items = rows.map((row) => {
5816
+ this.items = rows.map((row) => {
5833
5817
  const entity = this.createEntity(row);
5834
5818
  if (row._pivot) {
5835
5819
  entity._pivot = row._pivot;
5836
5820
  }
5837
5821
  return entity;
5838
5822
  });
5839
- this.#loaded = true;
5823
+ this.loaded = true;
5840
5824
  }
5841
5825
  toJSON() {
5842
- return this.#items;
5826
+ return this.items;
5843
5827
  }
5844
5828
  };
5845
5829
 
@@ -6181,31 +6165,12 @@ var relationLoaderCache = (meta, relationName, factory) => {
6181
6165
  // src/orm/entity-relations.ts
6182
6166
  var proxifyRelationWrapper = (wrapper) => {
6183
6167
  return new Proxy(wrapper, {
6184
- get(target, prop, _receiver) {
6185
- if (prop === "toJSON") {
6186
- return () => {
6187
- const wrapperToJSON = target.toJSON;
6188
- if (typeof wrapperToJSON === "function") {
6189
- return wrapperToJSON.call(target);
6190
- }
6191
- const getRef2 = target.get;
6192
- if (typeof getRef2 === "function") {
6193
- return getRef2.call(target);
6194
- }
6195
- const getItems2 = target.getItems;
6196
- if (typeof getItems2 === "function") {
6197
- return getItems2.call(target);
6198
- }
6199
- return target;
6200
- };
6201
- }
6168
+ get(target, prop, receiver) {
6202
6169
  if (typeof prop === "symbol") {
6203
- const value = Reflect.get(target, prop, target);
6204
- return typeof value === "function" ? value.bind(target) : value;
6170
+ return Reflect.get(target, prop, receiver);
6205
6171
  }
6206
6172
  if (prop in target) {
6207
- const value = Reflect.get(target, prop, target);
6208
- return typeof value === "function" ? value.bind(target) : value;
6173
+ return Reflect.get(target, prop, receiver);
6209
6174
  }
6210
6175
  const getItems = target.getItems;
6211
6176
  if (typeof getItems === "function") {
@@ -6227,12 +6192,12 @@ var proxifyRelationWrapper = (wrapper) => {
6227
6192
  }
6228
6193
  return void 0;
6229
6194
  },
6230
- set(target, prop, value, _receiver) {
6195
+ set(target, prop, value, receiver) {
6231
6196
  if (typeof prop === "symbol") {
6232
- return Reflect.set(target, prop, value, target);
6197
+ return Reflect.set(target, prop, value, receiver);
6233
6198
  }
6234
6199
  if (prop in target) {
6235
- return Reflect.set(target, prop, value, target);
6200
+ return Reflect.set(target, prop, value, receiver);
6236
6201
  }
6237
6202
  const getRef = target.get;
6238
6203
  if (typeof getRef === "function") {
@@ -6246,7 +6211,7 @@ var proxifyRelationWrapper = (wrapper) => {
6246
6211
  const items = getItems.call(target);
6247
6212
  return Reflect.set(items, prop, value);
6248
6213
  }
6249
- return Reflect.set(target, prop, value, target);
6214
+ return Reflect.set(target, prop, value, receiver);
6250
6215
  }
6251
6216
  });
6252
6217
  };
@@ -6348,15 +6313,7 @@ var instantiateWrapper = (meta, relationName, relation, owner, createEntity) =>
6348
6313
  // src/orm/entity.ts
6349
6314
  var isRelationWrapperLoaded = (value) => {
6350
6315
  if (!value || typeof value !== "object") return false;
6351
- const wrapper = value;
6352
- if (typeof wrapper.isLoaded === "function") return wrapper.isLoaded();
6353
- return false;
6354
- };
6355
- var unwrapRelationForJson = (value) => {
6356
- if (value && typeof value === "object" && typeof value.toJSON === "function") {
6357
- return value.toJSON();
6358
- }
6359
- return value;
6316
+ return Boolean(value.loaded);
6360
6317
  };
6361
6318
  var createEntityProxy = (ctx, table, row, lazyRelations = [], lazyRelationOptions = /* @__PURE__ */ new Map()) => {
6362
6319
  const target = { ...row };
@@ -6373,13 +6330,12 @@ var createEntityProxy = (ctx, table, row, lazyRelations = [], lazyRelationOption
6373
6330
  const buildJson = (self) => {
6374
6331
  const json = {};
6375
6332
  for (const key of Object.keys(target)) {
6376
- const value = self[key];
6377
- json[key] = unwrapRelationForJson(value);
6333
+ json[key] = self[key];
6378
6334
  }
6379
6335
  for (const [relationName, wrapper] of meta.relationWrappers) {
6380
6336
  if (Object.prototype.hasOwnProperty.call(json, relationName)) continue;
6381
6337
  if (isRelationWrapperLoaded(wrapper)) {
6382
- json[relationName] = unwrapRelationForJson(wrapper);
6338
+ json[relationName] = wrapper;
6383
6339
  }
6384
6340
  }
6385
6341
  return json;