Skip to content

Commit

Permalink
feat(pouch-link): Apply new index naming algorithm on CozyPouchLink
Browse files Browse the repository at this point in the history
In #1495 we improved index naming for CozyStackClient

This commit applies the same algorithm to CozyPouchLink
  • Loading branch information
Ldoppea committed Sep 9, 2024
1 parent 54d82fa commit 6ee5744
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 13 deletions.
7 changes: 1 addition & 6 deletions packages/cozy-pouch-link/src/CozyPouchLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -509,13 +509,8 @@ class PouchLink extends CozyLink {
if (!indexedFields) {
indexedFields = getIndexFields(options)
}
const partialFilterFields = partialFilter
? getIndexFields({ selector: {}, partialFilter })
: null

const indexName = getIndexNameFromFields(indexedFields, {
partialFilterFields
})
const indexName = getIndexNameFromFields(indexedFields, partialFilter)

const existingIndex = this.findExistingIndex(doctype, options, indexName)
if (!existingIndex) {
Expand Down
61 changes: 54 additions & 7 deletions packages/cozy-pouch-link/src/mango.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,60 @@
import head from 'lodash/head'

export const getIndexNameFromFields = (
fields,
{ partialFilterFields } = {}
) => {
/**
* Process a partial filter to generate a string key
*
* /!\ Warning: this method is similar to cozy-stack-client mangoIndex.makeKeyFromPartialFilter()
* If you edit this method, please check if the change is also needed in mangoIndex file
*
* @param {object} condition - An object representing the partial filter or a sub-condition of the partial filter
* @returns {string} - The string key of the processed partial filter
*/
export const makeKeyFromPartialFilter = condition => {
if (typeof condition !== 'object' || condition === null) {
return String(condition)
}

const conditions = Object.entries(condition).map(([key, value]) => {
if (
Array.isArray(value) &&
value.every(subObj => typeof subObj === 'string')
) {
return `${key}_(${value.join('_')})`
} else if (Array.isArray(value)) {
return `(${value
.map(subCondition => `${makeKeyFromPartialFilter(subCondition)}`)
.join(`)_${key}_(`)})`
} else if (typeof value === 'object') {
return `${key}_${makeKeyFromPartialFilter(value)}`
} else {
return `${key}_${value}`
}
})

return conditions.join(')_and_(')
}

/**
* Name an index, based on its indexed fields and partial filter.
*
* It follows this naming convention:
* `by_{indexed_field1}_and_{indexed_field2}_filter_({partial_filter.key1}_{partial_filter.value1})_and_({partial_filter.key2}_{partial_filter.value2})`
*
* /!\ Warning: this method is similar to cozy-stack-client mangoIndex.getIndexNameFromFields()
* If you edit this method, please check if the change is also needed in mangoIndex file
*
* @param {Array<string>} fields - The indexed fields
* @param {object} [partialFilter] - The partial filter
* @returns {string} The index name, built from the fields
*/
export const getIndexNameFromFields = (fields, partialFilter) => {
const indexName = `by_${fields.join('_and_')}`
return partialFilterFields
? `${indexName}_filter_${partialFilterFields.join('_and_')}`
: indexName

if (partialFilter) {
return `${indexName}_filter_(${makeKeyFromPartialFilter(partialFilter)})`
}

return indexName
}

/**
Expand Down
6 changes: 6 additions & 0 deletions packages/cozy-stack-client/src/mangoIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ export const normalizeDesignDoc = designDoc => {
/**
* Process a partial filter to generate a string key
*
* /!\ Warning: this method is similar to cozy-pouch-link mango.makeKeyFromPartialFilter()
* If you edit this method, please check if the change is also needed in mango file
*
* @param {object} condition - An object representing the partial filter or a sub-condition of the partial filter
* @returns {string} - The string key of the processed partial filter
*/
Expand Down Expand Up @@ -84,6 +87,9 @@ export const makeKeyFromPartialFilter = condition => {
* It follows this naming convention:
* `by_{indexed_field1}_and_{indexed_field2}_filter_({partial_filter.key1}_{partial_filter.value1})_and_({partial_filter.key2}_{partial_filter.value2})`
*
* /!\ Warning: this method is similar to cozy-pouch-link mango.getIndexNameFromFields()
* If you edit this method, please check if the change is also needed in mango file
*
* @param {Array<string>} fields - The indexed fields
* @param {object} [partialFilter] - The partial filter
* @returns {string} The index name, built from the fields
Expand Down

0 comments on commit 6ee5744

Please sign in to comment.