Mentions légales du service

Skip to content
Snippets Groups Projects

Handle concurrent updates errors

Merged Ludovic Le Frioux requested to merge 44-handle-concurrent-updates-errors into dev
1 file
+ 23
5
Compare changes
  • Side-by-side
  • Inline
+ 23
5
@@ -232,7 +232,11 @@ function deleteApp(dbName: string) {
@@ -232,7 +232,11 @@ function deleteApp(dbName: string) {
* @param docName the targeted document name
* @param docName the targeted document name
* @param document the document new content
* @param document the document new content
*/
*/
function updateObject(dbName: string, docName: string, document: string) {
function updateObject(
 
dbName: string,
 
docName: string,
 
document: string
 
): Promise<string> {
console.warn(
console.warn(
`[SERVER] Updating document '${docName}' in database '${dbName}'`
`[SERVER] Updating document '${docName}' in database '${dbName}'`
);
);
@@ -252,8 +256,15 @@ function updateObject(dbName: string, docName: string, document: string) {
@@ -252,8 +256,15 @@ function updateObject(dbName: string, docName: string, document: string) {
bodyCRDT.merge(CRDT);
bodyCRDT.merge(CRDT);
const newDocument = JSON.parse(bodyCRDT.toJson());
const newDocument = JSON.parse(bodyCRDT.toJson());
newDocument._rev = body._rev;
newDocument._rev = body._rev;
client.db.use(dbName).insert(newDocument, docName);
return client.db
return "OK";
.use(dbName)
 
.insert(newDocument, docName)
 
.then((body) => {
 
return "OK";
 
})
 
.catch((error) => {
 
return updateObject(dbName, docName, document);
 
});
} catch (error) {
} catch (error) {
console.error(
console.error(
`[SERVER][ERROR] Failed updating document '${docName}' in database '${dbName}'`
`[SERVER][ERROR] Failed updating document '${docName}' in database '${dbName}'`
@@ -265,8 +276,15 @@ function updateObject(dbName: string, docName: string, document: string) {
@@ -265,8 +276,15 @@ function updateObject(dbName: string, docName: string, document: string) {
.catch((error) => {
.catch((error) => {
try {
try {
const CRDT = crdtlib.crdt.DeltaCRDT.Companion.fromJson(document);
const CRDT = crdtlib.crdt.DeltaCRDT.Companion.fromJson(document);
client.db.use(dbName).insert(JSON.parse(document), docName);
return client.db
return "OK";
.use(dbName)
 
.insert(JSON.parse(document), docName)
 
.then((body) => {
 
return "OK";
 
})
 
.catch((error) => {
 
return updateObject(dbName, docName, document);
 
});
} catch (error) {
} catch (error) {
console.error(
console.error(
`[SERVER][ERROR] Failed updating document '${docName}' in database '${dbName}'`
`[SERVER][ERROR] Failed updating document '${docName}' in database '${dbName}'`
Loading