Skip to content

Commit

Permalink
feat(core): add Reference.getEntity() and Reference.getProperty()
Browse files Browse the repository at this point in the history
Similar to `Collection.getItems()`, it will first check if the entity is initialized, and throw if not.

Closes #304
  • Loading branch information
Martin Adamek committed Jan 27, 2020
1 parent ad6337e commit 05dc5ce
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
14 changes: 14 additions & 0 deletions docs/docs/entity-references.md
Expand Up @@ -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<Author> 123 not initialized
console.log(book.author.getProperty('name')); // Error: Reference<Author> 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:
Expand Down
14 changes: 13 additions & 1 deletion lib/entity/Reference.ts
Expand Up @@ -58,7 +58,19 @@ export class Reference<T extends AnyEntity<T>> {
}

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<K extends keyof T>(prop: K): T[K] {
return this.getEntity()[prop];
}

isInitialized(): boolean {
Expand Down
3 changes: 3 additions & 0 deletions tests/EntityManager.mongo.test.ts
Expand Up @@ -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<Author> ${ref.id} not initialized`);
expect(() => ref.getProperty('email')).toThrowError(`Reference<Author> ${ref.id} not initialized`);

const ref2 = Reference.create(author);
const ref3 = Reference.create(ref2);
Expand All @@ -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');
});

Expand Down

0 comments on commit 05dc5ce

Please sign in to comment.