feat(db): retry on document conflicts

This commit is contained in:
William Valentin
2025-09-23 11:25:28 -07:00
parent f9ccb50222
commit e3a924c0c6

View File

@@ -187,18 +187,46 @@ export class ProductionDatabaseStrategy implements DatabaseStrategy {
private async putDoc<T extends CouchDBDocument>(
dbName: string,
doc: T
doc: T,
retries = 2
): Promise<T> {
const response = await this.makeRequest<{ id: string; rev: string }>(
'PUT',
`/${dbName}/${doc._id}`,
doc
);
try {
const response = await this.makeRequest<{ id: string; rev: string }>(
'PUT',
`/${dbName}/${doc._id}`,
doc
);
return {
...doc,
_rev: response.rev,
};
return {
...doc,
_rev: response.rev,
};
} catch (error) {
if (
error instanceof DatabaseError &&
error.status === 409 &&
retries > 0 &&
doc._rev
) {
logger.db.warn('Document conflict detected, retrying update', {
dbName,
id: doc._id,
retriesRemaining: retries,
});
const latest = await this.getDoc<T>(dbName, doc._id);
if (latest) {
const mergedDoc = {
...latest,
...doc,
_rev: latest._rev,
};
return this.putDoc(dbName, mergedDoc, retries - 1);
}
}
throw error;
}
}
private async deleteDoc(