Skip to content

Instantly share code, notes, and snippets.

@B4nan
Created February 6, 2022 01:43
Show Gist options
  • Select an option

  • Save B4nan/9bc271492f7fe72ddfdda4437f7658e7 to your computer and use it in GitHub Desktop.

Select an option

Save B4nan/9bc271492f7fe72ddfdda4437f7658e7 to your computer and use it in GitHub Desktop.
// first partially load author with `id` and `email` only
const a1 = await em.findOneOrFail(Author, 123, { fields: ['id', 'email'] });
a1.email = 'lol'; // let's change the email
// reloading with same fields won't fire the query (as before)
const a2 = await em.findOneOrFail(Author, 123, { fields: ['email'] });
console.log(a1 === a2); // true, same entity instance, no query was fired
// reloading with additional fields will work without `refresh: true`
const a3 = await em.findOneOrFail(Author, 123, { fields: ['id', 'age'] });
console.log(a1 === a3); // true, same entity instance, but updated!
console.log(a1.age); // new values are loaded
a1.age = 1000; // let's override them
// reloading full entity will work without `refresh: true`
const a4 = await em.findOneOrFail(Author, 123, { populate: ['books'] });
console.log(a1 === a4); // true, same entity instance, but updated!
console.log(a1.termsAccepted); // new values are loaded
await em.flush(); // updates the author with new email and age
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment