From ae8dfd05751c0794c68b16e2a3efa52c32f0efa8 Mon Sep 17 00:00:00 2001 From: Kevin Burke Date: Tue, 29 Mar 2016 08:39:41 -0700 Subject: [PATCH] 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 https://github.com/Shyp/waterline-sequel/pull/6. Fixes #86. --- Makefile | 7 ++++++- sequel/lib/criteriaProcessor.js | 5 +++++ test/unit/index.test.js | 10 +++++++++- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index b2a610a..4784f5e 100644 --- a/Makefile +++ b/Makefile @@ -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 \ No newline at end of file + @NODE_ENV=test node test/integration/runnerDispatcher.js diff --git a/sequel/lib/criteriaProcessor.js b/sequel/lib/criteriaProcessor.js index be2cb51..2fcffdc 100644 --- a/sequel/lib/criteriaProcessor.js +++ b/sequel/lib/criteriaProcessor.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 diff --git a/test/unit/index.test.js b/test/unit/index.test.js index dce209d..c16a168 100644 --- a/test/unit/index.test.js +++ b/test/unit/index.test.js @@ -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(); }); }); - }); + }); });