Skip to content
This repository has been archived by the owner on Aug 13, 2021. It is now read-only.

Commit

Permalink
Throw an error if the operator is unknown
Browse files Browse the repository at this point in the history
Previously passing a query with an unknown operator would place the word
"undefined" in the SQL query:

```javascript
sequel.find('users', { balance: { 'in': [ 1, 2 ] } });
```

```sql
'SELECT "users"."id", "users"."email", "users"."balance", "users"."pickupCount"
FROM "users" AS "users"  "users"."balance" undefined  '
```

(Valid operators are things like 'contains', 'startsWith', 'endsWith', '>'.)

This happens because we define the var `str` to be undefined, never set it, and
then append it to the query string.

Instead, immediately throw an error when an unknown key gets passed to
Waterline, which should help diagnose these problems going forward (instead of
forcing us to parse a Postgres syntax error).

Cherry-picked from Shyp#6. Fixes #86.
  • Loading branch information
Kevin Burke committed Mar 29, 2016
1 parent 998b9ef commit ae8dfd0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
.PHONY: test test-unit install test-integration

ROOT=$(shell pwd)

install:
npm install

test: test-unit

test-unit:
Expand All @@ -12,4 +17,4 @@ test-integration:
ln -s $(ROOT) node_modules/sails-postgresql/node_modules/waterline-sequel
rm -rf node_modules/sails-mysql/node_modules/waterline-sequel
ln -s $(ROOT) node_modules/sails-mysql/node_modules/waterline-sequel
@NODE_ENV=test node test/integration/runnerDispatcher.js
@NODE_ENV=test node test/integration/runnerDispatcher.js
5 changes: 5 additions & 0 deletions sequel/lib/criteriaProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,11 @@ CriteriaProcessor.prototype.prepareCriterion = function prepareCriterion(key, va
}

break;

default:
var err = new Error('Unknown filtering operator: "' + key + "\". Should be 'startsWith', '>', 'contains' or similar");
err.operator = key;
throw err;
}

// Bump paramCount
Expand Down
10 changes: 9 additions & 1 deletion test/unit/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ describe('Sequel', function () {
});
});

describe('queries with an unknown operator', function () {
it('throws an error when the operator is unknown', function() {
var sequel = new Sequel(schema);
assert.throws(sequel.find.bind(sequel, 'bar', { id: { 'in': [ 1, 2 ] } }),
Error, "Unknown filtering operator: \"in\". Should be 'startsWith', '>', 'contains' or similar");
});
});

describe('.find() with schema name', function () {
var _options = _.extend({}, options, {schemaName: {'foo':'myschema','oddity':'anotherschema','bat':'public'}});
// Loop through the query objects and test them against the `.find()` method.
Expand All @@ -99,5 +107,5 @@ describe('Sequel', function () {
done();
});
});
});
});
});

0 comments on commit ae8dfd0

Please sign in to comment.