-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add compensating write error information
- Loading branch information
Showing
9 changed files
with
537 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
source/examples/generated/cpp/sync-errors.snippet.compensating-write-example.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// The complexity of this item is `7`. This is outside the bounds | ||
// of the subscription query, which triggers a compensating write. | ||
auto complexItem = Item { | ||
._id = primaryKey, | ||
.ownerId = user.identifier(), | ||
.itemName = "Test compensating writes", | ||
.complexity = 7 | ||
}; | ||
|
||
// This should trigger a compensating write error when it tries to sync | ||
// due to server-side permissions, which gets logged with the error handler. | ||
syncRealm.write([&] { | ||
syncRealm.add(std::move(complexItem)); | ||
}); |
7 changes: 7 additions & 0 deletions
7
source/examples/generated/cpp/sync-errors.snippet.compensating-write-model.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
struct Item { | ||
primary_key<realm::object_id> _id{realm::object_id::generate()}; | ||
std::string ownerId; | ||
std::string itemName; | ||
int64_t complexity; | ||
}; | ||
REALM_SCHEMA(Item, _id, ownerId, itemName, complexity) |
15 changes: 15 additions & 0 deletions
15
source/examples/generated/cpp/sync-errors.snippet.compensating-write-setup.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
auto appConfig = realm::App::configuration(); | ||
appConfig.app_id = APP_ID; | ||
auto app = realm::App(appConfig); | ||
auto user = app.login(realm::App::credentials::anonymous()).get(); | ||
auto dbConfig = user.flexible_sync_configuration(); | ||
auto syncRealm = realm::experimental::db(dbConfig); | ||
// Add subscription | ||
auto subscriptionUpdateSuccess = syncRealm.subscriptions().update([](realm::mutable_sync_subscription_set &subs) { | ||
// Get Items from Atlas that match this query. | ||
// Uses the queryable field `complexity`. | ||
// Sync Item objects with complexity less than or equal to 4. | ||
subs.add<Item>("simple items", [](auto &obj) { | ||
return obj.complexity <= 4; | ||
}); | ||
}).get(); |
6 changes: 6 additions & 0 deletions
6
source/examples/generated/cpp/sync-errors.snippet.get-compensating-write-error-info.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
auto info = receivedSyncError.compensating_writes_info(); | ||
for (auto& v : info) { | ||
std::cout << "A write was rejected with a compensating write error.\n"; | ||
std::cout << "An object of type " << v.object_name << "\n"; | ||
std::cout << "was rejected because " << v.reason << ".\n"; | ||
} |
14 changes: 14 additions & 0 deletions
14
source/examples/generated/cpp/sync-errors.snippet.successful-write-example.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Per the Device Sync permissions, users can only read and write data | ||
// where the `Item.ownerId` property matches their own user ID. | ||
auto simpleItem = Item { | ||
.ownerId = user.identifier(), | ||
.itemName = "This item meets sync criteria", | ||
.complexity = 3 | ||
}; | ||
|
||
// `simpleItem` successfully writes to the realm and syncs to Atlas | ||
// because its data matches the subscription query (complexity <= 4) | ||
// and its `ownerId` field matches the user ID. | ||
syncRealm.write([&] { | ||
syncRealm.add(std::move(simpleItem)); | ||
}); |
12 changes: 12 additions & 0 deletions
12
source/examples/generated/cpp/sync-errors.snippet.write-not-matching-permissions.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// The `ownerId` of this item does not match the user ID of the logged-in | ||
// user. The user does not have permissions to make this write, which | ||
// triggers a compensating write. | ||
auto itemWithWrongOwner = Item { | ||
.ownerId = "not the current user", | ||
.itemName = "Trigger an incorrect permissions compensating write", | ||
.complexity = 1 | ||
}; | ||
|
||
syncRealm.write([&] { | ||
syncRealm.add(std::move(itemWithWrongOwner)); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.