Skip to content

Instantly share code, notes, and snippets.

@B4nan
Created February 26, 2019 19:39
Show Gist options
  • Save B4nan/d572179c3fa2edcc6e0bdee60f57d5c7 to your computer and use it in GitHub Desktop.
Save B4nan/d572179c3fa2edcc6e0bdee60f57d5c7 to your computer and use it in GitHub Desktop.
Working with collections in MikroORM
// find author and populate his books collection
const author = orm.em.findOne(Author, '...', ['books']);
for (const book of author.books) {
console.log(book); // instance of Book
}
author.books.add(book);
console.log(author.books.contains(book)); // true
author.books.remove(book);
console.log(author.books.contains(book)); // false
author.books.add(book);
console.log(author.books.count()); // 1
console.log(author.books.getItems()); // Book[]
console.log(author.books.getIdentifiers()); // array of primary keys of all items
author.books.removeAll();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment