Skip to content

Commit

Permalink
fix(cozy-pouch-link): Merge partialFilter into selector on find query
Browse files Browse the repository at this point in the history
For some reason the Pouch engine would ignore any partialFilter if it
is not included in the selector

This may be the same reason we had to do this fix:
7c69838#diff-41848dd46551544674c134f359a5d7cddea46dd1e47c21da6814e1d1d585173dR482-R489
  • Loading branch information
Ldoppea committed Sep 19, 2024
1 parent d3876a3 commit 6871ce1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/cozy-pouch-link/src/CozyPouchLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,8 @@ class PouchLink extends CozyLink {
const findSelector = helpers.normalizeFindSelector({
selector,
sort,
indexedFields
indexedFields,
partialFilter
})

const findOpts = {
Expand Down
16 changes: 13 additions & 3 deletions packages/cozy-pouch-link/src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import merge from 'lodash/merge'
import startsWith from 'lodash/startsWith'

import logger from './logger'
Expand Down Expand Up @@ -63,7 +64,12 @@ helpers.insertBulkDocs = async (db, docs) => {
return db.bulkDocs(docs, { new_edits: false })
}

helpers.normalizeFindSelector = ({ selector, sort, indexedFields }) => {
helpers.normalizeFindSelector = ({
selector,
sort,
indexedFields,
partialFilter
}) => {
let findSelector = selector || {}
if (indexedFields) {
for (const indexedField of indexedFields) {
Expand Down Expand Up @@ -94,8 +100,12 @@ helpers.normalizeFindSelector = ({ selector, sort, indexedFields }) => {
}
}

return Object.keys(findSelector).length > 0
? findSelector
const mergedSelector = partialFilter
? merge({ ...findSelector }, partialFilter)
: findSelector

return Object.keys(mergedSelector).length > 0
? mergedSelector
: { _id: { $gt: null } } // PouchDB does not accept empty selector
}

Expand Down

0 comments on commit 6871ce1

Please sign in to comment.