https://github.com/darky/typeorm-optimistic-lock
TypeORM helper for optimistic lock persistance
https://github.com/darky/typeorm-optimistic-lock
lock optimistic typeorm
Last synced: 7 months ago
JSON representation
TypeORM helper for optimistic lock persistance
- Host: GitHub
- URL: https://github.com/darky/typeorm-optimistic-lock
- Owner: darky
- Created: 2023-04-12T20:07:31.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2023-04-12T21:09:10.000Z (about 3 years ago)
- Last Synced: 2025-02-23T20:47:20.356Z (over 1 year ago)
- Topics: lock, optimistic, typeorm
- Language: TypeScript
- Homepage:
- Size: 23.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# typeorm-optimistic-lock
TypeORM helper for optimistic lock persistance
## Notes
* This library tested only for Postgres, but potentially can be improved for any database
* You should manually detect type of persistance: insert or update. Detected by entity existance while fetching as usual
* Entity should have required integer property `version`
* On inserting need to set `version: 1`
* On updating in `whereUpdate` need to use current `version`, in `forUpdate` need to set current `version` + 1
## Insert example
```ts
import { typeormOptimisticLockSave } from "typeorm-optimistic-lock";
await typeormOptimisticLockSave({
repository: connection.getRepository(Test),
conflictError: new Error("conflict happens"),
forInsert: { id: 1, text: "foo bar", version: 1 },
});
```
## Update example
```ts
import { typeormOptimisticLockSave } from "typeorm-optimistic-lock";
await typeormOptimisticLockSave({
repository: connection.getRepository(Test),
conflictError: new Error("conflict happens"),
forUpdate: { id: 1, text: "test", version: 2 },
whereUpdate: { id: 1, version: 1 },
});
```