Last active
April 30, 2019 16:24
-
-
Save B4nan/9299a15612055c5cf6847b44ae07975f to your computer and use it in GitHub Desktop.
Using entity references in MikroORM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const book = orm.em.findOne(Book, '...'); | |
| console.log(book.author); // reference with ID only, instance of Author entity | |
| // this will get the same reference as we already have in `book.author` | |
| const author = orm.em.getReference(Author, book.author.id); | |
| console.log(author.id); // accessing the id will not trigger any db call | |
| console.log(author.isInitialized()); // false | |
| console.log(author.name); // undefined | |
| console.log(author === book.author); // true | |
| // this will trigger db call, we could also use `orm.em.findOne(Author, author.id)` to do the same | |
| await author.init(); | |
| console.log(author.isInitialized()); // true | |
| console.log(author.name); // defined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment