Skip to content

Commit

Permalink
FIX RxQuery.doesDocumentDataMatch() was wrong with $and-queries
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Jan 6, 2020
1 parent f78ede7 commit 88065cf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Other:

Bugfixes:
- Updates to documents fail with GraphQL replication. [#1812](https://github.com/pubkey/rxdb/issues/1812). Thanks [@gautambt](https://github.com/gautambt)
- `RxQuery.doesDocumentDataMatch()` was wrong on queries with `$and` which lead to a wrong result with QueryChangeDetection

### 8.7.4 (2 December 2019)

Expand Down
28 changes: 18 additions & 10 deletions src/rx-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from 'rxjs/operators';
import {
massageSelector,
rowFilter
filterInMemoryFields
} from 'pouchdb-selector-core';
import {
MQuery,
Expand Down Expand Up @@ -344,18 +344,26 @@ export class RxQueryBase<RxDocumentType = any, RxQueryResult = RxDocumentType[]
doesDocumentDataMatch(docData: RxDocumentType | any): boolean {
// if doc is deleted, it cannot match
if (docData._deleted) return false;

const selector = this.mquery._conditions;

docData = this.collection.schema.swapPrimaryToId(docData);
const inMemoryFields = Object.keys(selector);

const matches = rowFilter(
docData,
this.massageSelector,
inMemoryFields
// return matchesSelector(docData, selector);

/**
* the following is equal to the implementation of pouchdb
* we do not use matchesSelector() directly so we can cache the
* result of massageSelector
* @link https://github.com/pouchdb/pouchdb/blob/master/packages/node_modules/pouchdb-selector-core/src/matches-selector.js
*/
const selector = this.massageSelector;
const row = {
doc: docData
};
const rowsMatched = filterInMemoryFields(
[row],
{ selector: selector },
Object.keys(selector)
);
return matches;
return rowsMatched && rowsMatched.length === 1;
}

/**
Expand Down
22 changes: 21 additions & 1 deletion test/unit/rx-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
isRxQuery,
create as createRxDatabase
} from '../../';
import * as RxDatabase from '../../dist/lib/rx-database';
import * as humansCollection from './../helper/humans-collection';
import * as schemaObjects from '../helper/schema-objects';
import * as schemas from './../helper/schemas';
Expand Down Expand Up @@ -368,6 +367,27 @@ config.parallel('rx-query.test.js', () => {
assert.strictEqual(true, q.doesDocumentDataMatch(docData));
col.database.destroy();
});
it('BUG should not match regex', async () => {
const col = await humansCollection.create(0);
const q = col.find({
$and: [{
color: {
$regex: new RegExp('f', 'i')
}
}]
});

const docData = {
color: 'green',
hp: 100,
maxHP: 767,
name: 'asdfsadf',
_rev: '1-971bfd0b8749eb33b6aae7f6c0dc2cd4'
};

assert.strictEqual(false, q.doesDocumentDataMatch(docData));
col.database.destroy();
});
});
describe('.exec()', () => {
it('reusing exec should not make a execOverDatabase', async () => {
Expand Down

0 comments on commit 88065cf

Please sign in to comment.