Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix in prepareQuery for pouchdb #2213

Merged
merged 2 commits into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 4 additions & 2 deletions src/rx-storage-pouchdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,16 @@ export class RxStoragePouchDbClass implements RxStorage<PouchDBInstance> {
const query = mutateableQuery;

/**
* because sort wont work on unused keys we have to workarround
* because sort wont work on unused keys we have to workaround
* so we add the key to the selector if necessary
* @link https://github.com/nolanlawson/pouchdb-find/issues/204
*/
if (query.sort) {
query.sort.forEach(sortPart => {
const key = Object.keys(sortPart)[0];
if (!query.selector[key] || !query.selector[key].$gt) {
const comparisonOperators = ['$gt', '$gte', '$lt', '$lte'];
const keyUsed = query.selector[key] && Object.keys(query.selector[key]).some(op => comparisonOperators.includes(op)) || false;
if (!keyUsed) {
const schemaObj = rxQuery.collection.schema.getSchemaByObjectPath(key);
if (!schemaObj) {
throw newRxError('QU5', {
Expand Down
25 changes: 20 additions & 5 deletions test/unit/bug-report.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ describe('bug-report.test.js', () => {
minimum: 0,
maximum: 150
}
}
},
indexes: ['age'],
};

// generate a random database-name
Expand All @@ -64,6 +65,13 @@ describe('bug-report.test.js', () => {
age: 56
});

await collection.insert({
passportId: 'foobar2',
firstName: 'Bob2',
lastName: 'Kelso2',
age: 58
});

/**
* to simulate the event-propagation over multiple browser-tabs,
* we create the same database again
Expand All @@ -82,16 +90,23 @@ describe('bug-report.test.js', () => {

// find the document in the other tab
const myDocument = await collectionInOtherTab
.findOne()
.where('firstName')
.eq('Bob')
.findOne({
selector: {
age: {
$gte: 57,
},
},
sort: [{ age: 'asc' }]
})
.exec();

console.log(myDocument);

/*
* assert things,
* here your tests should fail to show that there is a bug
*/
assert.strictEqual(myDocument.age, 56);
assert.strictEqual(myDocument.age, 58);

// you can also wait for events
const emitted = [];
Expand Down