Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cozy-pouch-link/src/CozyPouchLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ class PouchLink extends CozyLink {
* Emits an event (pouchlink:sync:end) when the sync (all doctypes) is done
*/
handleOnSync(doctypeUpdates) {
// FIXME
const normalizedData = mapValues(doctypeUpdates, normalizeAll(this.client))
if (this.client) {
this.client.setData(normalizedData)
Expand Down
60 changes: 34 additions & 26 deletions packages/cozy-pouch-link/src/jsonapi.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,40 @@
import { generateWebLink } from 'cozy-client'

const normalizeDocs = (docs, doctype, client) => {
for (let i = docs.length; i >= 0; i--) {
const doc = docs[i]
if (!doc) {
docs.splice(i, 1)
continue
}
normalizeDoc(doc, doctype, client)
}
}

export const normalizeDoc = (doc, doctype, client) => {
const id = doc._id || doc.id

const { relationships, referenced_by } = doc

// PouchDB sends back .rev attribute but we do not want to
// keep it on the server. It is potentially higher than the
// _rev.
const _rev = doc.rev || doc._rev
const normalizedDoc = {
...doc,
id,
_id: id,
_rev,
_type: doctype,
relationships: {
...relationships,
referenced_by
doc.id = id
doc._id = id
doc._rev = _rev
doc._type = doctype

if (doc.relationships) {
doc.relationships.referenced_by = doc.referenced_by
} else {
doc.relationships = {
referenced_by: doc.referenced_by
}
}
if (normalizedDoc.rev) {
delete normalizedDoc.rev
if (doc.rev) {
delete doc.rev
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting to undefined is way more performant than calling delet(https://jsperf.app/delete-vs-undefined-vs-null/10 I don't like the void 0 stuff)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know! I did the change in another PR: 2b89a63

}

normalizeAppsLinks(normalizedDoc, doctype, client)
if (doctype === 'io.cozy.apps') {
normalizeAppsLinks(doc, doctype, client)
}

return normalizedDoc
return doc
}

const normalizeAppsLinks = (docRef, doctype, client) => {
Expand All @@ -50,8 +58,6 @@ const normalizeAppsLinks = (docRef, doctype, client) => {
}
}

const filterDeletedDocumentsFromRows = doc => !!doc

export const fromPouchResult = ({ res, withRows, doctype, client }) => {
// Sometimes, queries are transformed by Collections and they call a dedicated
// cozy-stack route. When this is the case, we want to be able to replicate the same
Expand All @@ -68,21 +74,23 @@ export const fromPouchResult = ({ res, withRows, doctype, client }) => {
}

if (withRows) {
const docs = res.rows
? res.rows.map(row => row.doc).filter(filterDeletedDocumentsFromRows)
: res.docs
const docs = res.rows ? res.rows.map(row => row.doc) : res.docs
const offset = res.offset || 0
normalizeDocs(docs, doctype)

return {
data: docs.map(doc => normalizeDoc(doc, doctype, client)),
const result = {
data: docs,
meta: { count: docs.length },
skip: offset,
next: offset + docs.length < res.total_rows || docs.length >= res.limit
}
return result
} else {
return {
data: Array.isArray(res)
// FIXME
? res.map(doc => normalizeDoc(doc, doctype, client))
// FIXME
: normalizeDoc(res, doctype, client)
}
}
Expand Down