Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): implement auto-flush mode #2491

Merged
merged 12 commits into from
Dec 11, 2021
Merged

feat(core): implement auto-flush mode #2491

merged 12 commits into from
Dec 11, 2021

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Dec 2, 2021

Checks state before querying entities and flush automatically if we have queued changes to entities that are being queried.

The flushing strategy is given by the flushMode of the current running EntityManager.

  • FlushMode.COMMIT - The EntityManager tries to delay the flush until the current Transaction is committed, although it might flush prematurely too.
  • FlushMode.AUTO - This is the default mode, and it flushes the EntityManager only if necessary.
  • FlushMode.ALWAYS - Flushes the EntityManager before every query.

FlushMode.AUTO will try to detect changes on the entity we are querying, and flush
if there is an overlap:

// querying for author will trigger auto-flush if we have new author persisted
const a1 = new Author(...);
orm.em.persist(a1);
const r1 = await orm.em.find(Author, {});

// querying author won't trigger auto-flush if we have new book, but no changes on author
const b4 = new Book(...);
orm.em.persist(b4);
const r2 = await orm.em.find(Author, {});

// but querying for book will trigger auto-flush
const r3 = await orm.em.find(Book, {});

We can set the flush mode on different places:

  • in the ORM config via Options.flushMode
  • for given EntityManager instance (and its forks) via em.setFlushMode()
  • for given EntityManager fork via em.fork({ flushMode })
  • for given QueryBuilder instance via qb.setFlushMode()
  • for given transaction scope via em.transactional(..., { flushMode })

Based on https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/chapters/flushing/Flushing.html

Closes #2359

@codecov-commenter
Copy link

codecov-commenter commented Dec 3, 2021

Codecov Report

Merging #2491 (f609d48) into master (a0827f5) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #2491   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          190       190           
  Lines        10874     10958   +84     
  Branches      2489      2502   +13     
=========================================
+ Hits         10874     10958   +84     
Impacted Files Coverage Δ
packages/core/src/drivers/IDatabaseDriver.ts 100.00% <ø> (ø)
packages/core/src/enums.ts 100.00% <ø> (ø)
packages/core/src/EntityManager.ts 100.00% <100.00%> (ø)
packages/core/src/entity/BaseEntity.ts 100.00% <100.00%> (ø)
packages/core/src/entity/EntityFactory.ts 100.00% <100.00%> (ø)
packages/core/src/entity/EntityHelper.ts 100.00% <100.00%> (ø)
packages/core/src/entity/WrappedEntity.ts 100.00% <100.00%> (ø)
packages/core/src/typings.ts 100.00% <100.00%> (ø)
packages/core/src/unit-of-work/UnitOfWork.ts 100.00% <100.00%> (ø)
packages/core/src/utils/Configuration.ts 100.00% <100.00%> (ø)
... and 5 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a0827f5...f609d48. Read the comment docs.

@B4nan B4nan marked this pull request as ready for review December 5, 2021 11:49
@B4nan B4nan merged commit f1d8bf1 into master Dec 11, 2021
@B4nan B4nan deleted the auto-flush branch December 11, 2021 22:24
@B4nan B4nan mentioned this pull request Dec 11, 2021
48 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support "Auto Flush"
2 participants