From 05dc5ceb00d96a1080a49c472edab1036d2370d7 Mon Sep 17 00:00:00 2001 From: Martin Adamek Date: Mon, 27 Jan 2020 19:28:42 +0100 Subject: [PATCH] feat(core): add `Reference.getEntity()` and `Reference.getProperty()` Similar to `Collection.getItems()`, it will first check if the entity is initialized, and throw if not. Closes #304 --- docs/docs/entity-references.md | 14 ++++++++++++++ lib/entity/Reference.ts | 14 +++++++++++++- tests/EntityManager.mongo.test.ts | 3 +++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/docs/docs/entity-references.md b/docs/docs/entity-references.md index a8a7a5616945..e0b91c78f337 100644 --- a/docs/docs/entity-references.md +++ b/docs/docs/entity-references.md @@ -85,6 +85,20 @@ console.log((await book.author.load()).name); // ok, author already loaded console.log(book.author.unwrap().name); // ok, author already loaded ``` +There are also `getEntity()` and `getProperty()` methods that are synchronous getters, +that will first check if the wrapped entity is initialized, and if not, it will throw +and error. + +```typescript +const book = await orm.em.findOne(Book, 1); +console.log(book.author instanceof Reference); // true +console.log(book.author.isInitialized()); // false +console.log(book.author.getEntity()); // Error: Reference 123 not initialized +console.log(book.author.getProperty('name')); // Error: Reference 123 not initialized +console.log((await book.author.get('name'))); // ok, loading the author first +console.log(book.author.getProperty('name')); // ok, author already loaded +``` + If you use different metadata provider than `TsMorphMetadataProvider` (e.g. `ReflectMetadataProvider`), you will also need to explicitly set `wrappedReference` parameter: diff --git a/lib/entity/Reference.ts b/lib/entity/Reference.ts index 213a60cfcf31..ee3cff94634c 100644 --- a/lib/entity/Reference.ts +++ b/lib/entity/Reference.ts @@ -58,7 +58,19 @@ export class Reference> { } unwrap(): T { - return wrap(this.entity); + return this.entity; + } + + getEntity(): T { + if (!this.isInitialized()) { + throw new Error(`Reference<${this.__meta.name}> ${this.__primaryKey} not initialized`); + } + + return this.entity; + } + + getProperty(prop: K): T[K] { + return this.getEntity()[prop]; } isInitialized(): boolean { diff --git a/tests/EntityManager.mongo.test.ts b/tests/EntityManager.mongo.test.ts index 5aee699d07c2..534836970a9c 100644 --- a/tests/EntityManager.mongo.test.ts +++ b/tests/EntityManager.mongo.test.ts @@ -1611,6 +1611,8 @@ describe('EntityManagerMongo', () => { expect(ref._id).toBeInstanceOf(ObjectId); expect(ref.unwrap()).toBeInstanceOf(Author); expect(wrap(ref.unwrap()).isInitialized()).toBe(false); + expect(() => ref.getEntity()).toThrowError(`Reference ${ref.id} not initialized`); + expect(() => ref.getProperty('email')).toThrowError(`Reference ${ref.id} not initialized`); const ref2 = Reference.create(author); const ref3 = Reference.create(ref2); @@ -1632,6 +1634,7 @@ describe('EntityManagerMongo', () => { expect(ref4.isInitialized()).toBe(false); await expect(ref4.get('name')).resolves.toBe('God'); expect(ref4.isInitialized()).toBe(true); + expect(ref4.getProperty('name')).toBe('God'); await expect(ref4.get('email')).resolves.toBe('hello@heaven.god'); });