metal-orm 1.0.107 → 1.0.108

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
@@ -5123,7 +5123,7 @@ var hydrateRows = (rows, plan) => {
5123
5123
  const rootId = row[plan.rootPrimaryKey];
5124
5124
  if (rootId === void 0 || rootId === null) {
5125
5125
  if (!hasRelations) {
5126
- rootMap.set(Symbol(), createBaseRow(row, plan));
5126
+ rootMap.set(/* @__PURE__ */ Symbol(), createBaseRow(row, plan));
5127
5127
  }
5128
5128
  continue;
5129
5129
  }
@@ -5183,7 +5183,7 @@ var buildPivot = (row, rel) => {
5183
5183
  };
5184
5184
 
5185
5185
  // src/orm/entity-meta.ts
5186
- var ENTITY_META = Symbol("EntityMeta");
5186
+ var ENTITY_META = /* @__PURE__ */ Symbol("EntityMeta");
5187
5187
  var toKey = (value) => value === null || value === void 0 ? "" : String(value);
5188
5188
  var getHydrationRows = (meta, relationName, key) => {
5189
5189
  const map = meta.relationHydration.get(relationName);
@@ -5422,7 +5422,10 @@ var DefaultHasManyCollection = class {
5422
5422
  * @returns Array of child entities
5423
5423
  */
5424
5424
  toJSON() {
5425
- return this.items;
5425
+ return this.items.map((item) => {
5426
+ const entityWithToJSON = item;
5427
+ return typeof entityWithToJSON.toJSON === "function" ? entityWithToJSON.toJSON() : item;
5428
+ });
5426
5429
  }
5427
5430
  };
5428
5431
 
@@ -5532,7 +5535,9 @@ var DefaultHasOneReference = class {
5532
5535
  return entity;
5533
5536
  }
5534
5537
  toJSON() {
5535
- return this.current;
5538
+ if (!this.current) return null;
5539
+ const entityWithToJSON = this.current;
5540
+ return typeof entityWithToJSON.toJSON === "function" ? entityWithToJSON.toJSON() : this.current;
5536
5541
  }
5537
5542
  detachCurrent() {
5538
5543
  const previous = this.current;
@@ -5676,7 +5681,9 @@ var DefaultBelongsToReference = class {
5676
5681
  this.loaded = true;
5677
5682
  }
5678
5683
  toJSON() {
5679
- return this.current;
5684
+ if (!this.current) return null;
5685
+ const entityWithToJSON = this.current;
5686
+ return typeof entityWithToJSON.toJSON === "function" ? entityWithToJSON.toJSON() : this.current;
5680
5687
  }
5681
5688
  };
5682
5689
 
@@ -5871,7 +5878,10 @@ var DefaultManyToManyCollection = class {
5871
5878
  this.loaded = true;
5872
5879
  }
5873
5880
  toJSON() {
5874
- return this.items;
5881
+ return this.items.map((item) => {
5882
+ const entityWithToJSON = item;
5883
+ return typeof entityWithToJSON.toJSON === "function" ? entityWithToJSON.toJSON() : item;
5884
+ });
5875
5885
  }
5876
5886
  };
5877
5887
 
@@ -6375,17 +6385,38 @@ var createEntityProxy = (ctx, table, row, lazyRelations = [], lazyRelationOption
6375
6385
  relationWrappers: /* @__PURE__ */ new Map()
6376
6386
  };
6377
6387
  const createRelationEntity = (relationTable, relationRow) => createEntityFromRow(meta.ctx, relationTable, relationRow);
6378
- const buildJson = (self) => {
6388
+ const isCollectionRelation = (relationName) => {
6389
+ const rel = table.relations[relationName];
6390
+ if (!rel) return false;
6391
+ return rel.type === RelationKinds.HasMany || rel.type === RelationKinds.BelongsToMany;
6392
+ };
6393
+ const buildJson = (self, options) => {
6379
6394
  const json = {};
6395
+ const includeAll = options?.includeAllRelations ?? true;
6380
6396
  for (const key of Object.keys(target)) {
6381
- if (table.relations[key]) continue;
6382
- json[key] = self[key];
6397
+ if (!table.relations[key]) {
6398
+ json[key] = self[key];
6399
+ }
6383
6400
  }
6384
- for (const relationName of Object.keys(table.relations)) {
6385
- const wrapper = self[relationName];
6386
- if (wrapper && isRelationWrapperLoaded(wrapper)) {
6387
- const wrapperWithToJSON = wrapper;
6388
- json[relationName] = typeof wrapperWithToJSON.toJSON === "function" ? wrapperWithToJSON.toJSON() : wrapper;
6401
+ if (includeAll) {
6402
+ for (const relationName of Object.keys(table.relations)) {
6403
+ const wrapper = self[relationName];
6404
+ if (wrapper && isRelationWrapperLoaded(wrapper)) {
6405
+ const wrapperWithToJSON = wrapper;
6406
+ json[relationName] = typeof wrapperWithToJSON.toJSON === "function" ? wrapperWithToJSON.toJSON() : wrapper;
6407
+ } else {
6408
+ json[relationName] = isCollectionRelation(relationName) ? [] : null;
6409
+ }
6410
+ }
6411
+ } else {
6412
+ for (const key of Object.keys(target)) {
6413
+ if (table.relations[key]) {
6414
+ const wrapper = self[key];
6415
+ if (wrapper && isRelationWrapperLoaded(wrapper)) {
6416
+ const wrapperWithToJSON = wrapper;
6417
+ json[key] = typeof wrapperWithToJSON.toJSON === "function" ? wrapperWithToJSON.toJSON() : wrapper;
6418
+ }
6419
+ }
6389
6420
  }
6390
6421
  }
6391
6422
  return json;
@@ -6413,7 +6444,7 @@ var createEntityProxy = (ctx, table, row, lazyRelations = [], lazyRelationOption
6413
6444
  if (prop in targetObj) {
6414
6445
  return Reflect.get(targetObj, prop, receiver);
6415
6446
  }
6416
- return () => buildJson(receiver);
6447
+ return (options) => buildJson(receiver, options);
6417
6448
  }
6418
6449
  if (typeof prop === "string" && table.relations[prop]) {
6419
6450
  return getRelationWrapper(meta, prop, receiver, createRelationEntity);