Skip to content

Commit b72f3eb

Browse files
committed
fix: Remove attributes and meta attributes before persisting docs
Those doc's attributes are specific to the JSON API and should not be inserted into the Pouch database This implies that we will have a difference on documents regarding if they are served through the cozy-stack or through a local PouchDB, the first one may include those fields in their result, but not the second one So from now we should avoid, as much as possible, to relies on the `attributes` member to prevent bugs on Offline mode. Multiple commits to fix usages of `attributes` in cozy-client will be done after this one Usage of `attributes` and `meta` members on cozy-app will also have to be fixed. This will be a requirement to implement Offline mode on cozy-apps This replies to #1486 (comment)
1 parent 4f3e14e commit b72f3eb

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

packages/cozy-pouch-link/src/CozyPouchLink.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -391,17 +391,44 @@ class PouchLink extends CozyLink {
391391
}
392392
}
393393

394-
async persistData(data, forward = doNothing) {
394+
sanitizeJsonApi(data) {
395395
const docWithoutType = sanitized(data)
396-
docWithoutType.cozyLocalOnly = true
396+
397+
/*
398+
We persist in the local Pouch database all the documents that do not
399+
exist on the remote Couch database
400+
401+
Those documents are computed by the cozy-stack then are sent to the
402+
client using JSON-API format containing `attributes` and `meta`
403+
attributes
404+
405+
Then the cozy-client would normalize those documents by spreading
406+
`attributes` and `meta` content into the document's root
407+
408+
So we don't need to store `attributes` and `meta` data into the Pouch
409+
database as their data already exists in the document's root
410+
411+
Note that this is also the case for `links` and `relationships`
412+
attributes, but we don't remove them for now. They are also part of the
413+
JSON-API, but the normalization do not spread them in the document's
414+
root, so we have to check their usefulnes first
415+
*/
416+
const sanitizedDoc = omit(docWithoutType, ['attributes', 'meta'])
417+
418+
return sanitizedDoc
419+
}
420+
421+
async persistCozyData(data, forward = doNothing) {
422+
const sanitizedDoc = this.sanitizeJsonApi(data)
423+
sanitizedDoc.cozyLocalOnly = true
397424

398425
const oldDoc = await this.getExistingDocument(data._id, data._type)
399426
if (oldDoc) {
400-
docWithoutType._rev = oldDoc._rev
427+
sanitizedDoc._rev = oldDoc._rev
401428
}
402429

403430
const db = this.pouches.getPouch(data._type)
404-
await db.put(docWithoutType)
431+
await db.put(sanitizedDoc)
405432
}
406433

407434
/**

0 commit comments

Comments
 (0)