Skip to content

Commit

Permalink
FIX #146 also throw on normal key
Browse files Browse the repository at this point in the history
  • Loading branch information
pubkey committed Jun 4, 2017
1 parent d515650 commit 85ba78a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/RxQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,19 @@ class RxQuery {
* @link https://github.com/nolanlawson/pouchdb-find/issues/204
*/
sort(params) {
const throwNotInSchema = (key) => {
throw new Error(`RxQuery.sort(${key}) does not work because ${key} is not defined in the schema`);
};
const clonedThis = this._clone();

// workarround because sort wont work on unused keys
if (typeof params !== 'object') {
const checkParam = params.charAt(0) == '-' ? params.substring(1) : params;
if (!clonedThis.mquery._conditions[checkParam]) {
const schemaObj = clonedThis.collection.schema.getSchemaByObjectPath(checkParam);
if (schemaObj && schemaObj.type == 'integer')
if (!schemaObj) throwNotInSchema(checkParam);

if (schemaObj.type == 'integer')
// TODO change back to -Infinity when issue resolved
// @link https://github.com/pouchdb/pouchdb/issues/6454
clonedThis.mquery.where(checkParam).gt(-9999999999999999999999999999); // -Infinity does not work since pouchdb 6.2.0
Expand All @@ -349,8 +354,7 @@ class RxQuery {
.filter(k => !clonedThis.mquery._conditions[k] || !clonedThis.mquery._conditions[k].$gt)
.forEach(k => {
const schemaObj = clonedThis.collection.schema.getSchemaByObjectPath(k);
if (!schemaObj)
throw new Error(`RxQuery.sort(${k}) does not work because ${k} is not defined in the schema`);
if (!schemaObj) throwNotInSchema(k);

if (schemaObj.type == 'integer')
// TODO change back to -Infinity when issue resolved
Expand Down
5 changes: 5 additions & 0 deletions test/helper/schemas.js
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,11 @@ export const primaryHuman = {
},
lastName: {
type: 'string'
},
age: {
type: 'integer',
minimum: 0,
maximum: 150
}
},
required: ['firstName', 'lastName']
Expand Down
2 changes: 1 addition & 1 deletion test/unit/QueryChangeDetector.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe('QueryChangeDetector.test.js', () => {
});
it('BUG: this should match', async() => {
const col = await humansCollection.create(0);
const q = col.find().sort('name');
const q = col.find();

const docData = {
color: 'green',
Expand Down
13 changes: 11 additions & 2 deletions test/unit/RxCollection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,14 +635,23 @@ describe('RxCollection.test.js', () => {
);
c.database.destroy();
});
it('#146 throw when field not in schema', async() => {
it('#146 throw when field not in schema (object)', async() => {
const c = await humansCollection.createAgeIndex();
await util.assertThrowsAsync(
() => c.find().sort({
foobar: 'desc'
}).exec(),
Error,
'foobar'
'not defined in the schema'
);
c.database.destroy();
});
it('#146 throw when field not in schema (string)', async() => {
const c = await humansCollection.createAgeIndex();
await util.assertThrowsAsync(
() => c.find().sort('foobar').exec(),
Error,
'not defined in the schema'
);
c.database.destroy();
});
Expand Down

0 comments on commit 85ba78a

Please sign in to comment.