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