Skip to content

Commit 3cba7fe

Browse files
committed
fix: strip meta and null values on create/update
- meta/_META is read-only, now always stripped before sending - null values are stripped, enabling partial updates - Update README to clarify these behaviors For updates, only key + changed fields needed: new Contact(key, null, "New Name", null, null)
1 parent cd3fe40 commit 3cba7fe

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,27 @@ var sbx = SBXServiceFactory.builder()
8181
// Get typed repository
8282
SbxRepository<Contact> contacts = sbx.repository(Contact.class);
8383

84-
// Create
84+
// Create (meta is ignored, null values are ignored)
8585
var contact = new Contact("John Doe", "john@example.com", "ACTIVE");
8686
String key = contacts.save(contact);
8787

8888
// Read
8989
Optional<Contact> found = contacts.findById(key);
9090
List<Contact> all = contacts.findAll();
9191

92-
// Update
93-
var updated = new Contact(key, found.get().meta(), "John Updated", "john@example.com", "ACTIVE");
94-
contacts.save(updated); // Has key, so updates
92+
// Update - only _KEY and changed fields needed (partial update)
93+
var updated = new Contact(key, null, "John Updated", null, null);
94+
contacts.save(updated); // Only updates name, other fields unchanged
9595

9696
// Delete
9797
contacts.deleteById(key);
9898
```
9999

100+
**Important:**
101+
- `meta` is read-only and always stripped before create/update
102+
- `null` values are stripped, enabling partial updates
103+
- For updates, only `key` + changed fields are required
104+
100105
---
101106

102107
## Repository API
@@ -192,8 +197,8 @@ repo.query().whereKeys("k1", "k2").list()
192197
```java
193198
@SbxModel("contact")
194199
public record Contact(
195-
String key,
196-
SBXMeta meta,
200+
String key, // Primary key (_KEY) - null for new records
201+
SBXMeta meta, // Read-only metadata (_META) - always null for create/update
197202
String name,
198203
String email
199204
) implements SbxEntity {}
@@ -202,8 +207,9 @@ public record Contact(
202207
The `@SbxModel` annotation automatically:
203208
- Sets the SBX model name for queries
204209
- Maps `key``_KEY` in JSON
205-
- Maps `meta``_META` in JSON
210+
- Maps `meta``_META` in JSON (read-only, stripped on create/update)
206211
- Ignores unknown JSON properties
212+
- Strips null values (enables partial updates)
207213

208214
### With Convenience Constructor
209215

src/main/java/com/sbxcloud/sbx/client/SBXService.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -839,9 +839,9 @@ private <T> SBXResponse<T> upsert(String endpoint, String model, List<Map<String
839839
}
840840

841841
try {
842-
// Clean _META from rows before sending
842+
// Clean rows for upsert (remove meta, nulls)
843843
List<Map<String, Object>> cleanedRows = rows.stream()
844-
.map(this::cleanMeta)
844+
.map(this::cleanForUpsert)
845845
.toList();
846846

847847
List<String> allKeys = new ArrayList<>();
@@ -870,9 +870,29 @@ private <T> SBXResponse<T> upsert(String endpoint, String model, List<Map<String
870870
}
871871
}
872872

873-
private Map<String, Object> cleanMeta(Map<String, Object> row) {
874-
var cleaned = new LinkedHashMap<>(row);
875-
cleaned.remove("_META");
873+
/**
874+
* Cleans a row for create/update:
875+
* - Removes _META/meta (read-only)
876+
* - Removes null values (allows partial updates)
877+
*/
878+
private Map<String, Object> cleanForUpsert(Map<String, Object> row) {
879+
var cleaned = new LinkedHashMap<String, Object>();
880+
for (var entry : row.entrySet()) {
881+
String key = entry.getKey();
882+
Object value = entry.getValue();
883+
884+
// Skip meta fields (read-only)
885+
if ("_META".equals(key) || "meta".equals(key)) {
886+
continue;
887+
}
888+
889+
// Skip null values (allows partial updates)
890+
if (value == null) {
891+
continue;
892+
}
893+
894+
cleaned.put(key, value);
895+
}
876896
return cleaned;
877897
}
878898

0 commit comments

Comments
 (0)