Skip to content

Commit

Permalink
feat: Add check to _rev attribute when persisting virtual documents
Browse files Browse the repository at this point in the history
In previous commit we added a check to `meta.rev` attribute in order to
discriminate documents that exist in remote CouchDB from the ones that
don't

This approach would work only for documents handled by the cozy-stack
using JSON-API

But some CouchDB documents are served directly through the `data` route
and are not wrapped into JSON-API

When this is the case, they have a `_rev` attribute instead of a
`meta.rev` one

So we want to check for both forms before persisting virtual documents
  • Loading branch information
Ldoppea committed Sep 9, 2024
1 parent 8ac08a9 commit 8cf4478
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
12 changes: 6 additions & 6 deletions packages/cozy-client/src/CozyClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -1142,15 +1142,15 @@ client.query(Q('io.cozy.bills'))`)
*
* @private
* @param {CozyClientDocument} document - Document to be saved
* @param {boolean} enforce - When true, save the document even if `meta.rev` exists
* @param {boolean} enforce - When true, save the document even if `meta.rev` or `_rev` exist
* @returns {Promise<void>}
*/
async persistVirtualDocument(document, enforce) {
if (
((document && !document.meta?.rev) || enforce) &&
!document.cozyLocalOnly &&
!document.cozyFromPouch
) {
if (!document || document.cozyLocalOnly || document.cozyFromPouch) {
return
}

if ((!document.meta?.rev && !document._rev) || enforce) {
await this.chain.persistData(document)
}
}
Expand Down
34 changes: 32 additions & 2 deletions packages/cozy-client/src/CozyClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,7 @@ describe('CozyClient', () => {
expect(client.requestQuery).toHaveBeenCalledTimes(1)
})

it('should persist virtual document when no meta.rev', async () => {
it('should persist virtual document when no meta.rev nor _rev', async () => {
jest.spyOn(client, 'requestQuery')
requestHandler.mockResolvedValue({
data: {
Expand All @@ -1411,7 +1411,37 @@ describe('CozyClient', () => {
)
})

it('should persist array of virtual documents when no meta.rev', async () => {
it('should not persist virtual document when meta.rev', async () => {
jest.spyOn(client, 'requestQuery')
requestHandler.mockResolvedValue({
data: {
_id: 'some_id',
meta: {
rev: 'SOME_REV'
}
}
})

await client.query(query, { as: 'allTodos' })

expect(persistHandler).not.toHaveBeenCalled()
})

it('should not persist virtual document when _rev', async () => {
jest.spyOn(client, 'requestQuery')
requestHandler.mockResolvedValue({
data: {
_id: 'some_id',
_rev: 'SOME_REV'
}
})

await client.query(query, { as: 'allTodos' })

expect(persistHandler).not.toHaveBeenCalled()
})

it('should persist array of virtual documents when no meta.rev nor _rev', async () => {
jest.spyOn(client, 'requestQuery')
requestHandler.mockResolvedValue({
data: [
Expand Down

0 comments on commit 8cf4478

Please sign in to comment.