Skip to content

Commit 314c7f4

Browse files
authored
chore: update README to document conditional upserts feature (#60)
- Add conditional upserts to features list - Add comprehensive section explaining conditional upserts - Include usage examples with comparison operators - Update SQL examples to show conditional syntax for MySQL and PostgreSQL - Document practical use cases like optimistic locking and concurrent updates
1 parent 4978309 commit 314c7f4

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ all correct and functional.
1919

2020
- Upsert a single entity or a list of entities
2121
- Support for custom ON clauses and ignored fields
22+
- Conditional upserts with comparison operators (>, >=, <, <=)
2223
- Compatible with Spring Data JPA repositories
2324
- Database-specific optimizations for MySQL and PostgreSQL
2425
- Automatic handling of generated keys
@@ -140,6 +141,41 @@ The method name is parsed to extract the following information:
140141
- `Ignoring<FieldName>`: The field(s) to ignore during updates (e.g., `IgnoringUpdatedAt`)
141142
- `IgnoringAllFields`: Whether to ignore all fields during updates (only insert new rows)
142143

144+
### Conditional Upserts
145+
146+
Since version 1.3.0, the library supports conditional upserts using the `When` clause in method names. This allows you to specify conditions under which the update should occur, preventing updates when certain conditions are not met.
147+
148+
You can use comparison operators to check field values:
149+
- `More` (>): Update only when the new value is greater than the existing value
150+
- `MoreOrEqual` (>=): Update only when the new value is greater than or equal to the existing value
151+
- `Less` (<): Update only when the new value is less than the existing value
152+
- `LessOrEqual` (<=): Update only when the new value is less than or equal to the existing value
153+
154+
```kotlin
155+
interface UserRepository : UpsertRepository<User, Long> {
156+
// Update only if the new updatedAt is more recent than the existing one
157+
fun upsertOnIdWhenUpdatedAtMore(user: User): Int
158+
159+
// Update only if the new version is greater than or equal to the existing one
160+
fun upsertOnIdWhenVersionMoreOrEqual(user: User): Int
161+
162+
// Update only if the new price is less than the existing one
163+
fun upsertOnIdWhenPriceLess(user: User): Int
164+
165+
// Combine conditional with ignored fields
166+
fun upsertOnIdWhenVersionMoreIgnoringCreatedAt(user: User): Int
167+
168+
// Batch operations with conditions
169+
fun upsertAllOnIdWhenUpdatedAtMore(users: List<User>): Int
170+
}
171+
```
172+
173+
This is particularly useful for:
174+
- **Optimistic locking**: Update only if the version number is higher
175+
- **Time-based updates**: Update only with more recent data
176+
- **Price protection**: Prevent accidental price increases
177+
- **Concurrent update protection**: Avoid overwriting newer data with older data
178+
143179
## Configuration
144180

145181
The library is automatically configured when you include it in your Spring Boot application. No
@@ -444,6 +480,16 @@ ON DUPLICATE KEY UPDATE
444480
email = VALUES(email)
445481
```
446482

483+
With conditional updates (MySQL 8.0.19+):
484+
```sql
485+
INSERT INTO users (id, username, email, version)
486+
VALUES (:id, :username, :email, :version)
487+
ON DUPLICATE KEY UPDATE
488+
username = IF(VALUES(version) > version, VALUES(username), username),
489+
email = IF(VALUES(version) > version, VALUES(email), email),
490+
version = IF(VALUES(version) > version, VALUES(version), version)
491+
```
492+
447493
[Learn more about MySQL implementation](docs/mysql.md)
448494

449495
#### PostgreSQL
@@ -459,6 +505,17 @@ ON CONFLICT (id) DO UPDATE SET
459505
email = EXCLUDED.email
460506
```
461507

508+
With conditional updates:
509+
```sql
510+
INSERT INTO users (id, username, email, updated_at)
511+
VALUES (:id, :username, :email, :updated_at)
512+
ON CONFLICT (id) DO UPDATE SET
513+
username = EXCLUDED.username,
514+
email = EXCLUDED.email,
515+
updated_at = EXCLUDED.updated_at
516+
WHERE EXCLUDED.updated_at > users.updated_at
517+
```
518+
462519
[Learn more about PostgreSQL implementation](docs/postgresql.md)
463520

464521
## Best Practices

0 commit comments

Comments
 (0)