Skip to content

Commit bc06b41

Browse files
cubapthehabes
andauthored
Document /overwrite 409 (#200)
* overwrite with version - adding version match for overwrite - testing locking - generated tests (not implemented) * AI loves CJS * Update overwrite-optimistic-locking.test.js * Let Claude have a try * surely you jest * ain't nobody got time for that * Update db-controller.txt * send this immediately The expressError handling was turning this into a text error with no payload, just a message * send object back * why doctype error? * experimenting with errors * add message * `message` is a bad key here * notes don't work either * Update API.html --------- Co-authored-by: Bryan Haberberger <bryan.j.haberberger@slu.edu>
1 parent de07b3f commit bc06b41

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

db-controller.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,6 +982,9 @@ const id = async function (req, res, next) {
982982
res.set("Cache-Control", "max-age=86400, must-revalidate")
983983
//Support requests with 'If-Modified_Since' headers
984984
res.set(utils.configureLastModifiedHeader(match))
985+
// Include current version for optimistic locking
986+
const currentVersion = match.__rerum?.isOverwritten ?? ""
987+
res.set('Current-Overwritten-Version', currentVersion)
985988
match = idNegotiation(match)
986989
res.location(_contextid(match["@context"]) ? match.id : match["@id"])
987990
res.json(match)

public/API.html

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -809,13 +809,19 @@ <h3 id="overwrite">Overwrite</h3>
809809

810810
<p>
811811
RERUM allows the Generator of a record to overwrite that record. An error will be returned if the agent encoded in the request "Authorization" access token does not match the <code __rerum.generatedBy></code> agent of the existing record. </span>
812-
813812
Replace a record using a reference to its internal RERUM id and receive the <code class="language-plaintext highlighter-rouge">Location</code> URI for the resulting record as a response header and the complete record as the response body.
814-
815813
This will have the effects of update, set, and unset actions. New keys will be created and keys not present in the request will not be present in the resulting record.
816-
817814
The record is replaced in place, or overwritten, and so the <code class="language-plaintext highlighter-rouge">@id</code> will not change and the history connected with this record will not be altered. The <code>__rerum.isOverwritten</code> property will be set to the date and time of the overwrite.
818815
</p>
816+
<p>
817+
This endpoint supports optimistic locking, if requested. Including the "If-Overwritten-Version" header with the
818+
value of the <code class="language-plaintext highlighter-rouge">__rerum.isOverwritten</code> property of the
819+
record will cause the request to fail if the record has been overwritten since that time.
820+
This is useful for preventing overwriting a record that has been changed by another agent, colliding within an
821+
application that rapidly requests overwrites of the same object, or if an object is held in memory or cache for a
822+
long time prior to the overwrite. Omitting the "If-Overwritten-Version" header or <code class="language-plaintext highlighter-rouge">__rerum.isOverwritten</code>
823+
property will cause the request to succeed regardless.
824+
</p>
819825

820826
<table>
821827
<thead>
@@ -865,6 +871,12 @@ <h3 id="overwrite">Overwrite</h3>
865871
</code></pre>
866872
</p>
867873

874+
<p class="alert">
875+
In the case of an optimistic locking failure, the response will be a 409 Conflict with a
876+
body containing the current version of the document, in case you would like to automate a
877+
retry of the overwrite request.
878+
</p>
879+
868880
<p>
869881
<div class="exHeading">Here is what the response <code>resp</code> looks like:</div>
870882
<pre><code class="respExample">
@@ -878,12 +890,11 @@ <h3 id="overwrite">Overwrite</h3>
878890
<span>}</span>
879891
</code></pre>
880892
<p class="alert">
881-
Note that resp.headers.get("location") will return "https://devstore.rerum.io/v1/id/abcdef1234567890"
893+
Note: resp.headers.get("location") will return "https://devstore.rerum.io/v1/id/abcdef1234567890"
882894
</p>
883895
<p class="alert">
884-
Note that the <code>@id</code> and <code>__rerum.history</code> properties of the original record "https://devstore.rerum.io/v1/id/abcdef1234567890" have not changed. This is the difference between /update and /overwrite. Be careful.
896+
Note: the <code>@id</code> and <code>__rerum.history</code> properties of the original record "https://devstore.rerum.io/v1/id/abcdef1234567890" have not changed. This is the difference between /update and /overwrite. Be careful.
885897
</p>
886-
887898
</p>
888899

889900
<!--

0 commit comments

Comments
 (0)