metal-orm 1.0.102 → 1.0.104

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