Create compound index with primary key and sort by its items #2456
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR contains:
A bugfix concerning compound indexes
Describe the problem you have without this PR
I tried to create a compound index (age + id) that contains the primary key (id) of the schema and sort docs by the attributes of the compound index (see test). This fails with the error message "cannot sort on field(s) 'age' when using the default index". Why is the default index ('_id)' used here, although an appropriate custom index has been defined?
The first problem was that in RxStoragePouchDbClass - prepareQuery() the primary key is changed to '_id' and therefore the new custom index does not match the attribute anymore:
rxdb/src/rx-storage-pouchdb.ts
Line 221 in 8b954b4
Therefore in my opinion the method createIndex() of RxCollection has to be adapted and the change of the primary key has to be done as well.
Second, the order of the attributes in sort() is changed. This has proven to be a problem with the util sortObject(). When the method is called recursively, the param noArraySort is currently not passed to further calls, which results in nested arrays being sorted. The problem occurred in the method toString() of RxQuery, so the custom index was not found. Additionally, the test
typeof obj === 'object'
in the method is not sufficient to exclude an array. Therefore I have added!Array.isArray(obj)
here.Hopefully I explained the problem sufficiently :)
Todos