Skip to content

Instantly share code, notes, and snippets.

@B4nan
Last active April 30, 2019 16:24
Show Gist options
  • Save B4nan/9a78b0bf501db5c9386650359c3bba3c to your computer and use it in GitHub Desktop.
Save B4nan/9a78b0bf501db5c9386650359c3bba3c to your computer and use it in GitHub Desktop.
Persisting entities in MikroORM
const author = new Author('Jon Snow', 'snow@wall.st');
author.born = new Date();
const publisher = new Publisher('7K publisher');
const book1 = new Book('My Life on The Wall, part 1', author);
book1.publisher = publisher;
const book2 = new Book('My Life on The Wall, part 2', author);
book2.publisher = publisher;
const book3 = new Book('My Life on The Wall, part 3', author);
book3.publisher = publisher;
// just persist books, author and publisher will be automatically cascade persisted
await orm.em.persistAndFlush([book1, book2, book3]);
// or one by one
orm.em.persistLater(book1);
orm.em.persistLater(book2);
orm.em.persistLater(book3);
await orm.em.flush(); // flush everything to database at once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment