From 5bcaf436f15add1e9c2915b143a26879e712d8c9 Mon Sep 17 00:00:00 2001 From: Dekel Barzilay Date: Fri, 11 Oct 2019 21:17:33 +0300 Subject: [PATCH] - Fixed support and docs for JSON specific query operators - Bumped version to 4.4.2 --- README.md | 4 ++-- package-lock.json | 2 +- package.json | 2 +- src/index.js | 14 ++++++++------ test/index.test.js | 16 ++++++++++++++-- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index a10ece9..ec258bc 100644 --- a/README.md +++ b/README.md @@ -198,9 +198,9 @@ Note that all this eager related options are optional. - **`$all`** (Postgres) - filter based on if a column contains all keys from array of strings -- **`$contains`** (Postgres) - filter based on if a column contains a value +- **`$contains`** (Postgres) - filter based on if a column contains all values from array of values -- **`$contained`** (Postgres) - filter based on if a column is contained in a value +- **`$contained`** (Postgres) - filter based on if a column is contained within a serialized object #### Params Operators diff --git a/package-lock.json b/package-lock.json index 269780d..c77498f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "feathers-objection", - "version": "4.4.1", + "version": "4.4.2", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 384b59d..101219e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "feathers-objection", "description": "A service plugin for ObjectionJS an ORM based on KnexJS", - "version": "4.4.1", + "version": "4.4.2", "homepage": "https://github.com/feathersjs-ecosystem/feathers-objection", "keywords": [ "feathers", diff --git a/src/index.js b/src/index.js index 8364e59..fc7cfc6 100644 --- a/src/index.js +++ b/src/index.js @@ -51,14 +51,14 @@ const OPERATORS_MAP = { $all: '?&' }; -const RANGE_OPERATORS = [ +const DESERIALIZED_ARRAY_OPERATORS = [ 'between', 'not between', '?|', '?&' ]; -const JSON_OPERATORS = [ +const NON_COMPARISON_OPERATORS = [ '@>', '?', '<@', @@ -213,19 +213,21 @@ class Service extends AdapterService { refColumn = ref(`${this.Model.tableName}.${methodKey || column}:${(methodKey ? column : key).replace(/\(/g, '[').replace(/\)/g, ']')}`); } - if (RANGE_OPERATORS.includes(operator) && typeof value === 'string' && value[0] === '[' && value[value.length - 1] === ']') { - value = JSON.parse(value); + if (operator === '@>') { + if (Array.isArray(value)) { value = JSON.stringify(value); } + } else if (DESERIALIZED_ARRAY_OPERATORS.includes(operator)) { + if (typeof value === 'string' && value[0] === '[' && value[value.length - 1] === ']') { value = JSON.parse(value); } } return query.where( - JSON_OPERATORS.includes(operator) ? refColumn : refColumn.castText(), + NON_COMPARISON_OPERATORS.includes(operator) ? refColumn : refColumn.castText(), operator, value ); } } - if (RANGE_OPERATORS.includes(operator) && typeof value === 'string' && value[0] === '[' && value[value.length - 1] === ']') { + if (DESERIALIZED_ARRAY_OPERATORS.includes(operator) && typeof value === 'string' && value[0] === '[' && value[value.length - 1] === ']') { value = JSON.parse(value); } diff --git a/test/index.test.js b/test/index.test.js index 5d8b59e..7eb274a 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -1263,7 +1263,7 @@ describe('Feathers Objection Service', () => { { name: 'Apple', jsonbObject: { z: 0 }, - jsonbArray: [0] + jsonbArray: ['a', 'b'] } ]); }); @@ -1284,12 +1284,24 @@ describe('Feathers Objection Service', () => { }); }); - it('$contains', () => { + it('$contains number', () => { return companies.find({ query: { jsonbArray: { $contains: 1 } } }).then(data => { expect(data[0].name).to.be.equal('Google'); }); }); + it('$contains array of numbers', () => { + return companies.find({ query: { jsonbArray: { $contains: [2, 1] } } }).then(data => { + expect(data[0].name).to.be.equal('Google'); + }); + }); + + it('$contains array of string', () => { + return companies.find({ query: { jsonbArray: { $contains: ['b', 'a'] } } }).then(data => { + expect(data[0].name).to.be.equal('Apple'); + }); + }); + it('$contains - nested', () => { return companies.find({ query: { jsonbObject: { e: { $contains: 4 } } } }).then(data => { expect(data[0].name).to.be.equal('Google');