Async Disposable
Joist’s EntityManager
can be used with the new using
keyword in TypeScript 5.2, to auto-flush
changes to the database.
For example, in a method that creates an EntityManager
:
async function performWork() { // Create an EntityManager w/your context & driver await using em = new EntityManager({}, driver); // Load an entity const a1 = await em.load(Author, "a:1"); // Make any mutations a1.firstName = "a2"; // That's it; `em.flush` will be called automatically}
Note that the em.flush
method can fail if any validation rules are invalid, or any errors occur while running hooks, in which case the caller of performWork
would get a rejected promise.