Skip to content

Commit

Permalink
fix: Remove attributes and meta attributes before persisting docs
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Ldoppea committed Sep 24, 2024
1 parent d4f0adc commit 118a343
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions packages/cozy-pouch-link/src/CozyPouchLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -391,17 +391,44 @@ class PouchLink extends CozyLink {
}
}

async persistData(data, forward = doNothing) {
sanitizeJsonApi(data) {
const docWithoutType = sanitized(data)
docWithoutType.cozyLocalOnly = true

/*
We persist in the local Pouch database all the documents that do not
exist on the remote Couch database
Those documents are computed by the cozy-stack then are sent to the
client using JSON-API format containing `attributes` and `meta`
attributes
Then the cozy-stack-client would normalize those documents by spreading
`attributes` and `meta` content into the document's root
So we don't need to store `attributes` and `meta` data into the Pouch
database as their data already exists in the document's root
Note that this is also the case for `links` and `relationships`
attributes, but we don't remove them for now. They are also part of the
JSON-API, but the normalization do not spread them in the document's
root, so we have to check their usefulnes first
*/
const sanitizedDoc = omit(docWithoutType, ['attributes', 'meta'])

return sanitizedDoc
}

async persistCozyData(data, forward = doNothing) {
const sanitizedDoc = this.sanitizeJsonApi(data)
sanitizedDoc.cozyLocalOnly = true

const oldDoc = await this.getExistingDocument(data._id, data._type)
if (oldDoc) {
docWithoutType._rev = oldDoc._rev
sanitizedDoc._rev = oldDoc._rev
}

const db = this.pouches.getPouch(data._type)
await db.put(docWithoutType)
await db.put(sanitizedDoc)
}

/**
Expand Down

0 comments on commit 118a343

Please sign in to comment.