@mikro-orm/core 6.4.17-dev.19 → 6.4.17-dev.20

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.
@@ -56,6 +56,11 @@ export declare class ScalarReference<Value> {
56
56
  * Returns either the whole entity, or the requested property.
57
57
  */
58
58
  load(options?: Omit<LoadReferenceOptions<any, any>, 'populate' | 'fields' | 'exclude'>): Promise<Value | undefined>;
59
+ /**
60
+ * Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
61
+ * Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options).
62
+ */
63
+ loadOrFail(options?: Omit<LoadReferenceOrFailOptions<any, any>, 'populate' | 'fields' | 'exclude'>): Promise<Value>;
59
64
  set(value: Value): void;
60
65
  bind<Entity extends object>(entity: Entity, property: EntityKey<Entity>): void;
61
66
  unwrap(): Value | undefined;
@@ -7,6 +7,7 @@ const node_util_1 = require("node:util");
7
7
  const enums_1 = require("../enums");
8
8
  const wrap_1 = require("./wrap");
9
9
  const utils_1 = require("../utils");
10
+ const errors_1 = require("../errors");
10
11
  class Reference {
11
12
  entity;
12
13
  constructor(entity) {
@@ -178,6 +179,21 @@ class ScalarReference {
178
179
  }
179
180
  return this.value;
180
181
  }
182
+ /**
183
+ * Ensures the underlying entity is loaded first (without reloading it if it already is loaded).
184
+ * Returns the entity or throws an error just like `em.findOneOrFail()` (and respects the same config options).
185
+ */
186
+ async loadOrFail(options = {}) {
187
+ const ret = await this.load(options);
188
+ if (!ret) {
189
+ const wrapped = (0, wrap_1.helper)(this.entity);
190
+ options.failHandler ??= wrapped.__em.config.get('findOneOrFailHandler');
191
+ const entityName = this.entity.constructor.name;
192
+ const where = wrapped.getPrimaryKey();
193
+ throw new errors_1.NotFoundError(`${entityName} (${where}) failed to load property '${this.property}'`);
194
+ }
195
+ return ret;
196
+ }
181
197
  set(value) {
182
198
  this.value = value;
183
199
  this.initialized = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/core",
3
- "version": "6.4.17-dev.19",
3
+ "version": "6.4.17-dev.20",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -64,7 +64,7 @@
64
64
  "esprima": "4.0.1",
65
65
  "fs-extra": "11.3.0",
66
66
  "globby": "11.1.0",
67
- "mikro-orm": "6.4.17-dev.19",
67
+ "mikro-orm": "6.4.17-dev.20",
68
68
  "reflect-metadata": "0.2.2"
69
69
  }
70
70
  }