Hi,
We have a requirement to create a new version of an existing record(couchbase document) everytime the record is updated.
The approach to be taken is:
- Update the doc and lock it using a JSON field(lock, in this example) and if the update is successful, return the document.
- If the record is retrieved and locked, the user can make further changes to the record and save it.
- On save we will match the CAS. and if it matches then the save is successful else not.
To achieve this we need to return the CAS value on the first operation itself, so as to be able to match it with the CAS during the third operation.
For the first operation we’re using N1ql query.
Questions:
- Is there a way to return CAS with N1ql query?
- If yes, then is there a difference in the CAS returned through N1ql and a sub-document API?
Request you to please help us out with this.
Sample Doc
Example:
Existing Document-
Record:1:OldTimeStamp
{
“id”:1,
“lock”:false,
“name”: “first name”,
“latestVersion”:true
}
On Step 1: updating the lock to “false”:
“Update BucketName record set lock = true where record.id=“1” AND record.lock = false AND record.latestVersion = true returning RAW record”
Record:1:OldTimeStamp
{
“id”:1,
“lock”:true,
“name”: “first name”,
“latestVersion”:true
}
On Step 3: Saving further changes to the record
Record:1:OldTimeStamp
{
“id”:1,
“lock”:false,
“name”: “first name”,
“latestVersion”:false
}
Record:1:NewTimeStamp
{
“id”:1,
“lock”:true,
“name”: “first name”,
“latestVersion”:true
}