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): add support for concurrency checks #2437

Merged
merged 1 commit into from
Nov 20, 2021
Merged

Conversation

B4nan
Copy link
Member

@B4nan B4nan commented Nov 20, 2021

As opposed to version fields that are handled automatically, we can use
concurrency checks. They allow us to mark specific properties to be included
in the concurrency check, just like the version field was. But this time, we
will be responsible for updating the fields explicitly.

When we try to update such entity without changing one of the concurrency fields,
OptimisticLockError will be thrown. Same mechanism is then used to check whether
the update succeeded, and throw the same type of error when not.

@Entity()
export class ConcurrencyCheckUser {

  // all primary keys are by default part of the concurrency check
  @PrimaryKey({ length: 100 })
  firstName: string;

  // all primary keys are by default part of the concurrency check
  @PrimaryKey({ length: 100 })
  lastName: string;

  @Property({ concurrencyCheck: true })
  age: number;

  @Property({ nullable: true })
  other?: string;

}

Closes #1638

As opposed to version fields that are handled automatically, we can use
concurrency checks. They allow us to mark specific properties to be included
in the concurrency check, just like the version field was. But this time, we
will be responsible for updating the fields explicitly.

When we try to update such entity without changing one of the concurrency fields,
`OptimisticLockError` will be thrown. Same mechanism is then used to check whether
the update succeeded, and throw the same type of error when not.

```ts
@entity()
export class ConcurrencyCheckUser {

  // all primary keys are by default part of the concurrency check
  @PrimaryKey({ length: 100 })
  firstName: string;

  // all primary keys are by default part of the concurrency check
  @PrimaryKey({ length: 100 })
  lastName: string;

  @Property({ concurrencyCheck: true })
  age: number;

  @Property({ nullable: true })
  other?: string;

}
```

Closes #1638
@codecov-commenter
Copy link

codecov-commenter commented Nov 20, 2021

Codecov Report

Merging #2437 (e9287cd) into master (209719d) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##            master     #2437   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          188       188           
  Lines        10724     10747   +23     
  Branches      2445      2451    +6     
=========================================
+ Hits         10724     10747   +23     
Impacted Files Coverage Δ
packages/core/src/decorators/Property.ts 100.00% <ø> (ø)
packages/core/src/metadata/MetadataDiscovery.ts 100.00% <100.00%> (ø)
packages/core/src/typings.ts 100.00% <100.00%> (ø)
...ckages/core/src/unit-of-work/ChangeSetPersister.ts 100.00% <100.00%> (ø)
packages/knex/src/AbstractSqlDriver.ts 100.00% <100.00%> (ø)

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 209719d...e9287cd. Read the comment docs.

@B4nan B4nan merged commit acd43fe into master Nov 20, 2021
@B4nan B4nan deleted the concurrency-checks branch November 20, 2021 15:12
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.

Allow control over optimistic version numbers
2 participants