Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): type safe references #691

Merged
merged 1 commit into from
Jul 30, 2020
Merged

feat(core): type safe references #691

merged 1 commit into from
Jul 30, 2020

Commits on Jul 30, 2020

  1. feat(core): type safe references

    EM now returns `Loaded<T, P>` instead of the entity (`T`). This type automatically
    adds synchronous method `get()` that returns the entity (for references) or array
    of entities (for collections).
    
    There is also `$` property that contains the same as return value of this `get()`
    method.
    
    ```typescript
    const book1 = await orm.em.findOneOrFail(Book, bible, { populate: ['publisher', 'tags'] });
    expect(book1.publisher!.$.name).toBe('Publisher 123');
    expect(book1.tags!.$[0].name).toBe('t1');
    expect(book1.tags!.$[1].name).toBe('t2');
    expect(book1.tags!.$[2].name).toBe('t3');
    orm.em.clear();
    
    const books = await orm.em.find(Book, { id: bible.id }, { populate:
      { publisher: { books: { publisher: true } } },
    });
    expect(books[0].publisher!.$.books.$[0].publisher!.$.name).toBe('Publisher 123');
    
    const book5 = await orm.em.findOneOrFail(Book, bible, { populate:
      { publisher: true, tags: true, perex: true },
    });
    expect(book5.publisher!.$.name).toBe('Publisher 123');
    expect(book5.tags.$[0].name).toBe('t1');
    ```
    
    Closes #214
    
    BREAKING CHANGE:
    `Reference.get()` is now available only with correct `Loaded` type hint and is used
    as a sync getter for the entity, just like `unwrap()`. You can use `Reference.load(prop)`
    for the original `get()` method functionality.
    
    `em.find()` and similar methods now have two type arguments, due to TypeScript not supporting
    partial type inference, it is no longer possible to specify the `T` explicitly (without also
    explicitly specifying the load hint).
    B4nan committed Jul 30, 2020
    Configuration menu
    Copy the full SHA
    4b57cf4 View commit details
    Browse the repository at this point in the history