Skip to content

Commit 161d8b5

Browse files
authored
Merge pull request #670 from share/change-docs
📝 Add documentation about compering old and new version of snapshot
2 parents dcef549 + 1ab5a28 commit 161d8b5

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

docs/middleware/op-submission.md

+23
Original file line numberDiff line numberDiff line change
@@ -157,3 +157,26 @@ backend.use('apply', (request, next) => {
157157

158158
{: .warn :}
159159
The `request.$fixup()` method may throw an error, which should be handled appropriately, usually by passing directly to the `next()` callback.
160+
161+
## Comparing old snapshot version with new version
162+
163+
Frequently, it becomes necessary to verify the changes made. This can be accomplished by leveraging two hooks, `apply` and `commit`, and creating a snapshot clone within the `apply` hook.
164+
165+
```js
166+
// Alternatively use your favourite deep-clone library
167+
function deepClone(obj) {
168+
return JSON.parse(JSON.stringify(obj));
169+
}
170+
171+
backend.use('apply', (request, next) => {
172+
request.snapshotBeforeApply = deepClone(request.snapshot);
173+
next(error);
174+
});
175+
176+
backend.use('commit', (request, next) => {
177+
// Snapshot without ops and $fixupOps applied is now available as request.snapshotBeforeApply
178+
// Snapshot with ops and $fixupOps applied is still available as request.snapshot
179+
console.log(request.snapshotBeforeApply)
180+
next(error);
181+
});
182+
```

0 commit comments

Comments
 (0)