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>( private async putDoc<T extends CouchDBDocument>(
dbName: string, dbName: string,
doc: T doc: T,
retries = 2
): Promise<T> { ): Promise<T> {
const response = await this.makeRequest<{ id: string; rev: string }>( try {
'PUT', const response = await this.makeRequest<{ id: string; rev: string }>(
`/${dbName}/${doc._id}`, 'PUT',
doc `/${dbName}/${doc._id}`,
); doc
);
return { return {
...doc, ...doc,
_rev: response.rev, _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( private async deleteDoc(