Skip to content
Permalink
Browse files

Throw an error if the operator is unknown

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
Kevin Burke committed Mar 29, 2016
1 parent 998b9ef commit ae8dfd05751c0794c68b16e2a3efa52c32f0efa8
Showing with 20 additions and 2 deletions.
  1. +6 −1 Makefile
  2. +5 −0 sequel/lib/criteriaProcessor.js
  3. +9 −1 test/unit/index.test.js
@@ -1,5 +1,10 @@
.PHONY: test test-unit install test-integration

ROOT=$(shell pwd)

install:
npm install

test: test-unit

test-unit:
@@ -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
@@ -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
@@ -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.
@@ -99,5 +107,5 @@ describe('Sequel', function () {
done();
});
});
});
});
});

0 comments on commit ae8dfd0

Please sign in to comment.
You can’t perform that action at this time.